{"_id":"@api-analytics/koa","name":"@api-analytics/koa","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@api-analytics/koa","version":"1.0.0","description":"API Analytics middleware for Koa applications.","main":"index.js","exports":"./index.js","type":"module","engines":{"node":">=18"},"keywords":["analytics","api","dashboard","koa","middleware"],"author":{"name":"Tom Draper"},"license":"MIT","dependencies":{"@api-analytics/core":"^1.0.0"},"peerDependencies":{"koa":">=2.0.0"},"repository":{"type":"git","url":"git+https://github.com/tom-draper/api-analytics.git"},"bugs":{"url":"https://github.com/tom-draper/api-analytics/issues"},"homepage":"https://github.com/tom-draper/api-analytics#readme","publishConfig":{"access":"public"},"gitHead":"2a10aad1a61c468131c373a3b7ca0934830d2e70","_id":"@api-analytics/koa@1.0.0","_nodeVersion":"22.16.0","_npmVersion":"11.7.0","dist":{"integrity":"sha512-ifTWPSVlTbRc532J/tf1vSU+odF8Mm9r6mnDSk0eYidzExXrHOhQ1qMLVVZ9KlHKVhiI9igzj9qcBWEQQjJYNg==","shasum":"5bb0c80097e987dbaf7c0b6492fbe15ec52cedd6","tarball":"https://registry.npmjs.org/@api-analytics/koa/-/koa-1.0.0.tgz","fileCount":3,"unpackedSize":9160,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCurj3QKML2TH2wHyTag1Xs/KfMRyf07dOWK4PhLkH99AIhAKwgTt3usvhjU0IWV4GzvhRg3DGGfMQFVZwO+6qVVnIu"}]},"_npmUser":{"name":"tomdraper","email":"tomjdraper1@gmail.com"},"directories":{},"maintainers":[{"name":"tomdraper","email":"tomjdraper1@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/koa_1.0.0_1773232867338_0.33241669940592167"},"_hasShrinkwrap":false}},"time":{"created":"2026-03-11T12:41:07.245Z","1.0.0":"2026-03-11T12:41:07.509Z","modified":"2026-03-11T12:41:07.706Z"},"maintainers":[{"name":"tomdraper","email":"tomjdraper1@gmail.com"}],"description":"API Analytics middleware for Koa applications.","homepage":"https://github.com/tom-draper/api-analytics#readme","keywords":["analytics","api","dashboard","koa","middleware"],"repository":{"type":"git","url":"git+https://github.com/tom-draper/api-analytics.git"},"author":{"name":"Tom Draper"},"bugs":{"url":"https://github.com/tom-draper/api-analytics/issues"},"license":"MIT","readme":"# Koa Analytics\n\nA free and lightweight API analytics solution, complete with a dashboard.\n\n## Getting Started\n\n### 1. Generate an API key\n\nHead to [apianalytics.dev/generate](https://apianalytics.dev/generate) to generate your unique API key with a single click. This key is used to monitor your specific API and should be stored privately. It's also required in order to access your API analytics dashboard and data.\n\n### 2. Add middleware to your API\n\nAdd our lightweight middleware to your API. Almost all processing is handled by our servers so there is minimal impact on the performance of your API.\n\n[![npm](https://img.shields.io/npm/v/@api-analytics/koa)](https://www.npmjs.com/package/@api-analytics/koa)\n\n```bash\nnpm install @api-analytics/koa\n```\n\n```js\nimport Koa from 'koa';\nimport { koaAnalytics } from '@api-analytics/koa';\n\nconst app = new Koa();\n\napp.use(koaAnalytics('<API-KEY>'));  // Add middleware\n\napp.use((ctx) => {\n    ctx.body = { message: 'Hello World!' };\n});\n\napp.listen(8080, () => {\n    console.log('Server listening at http://localhost:8080');\n});\n```\n\n### 3. View your analytics\n\nYour API will now log and store incoming request data on all routes. Your logged data can be viewed using two methods:\n\n1. Through visualizations and statistics on the dashboard\n2. Accessed directly via the data API\n\nYou can use the same API key across multiple APIs, but all of your data will appear in the same dashboard. We recommend generating a new API key for each additional API server you want analytics for.\n\n#### Dashboard\n\nHead to [apianalytics.dev/dashboard](https://apianalytics.dev/dashboard) and paste in your API key to access your dashboard.\n\nDemo: [apianalytics.dev/dashboard/demo](https://apianalytics.dev/dashboard/demo)\n\n![dashboard](https://user-images.githubusercontent.com/41476809/272061832-74ba4146-f4b3-4c05-b759-3946f4deb9de.png)\n\n#### Data API\n\nLogged data for all requests can be accessed via our REST API. Simply send a GET request to `https://apianalytics-server.com/api/data` with your API key set as `X-AUTH-TOKEN` in the headers.\n\n##### Python\n\n```py\nimport requests\n\nheaders = {\n    \"X-AUTH-TOKEN\": \"<API-KEY>\"\n}\n\nresponse = requests.get(\"https://apianalytics-server.com/api/data\", headers=headers)\nprint(response.json())\n```\n\n##### Node.js\n\n```js\nfetch(\"https://apianalytics-server.com/api/data\", {\n    headers: { \"X-AUTH-TOKEN\": \"<API-KEY>\" },\n})\n    .then((response) => response.json())\n    .then((data) => console.log(data));\n```\n\n##### cURL\n\n```bash\ncurl --header \"X-AUTH-TOKEN: <API-KEY>\" https://apianalytics-server.com/api/data\n```\n\n##### Parameters\n\nYou can filter your data by providing URL parameters in your request.\n\n- `page` - the page number, with a max page size of 50,000 (defaults to 1)\n- `date` - the exact day the requests occurred on (`YYYY-MM-DD`)\n- `dateFrom` - a lower bound of a date range the requests occurred in (`YYYY-MM-DD`)\n- `dateTo` - an upper bound of a date range the requests occurred in (`YYYY-MM-DD`)\n- `hostname` - the hostname of your service\n- `ipAddress` - the IP address of the client\n- `status` - the status code of the response\n- `location` - a two-character location code of the client\n- `user_id` - a custom user identifier (only relevant if a `getUserID` mapper function has been set)\n\nExample:\n\n```bash\ncurl --header \"X-AUTH-TOKEN: <API-KEY>\" https://apianalytics-server.com/api/data?page=3&dateFrom=2022-01-01&hostname=apianalytics.dev&status=200&user_id=b56cbd92-1168-4d7b-8d94-0418da207908\n```\n\n## Customisation\n\nCustom mapping functions can be assigned to override the default behaviour and define how values are extracted from each incoming request to better suit your specific API.\n\n```js\nimport Koa from 'koa';\nimport { koaAnalytics, Config } from '@api-analytics/koa';\n\nconst config = new Config();\nconfig.getIPAddress = (ctx) => ctx.headers['x-forwarded-for'];\nconfig.getUserAgent = (ctx) => ctx.headers['user-agent'];\nconfig.getUserID = (ctx) => ctx.headers['x-auth-token'] ?? null;\n\nconst app = new Koa();\n\napp.use(koaAnalytics('<API-KEY>', config));  // Add middleware\n```\n\n## Client ID and Privacy\n\nBy default, API Analytics logs and stores the client IP address of all incoming requests made to your API and infers a location (country) from each IP address if possible. The IP address is used as a form of client identification in the dashboard to estimate the number of users accessing your service.\n\nThis behaviour can be controlled through a privacy level defined in the configuration of the API middleware. There are three privacy levels to choose from 0 (default) to a maximum of 2. A privacy level of 1 will disable IP address storing, and a value of 2 will also disable location inference.\n\nPrivacy Levels:\n\n- `0` - The client IP address is used to infer a location and then stored for user identification. (default)\n- `1` - The client IP address is used to infer a location and then discarded.\n- `2` - The client IP address is never accessed and location is never inferred.\n\n```js\nconst config = new Config();\nconfig.privacyLevel = 2;  // Disable IP storing and location inference\n```\n\nWith any of these privacy levels, there is the option to define a custom user ID as a function of a request by providing a mapper function in the API middleware configuration. For example, your service may require an API key sent in the `X-AUTH-TOKEN` header field that can be used to identify a user. In the dashboard, this custom user ID will identify the user in conjunction with the IP address or as an alternative.\n\n```js\nconst config = new Config();\nconfig.getUserID = (ctx) => ctx.headers['x-auth-token'] ?? null;\n```\n\n## Data and Security\n\nAll data is stored securely in compliance with The EU General Data Protection Regulation (GDPR).\n\nFor any given request to your API, data recorded is limited to:\n\n- Path requested by client\n- Client IP address (optional)\n- Client operating system\n- Client browser\n- Request method (GET, POST, PUT, etc.)\n- Time of request\n- Status code\n- Response time\n- API hostname\n- API framework (Koa)\n\nData collected is only ever used to populate your analytics dashboard. All stored data is pseudo-anonymous, with the API key the only link between you and your logged request data. Should you lose your API key, you will have no method to access your API analytics.\n\n### Data Deletion\n\nAt any time you can delete all stored data associated with your API key by going to [apianalytics.dev/delete](https://apianalytics.dev/delete) and entering your API key.\n\nAPI keys and their associated logged request data are scheduled to be deleted after 6 months of inactivity.\n\n## Monitoring\n\nActive API monitoring can be set up by heading to [apianalytics.dev/monitoring](https://apianalytics.dev/monitoring) to enter your API key. Our servers will regularly ping chosen API endpoints to monitor uptime and response time.\n\n![Monitoring](https://user-images.githubusercontent.com/41476809/208298759-f937b668-2d86-43a2-b615-6b7f0b2bc20c.png)\n\n## Contributions\n\nContributions, issues and feature requests are welcome.\n\n- Fork it (https://github.com/tom-draper/api-analytics)\n- Create your feature branch (`git checkout -b my-new-feature`)\n- Commit your changes (`git commit -am 'Add some feature'`)\n- Push to the branch (`git push origin my-new-feature`)\n- Create a new Pull Request\n\n---\n\nIf you find value in my work consider supporting me.\n\nBuy Me a Coffee: https://www.buymeacoffee.com/tomdraper<br>\nPayPal: https://www.paypal.com/paypalme/tomdraper\n","readmeFilename":"README.md","_rev":"1-3d18f105219aabd693524ef4dbb15d64"}