Installation
Install the Koa.js integration package:Node 18+ required
Basic Setup
Add ApiTraffic middleware to your Koa.js application:Configuration Options
Configuration Table
Option | Environment Variable | Required | Type | Description |
---|---|---|---|---|
token | API_TRAFFIC_TOKEN | Yes | String | Ingest token from your ApiTraffic account |
bucket | API_TRAFFIC_BUCKET | Yes | String | Bucket ID for data organization |
interceptOutbound | API_TRAFFIC_INTERCEPT_OUTBOUND | No | Boolean | Monitor outbound HTTP requests (default: true) |
debug | API_TRAFFIC_DEBUG | No | Boolean | Enable debug logging (default: false) |
Advanced Usage
Custom Tagging
Add custom tags to requests for better organization and searchability:Request Tracing
Add trace messages for debugging and monitoring:Error Handling
ApiTraffic automatically captures errors, but you can add additional context:Environment Variables
Configure using environment variables:.env
Middleware Order
Place ApiTraffic middleware early in your middleware stack:TypeScript Support
The package includes TypeScript definitions:With Koa Router
Using ApiTraffic with@koa/router
:
Security Features
Data Redaction
ApiTraffic automatically redacts sensitive data based on your account settings. No code changes required - configure redaction rules in your ApiTraffic dashboard.Request Exclusions
Exclude specific endpoints from monitoring by configuring exclusion rules in your ApiTraffic account. Common exclusions include:- Health check endpoints
- Static asset requests
- Internal monitoring endpoints
Troubleshooting
Middleware not capturing requests
Middleware not capturing requests
Ensure ApiTraffic middleware is placed before your route handlers and after body parsing middleware.
Missing environment variables
Missing environment variables
Verify that
API_TRAFFIC_TOKEN
and API_TRAFFIC_BUCKET
are set in your environment.Outbound requests not being tracked
Outbound requests not being tracked
Set
interceptOutbound: true
in your configuration to enable outbound request monitoring.Context not available in routes
Context not available in routes
Make sure you’re using async/await syntax and that ApiTraffic middleware is properly registered.