Mock API Testing Guide
Audience: Developer, Admin
Page Type: How-To
Overview
COS ships with a mock KPI API (tools/mock-kpi-api/) that simulates an external data source. Use it to test pull integrations without needing a real BI tool, data warehouse, or monitoring system.
The mock API:
- Supports all 5 auth modes (NONE, BEARER_TOKEN, BASIC_AUTH, API_KEY_HEADER, API_KEY_QUERY)
- Returns realistic KPI data (revenue, NPS, churn, etc.)
- Provides multiple response formats (flat, nested, timeseries, paginated, multi-metric)
- Simulates error scenarios (timeout, 500, rate limit)
Quick Start
cd tools/mock-kpi-api
npm install
npm start
# → http://localhost:4444
For development with auto-reload:
npm run dev
Authentication
All /api/* endpoints require authentication. Use one of the following methods:
Bearer Token
curl -H "Authorization: Bearer mock-token-2024" \
http://localhost:4444/api/metrics/revenue
Basic Auth
curl -u "cosuser:cospass123" \
http://localhost:4444/api/metrics/revenue
API Key (Header)
curl -H "X-API-Key: mock-api-key-xyz" \
http://localhost:4444/api/metrics/revenue
API Key (Query Parameter)
curl "http://localhost:4444/api/metrics/revenue?api_key=mock-api-key-xyz"
No Auth
Set AUTH_REQUIRED=false environment variable when starting the server:
AUTH_REQUIRED=false npm start
Default Credentials
| Mode | Credential | Value |
|---|---|---|
| Bearer Token | Token | mock-token-2024 |
| Basic Auth | Username | cosuser |
| Basic Auth | Password | cospass123 |
| API Key | Key | mock-api-key-xyz |
Override via environment variables: BEARER_TOKEN, BASIC_USER, BASIC_PASS, API_KEY.
Endpoints
Flat Format — GET /api/metrics/:name
The simplest format. Returns a single metric value.
curl -H "Authorization: Bearer mock-token-2024" \
http://localhost:4444/api/metrics/revenue
Response:
{
"metric": "revenue",
"value": 1250000,
"target": 1100000,
"forecast": 1180000,
"unit": "USD",
"achievementRate": 113.6,
"period": "2026-03",
"timestamp": "2026-03-06T10:00:00.000Z"
}
COS Response Mapping:
{ "actual": "$.value", "target": "$.target", "forecast": "$.forecast" }
Nested Format — GET /api/metrics/:name/detailed
Values are nested inside a data.metrics object.
curl -H "Authorization: Bearer mock-token-2024" \
http://localhost:4444/api/metrics/nps/detailed
Response:
{
"status": "success",
"data": {
"kpiName": "Net Promoter Score",
"description": "Customer satisfaction score",
"metrics": {
"current": 72.5,
"target": 75.0,
"forecast": 73.8,
"previousPeriod": 68.2,
"achievementRate": 96.7
},
"metadata": {
"unit": "score",
"period": "2026-03",
"collectedAt": "2026-03-06T10:00:00.000Z",
"source": "mock-kpi-api"
}
}
}
COS Response Mapping:
{ "actual": "$.data.metrics.current", "target": "$.data.metrics.target", "forecast": "$.data.metrics.forecast" }
Timeseries Format — GET /api/metrics/:name/timeseries
Returns an array of historical data points.
curl -H "Authorization: Bearer mock-token-2024" \
"http://localhost:4444/api/metrics/revenue/timeseries?months=3"
Response:
{
"metric": "revenue",
"period": "2026-03",
"count": 3,
"series": [
{ "period": "2026-01", "periodStart": "2026-01-01T...", "periodEnd": "2026-01-31T...", "target": 1100000, "value": 980000, "unit": "USD" },
{ "period": "2026-02", "periodStart": "2026-02-01T...", "periodEnd": "2026-02-28T...", "target": 1150000, "value": 1120000, "unit": "USD" },
{ "period": "2026-03", "periodStart": "2026-03-01T...", "periodEnd": "2026-03-31T...", "target": 1200000, "value": 1250000, "unit": "USD" }
]
}
COS Response Mapping (latest value):
{ "actual": "$.series[-1:].value", "target": "$.series[-1:].target" }
Paginated Format — GET /api/metrics/:name/paginated
Simulates a paginated API with page and per_page query params.
curl -H "X-API-Key: mock-api-key-xyz" \
"http://localhost:4444/api/metrics/revenue/paginated?page=1&per_page=3"
Response:
{
"pagination": {
"page": 1,
"perPage": 3,
"total": 24,
"totalPages": 8,
"hasNext": true
},
"results": [
{ "period": "2024-04", "measurement": 1050000, "target": 1100000, "unit": "USD" },
{ "period": "2024-05", "measurement": 1080000, "target": 1100000, "unit": "USD" },
{ "period": "2024-06", "measurement": 1120000, "target": 1150000, "unit": "USD" }
]
}
COS Response Mapping:
{ "actual": "$.results[0].measurement", "target": "$.results[0].target" }
Dashboard Summary — GET /api/dashboard/summary
All metrics in a single response.
curl -H "Authorization: Bearer mock-token-2024" \
http://localhost:4444/api/dashboard/summary
Response:
{
"status": "success",
"period": "2026-03",
"metricsCount": 8,
"kpis": {
"revenue": { "displayName": "Gelir", "actual": 1250000, "target": 1100000, ... },
"nps": { "displayName": "Net Promoter Score", "actual": 72.5, "target": 75.0, ... },
"customer-count": { ... },
...
}
}
COS Response Mapping (for revenue):
{ "actual": "$.kpis.revenue.actual", "target": "$.kpis.revenue.target" }
Error Scenarios
Test how COS handles various error conditions:
| Endpoint | Behavior |
|---|---|
GET /api/error/timeout | Responds after 10 seconds (tests timeout handling) |
GET /api/error/500 | Returns HTTP 500 Internal Server Error |
GET /api/error/rate-limit | Returns HTTP 429 Too Many Requests |
Available Metrics
| Name | Description | Value Range |
|---|---|---|
revenue | Monthly revenue | 800K – 1.5M USD |
customer-count | Active customers | 4,000 – 6,500 |
nps | Net Promoter Score | 35 – 85 |
churn-rate | Monthly churn rate | 1% – 8% |
employee-satisfaction | Employee satisfaction | 55% – 95% |
uptime | System uptime | 95% – 99.99% |
conversion-rate | Website conversion | 1% – 12% |
response-time | API response time | 50 – 500 ms |
Any custom name also works (e.g., /api/metrics/my-custom-kpi) — returns random values.
COS Integration Setup Example
To test with the mock API in COS:
1. Create Integration Endpoint
| Field | Value |
|---|---|
| Name | Mock KPI API |
| Base URL | http://localhost:4444 |
| Auth Type | BEARER_TOKEN |
| Credentials | { "token": "mock-token-2024" } |
2. Create KPI Binding
| Field | Value |
|---|---|
| Path Suffix | /api/metrics/revenue |
| HTTP Method | GET |
| Response Mapping | { "actual": "$.value", "target": "$.target" } |
| Cron Expression | 0 */6 * * * (every 6 hours) |
3. Test (Dry Run)
Click the Test button to verify the integration. You should see:
- HTTP Status: 200
- Extracted values:
{ actual: 1250000, target: 1100000 }
Environment Variables
| Variable | Default | Description |
|---|---|---|
PORT | 4444 | Server port |
BEARER_TOKEN | mock-token-2024 | Expected Bearer token |
BASIC_USER | cosuser | Basic auth username |
BASIC_PASS | cospass123 | Basic auth password |
API_KEY | mock-api-key-xyz | Expected API key |
AUTH_REQUIRED | true | Set to false to disable auth |