|
1 | | -# Matomo ApiReference Plugin |
| 1 | +# API Reference Plugin for Matomo |
2 | 2 |
|
3 | 3 | ## Description |
4 | 4 |
|
5 | | -Allow generating OpenAPI documentation for the Matomo public APIs. |
| 5 | +As APIs and plugins expose additional metadata, developers can browse available API endpoints, explore request parameters and response formats, and test API requests directly from the documentation interface. |
6 | 6 |
|
7 | | -## Frontend assets |
8 | | -Swagger UI is managed via npm inside [vue/package.json](/vue/package.json). The plugin runtime does not load Swagger UI from `node_modules`; instead, the needed distributable files are synced into `vue/lib/swagger-ui/`. |
| 7 | +### Features include: |
| 8 | + |
| 9 | +* Interactive Swagger/OpenAPI-based API documentation |
| 10 | +* Searchable API methods and endpoints |
| 11 | +* Request and response schema documentation |
| 12 | +* Parameter descriptions and example requests |
| 13 | +* Improved navigation and API discoverability |
| 14 | +* Support for modern API documentation standards |
| 15 | + |
| 16 | +The API Reference plugin improves API discoverability while supporting modern API documentation standards and tooling. |
| 17 | + |
| 18 | +## Access the API Reference |
| 19 | + |
| 20 | +Navigate to: |
| 21 | + |
| 22 | +```text |
| 23 | +Administration → System → SwaggerUI |
| 24 | +``` |
| 25 | + |
| 26 | +The Swagger UI page will display API documentation for installed plugins. |
| 27 | + |
| 28 | +## Generate OpenAPI Specifications |
| 29 | + |
| 30 | +OpenAPI specifications are generated daily using a scheduled Matomo task. |
| 31 | + |
| 32 | +To manually regenerate specifications: |
| 33 | + |
| 34 | +```bash |
| 35 | +./console core:run-scheduled-tasks "Piwik\Plugins\ApiReference\Tasks.generateConfiguredPluginSpecs" |
| 36 | +``` |
| 37 | + |
| 38 | +This is useful after installing, enabling, or updating plugins. |
| 39 | + |
| 40 | +## Authorise API Requests |
| 41 | + |
| 42 | +To test API endpoints from Swagger UI: |
| 43 | + |
| 44 | +1. Create or copy your Matomo API token from your user settings |
| 45 | +2. Open the Swagger page |
| 46 | +3. Click **Authorize** |
| 47 | +4. Enter your API token |
| 48 | +5. Confirm authorisation |
| 49 | + |
| 50 | +Swagger UI will then execute requests using your Matomo permissions. |
| 51 | + |
| 52 | +## Try an API Endpoint |
| 53 | + |
| 54 | +1. Expand an endpoint in Swagger UI |
| 55 | +2. Review available parameters |
| 56 | +3. Click **Try it out** |
| 57 | +4. Update request values if needed |
| 58 | +5. Click **Execute** |
| 59 | + |
| 60 | +Responses are returned directly from your Matomo instance. |
| 61 | + |
| 62 | +## Technical Details |
| 63 | + |
| 64 | +### Frontend Assets |
| 65 | + |
| 66 | +Swagger UI is managed via npm inside `vue/package.json`. |
| 67 | + |
| 68 | +The plugin runtime does not load Swagger UI directly from `node_modules`. Instead, the required distributable files are synced into: |
| 69 | + |
| 70 | +```text |
| 71 | +vue/lib/swagger-ui/ |
| 72 | +``` |
9 | 73 |
|
10 | 74 | Typical workflow: |
11 | | -- Run `npm install` in `plugins/ApiReference/vue`. |
12 | | -- Run `npm run sync-swagger-ui` in `plugins/ApiReference/vue` after adding or updating `swagger-ui-dist`. |
13 | | -- Run `ddev matomo:console vue:build ApiReference` after changing Vue source. |
14 | 75 |
|
15 | | -The plugin-specific Swagger overrides live in [vue/src/SwaggerPage/swagger-ui-overrides.css](/vue/src/SwaggerPage/swagger-ui-overrides.css). |
| 76 | +```bash |
| 77 | +cd plugins/ApiReference/vue |
| 78 | + |
| 79 | +npm install |
| 80 | +npm run sync-swagger-ui |
| 81 | + |
| 82 | +ddev matomo:console vue:build ApiReference |
| 83 | +``` |
| 84 | + |
| 85 | +Plugin-specific Swagger UI overrides are located in: |
| 86 | + |
| 87 | +```text |
| 88 | +vue/src/SwaggerPage/swagger-ui-overrides.css |
| 89 | +``` |
| 90 | + |
| 91 | +### Dependencies |
| 92 | + |
| 93 | +This plugin uses vendored dependencies scoped with `matomo scoper` to avoid conflicts with dependencies used by other plugins. |
| 94 | + |
| 95 | +If updating dependencies: |
| 96 | + |
| 97 | +1. Run `composer install` |
| 98 | +2. Re-scope dependencies using the plugin scoper configuration |
| 99 | +3. Run Rector to ensure compatibility with Matomo's minimum supported PHP version |
| 100 | + |
| 101 | +Example Rector configuration: |
16 | 102 |
|
17 | | -## Dependencies |
18 | | -This plugin had its vendored dependencies scoped using [matomo scoper](https://github.com/matomo-org/matomo-scoper). This means that composer packages are prefixed so that they won't conflict with the same libraries used by other plugins. |
19 | | -If you need to update a dependency, you should be able to run `composer install` to populate the vendor directory and then follow the [instructions for scoping a plugin](https://github.com/matomo-org/matomo-scoper#how-to-scope-a-matomo-plugin). Since the scoper.inc.php file already exists, it will hopefully be as simple as running the scoper for this plugin. Once that's done, you'll also need to make some of the dependencies compatible with Matomo's minimum supported version of PHP. |
20 | | -This is done using the [Rector library](https://github.com/rectorphp/rector-downgrade-php). It's preferable that you install the composer package in a separate project and point to this project so that it doesn't get committed in this project. You should also have a config file saved containing the following: |
21 | 103 | ```php |
22 | 104 | <?php |
23 | 105 |
|
24 | 106 | use Rector\Config\RectorConfig; |
25 | 107 |
|
26 | 108 | return static function (RectorConfig $rectorConfig): void { |
27 | | - // Matomo requires PHP >= 7.2.5, but PHP 7.3 is close enough. We don't want to downgrade further than necessary. |
| 109 | + // Matomo requires PHP >= 7.2.5, but PHP 7.3 is close enough. |
28 | 110 | $rectorConfig->sets([ |
29 | 111 | \Rector\Set\ValueObject\DowngradeLevelSetList::DOWN_TO_PHP_73 |
30 | 112 | ]); |
31 | 113 |
|
32 | 114 | $rectorConfig->skip([ |
33 | 115 | \Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector::class, |
| 116 | + |
34 | 117 | // Skip downgrading Token for php-parser since it already provides a polyfill |
35 | 118 | \Rector\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector::class => [ |
36 | 119 | '*/vendor/prefixed/nikic/php-parser/*', |
37 | 120 | ], |
38 | 121 | ]); |
39 | 122 | }; |
40 | 123 | ``` |
41 | | -With all that in place, you should be able to run Rector like so: `vendor/bin/rector process {path_to_this_plugin/vendor/prefixed} --config={path_to_config_file}` |
42 | 124 |
|
43 | | -> **_NOTE:_** For Matomo developers, there's an internal DevPluginCommands plugin with a command that handles scoping and running Rector. See the SearchEngineKeywordsPerformance plugin's README.md for more details. |
| 125 | +Run Rector using: |
| 126 | + |
| 127 | +```bash |
| 128 | +vendor/bin/rector process {path_to_this_plugin/vendor/prefixed} --config={path_to_config_file} |
| 129 | +``` |
| 130 | + |
| 131 | +> NOTE: Internal Matomo development environments may include the `DevPluginCommands` plugin, which provides commands to automate dependency scoping and Rector processing. See the `SearchEngineKeywordsPerformance` plugin README for additional details. |
| 132 | +
|
0 commit comments