Analytics API
This page documents the 12 gateway endpoints tagged analytics in the gateway's OpenAPI document. Every example targets a gateway on http://localhost:4000 and authenticates with the SPANWAY_API_KEY environment variable, which holds the master key or a virtual key that has access to the route. Request and response fields are taken from the schema the running gateway publishes at /openapi.json, and the same document is bundled with this site as openapi.json.
| Method | Path | Summary |
|---|---|---|
| POST | /analytics/explore | Chart a usage metric grouped by up to two dimensions |
| POST | /analytics/query | Run a read only SQL query over the analytics tables |
| POST | /analytics/query/draft | Draft an analytics query from a question with Ask AI |
| GET | /analytics/query/help | List the tables and functions an analytics query may use |
| GET | /analytics/saved | List saved queries and charts |
| POST | /analytics/saved | Save a query or chart |
| GET | /analytics/saved/{query_id} | Read one saved query or chart |
| PATCH | /analytics/saved/{query_id} | Change a saved query or chart |
| DELETE | /analytics/saved/{query_id} | Delete a saved query or chart |
| GET | /analytics/trends | Rank what moved most against the previous period |
| GET | /analytics/users/active | Daily active and new end users of your application |
| GET | /analytics/users/retention | Weekly retention of your application's end users |
Endpoints
POST /analytics/explore
Chart a usage metric grouped by up to two dimensions
Add up one metric, such as spend or requests, from the daily usage tables between two dates, grouped by up to two dimensions such as date and model, with optional filters. Grouping or filtering by user, team, end user or tag reads that table, and only one of them may be used at a time. Rows follow the same role scope as SQL queries and nothing changes. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#explore)
Request body (application/json, schema ExploreRequest):
| Field | Type | Required | Description |
|---|---|---|---|
end_date | string | Yes | End Date |
filters | array of ExploreFilter | No | Filters |
group_by | array of enum: "date", "model", "model_group", "provider", "api_key", "endpoint", "mcp_tool", "user", ... | No | Group By |
limit | integer | No | Most groups returned, largest values first |
metric | enum: "spend", "requests", "successful_requests", "failed_requests", "tokens", "prompt_tokens", "completion_tokens", "avg_latency_ms" | No | Metric |
start_date | string | Yes | Start Date |
Example request:
curl -X POST "http://localhost:4000/analytics/explore" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"end_date": "2026-11-22",
"group_by": [
"date",
"model"
],
"metric": "spend",
"start_date": "2026-11-01"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | One row per group | application/json ExploreResult |
| 400 | The dimensions come from different tables | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The key has no gateway user, the role may not do this, or analytics are turned off | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 429 | Too many queries are running on this gateway instance | application/json ErrorDetail |
| 500 | The gateway runs without a database | application/json ErrorDetail |
| 503 | The database did not answer | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
group_by | array of enum: "date", "model", "model_group", "provider", "api_key", "endpoint", "mcp_tool", "user", ... | Yes | Group By |
metric | enum: "spend", "requests", "successful_requests", "failed_requests", "tokens", "prompt_tokens", "completion_tokens", "avg_latency_ms" | Yes | Metric |
rows | array of ExploreRow | Yes | Rows |
source | string | Yes | The analytics table the numbers came from |
truncated | boolean | Yes | Truncated |
POST /analytics/query
Run a read only SQL query over the analytics tables
Run one SELECT over the request log and daily usage tables, for example spend per model this week. Admins and admin viewers see every row, other users see their own rows and their permitted teams' rows. The query runs in a read only transaction with a time limit and a row limit, only listed tables and functions are allowed, and prompts, answers, metadata and IP addresses are never readable. Each run writes an audit row with a hash of the SQL. Docs (https://docs.spanway.ai/docs/proxy/analytics_query)
Request body (application/json, schema AnalyticsQueryRequest):
| Field | Type | Required | Description |
|---|---|---|---|
max_rows | integer or null | No | Fewer rows than the gateway limit |
sql | string | Yes | One SELECT statement over the tables GET /analytics/query/help lists |
Example request:
curl -X POST "http://localhost:4000/analytics/query" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"sql": "SELECT model, count(*) AS requests, round(sum(spend)::numeric, 4) AS spend FROM spend_logs WHERE start_time > now() - interval '\''7 days'\'' GROUP BY model ORDER BY spend DESC"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The rows the query returned | application/json AnalyticsQueryResult |
| 400 | The SQL was refused by the checks, failed in the database or ran past the time limit | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The key has no gateway user, the role may not do this, or analytics are turned off | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 429 | Too many queries are running on this gateway instance | application/json ErrorDetail |
| 500 | The gateway runs without a database | application/json ErrorDetail |
| 503 | The database did not answer | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
columns | array of string | Yes | Column names in the order the query returned them |
elapsed_ms | integer | Yes | How long the database took, in milliseconds |
row_count | integer | Yes | Number of rows returned |
rows | array of array of JsonValue | Yes | The rows, each value in the column order |
truncated | boolean | Yes | True when the query had more rows than the row limit |
POST /analytics/query/draft
Draft an analytics query from a question with Ask AI
Turn a question in plain words into one SQL query over the analytics tables, written by a model on this gateway. The draft is checked like a real run and returned as text, nothing runs until you run it. Proxy admins only, because the model call is billed to the gateway. Each draft writes an audit row. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#ask-ai)
Request body (application/json, schema AnalyticsDraftRequest):
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Yes | A model group on this gateway that writes the draft |
question | string | Yes | The question in plain words, for example spend per model last week |
Example request:
curl -X POST "http://localhost:4000/analytics/query/draft" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.5",
"question": "Which models cost the most this week?"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The drafted SQL and whether it passes the checks | application/json AnalyticsDraft |
| 400 | The model is not served by this gateway | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The caller is not a proxy admin, or analytics are turned off | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model |
problem | string or null | No | Why the draft would be refused, when it would be |
sql | string | Yes | The drafted query. It is not run until you run it |
valid | boolean | Yes | True when the draft passes the same checks a query run does |
GET /analytics/query/help
List the tables and functions an analytics query may use
Describe what POST /analytics/query may read: each table with its columns, which rows your role sees in it, the functions a query may call, the row and time limits and example queries. Any gateway user may call it and nothing changes. Docs (https://docs.spanway.ai/docs/proxy/analytics_query)
Example request:
curl -X GET "http://localhost:4000/analytics/query/help" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The query reference for the caller | application/json AnalyticsQueryHelp |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The key has no gateway user, the role may not do this, or analytics are turned off | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
examples | array of AnalyticsExample | Yes | Examples |
functions | array of string | Yes | Functions a query may call |
limits | AnalyticsLimits | Yes | |
scope | enum: "all", "owned" | Yes | all for admins, owned when only your own and team rows show |
tables | array of AnalyticsTable | Yes | Tables |
GET /analytics/saved
List saved queries and charts
List your own saved queries and charts plus the ones shared with everyone, newest first. Admins and admin viewers see every item. Nothing changes. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#saved-queries-and-charts)
Example request:
curl -X GET "http://localhost:4000/analytics/saved" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The saved items | application/json SavedQueryList |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The key has no gateway user, the role may not do this, or analytics are turned off | application/json ErrorDetail |
| 500 | The gateway runs without a database | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
items | array of SavedQuery | Yes | Items |
POST /analytics/saved
Save a query or chart
Save a SQL query or an Explore chart under a name. private keeps it to you, shared shows it to everyone who may run queries, and each person who opens it still only sees their own rows. Proxy admins and internal users may save, and the new item is written to the audit log. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#saved-queries-and-charts)
Request body (application/json, schema SavedQueryCreate):
| Field | Type | Required | Description |
|---|---|---|---|
definition | SavedSql or SavedChart | Yes | Definition |
description | string | No | Description |
name | string | Yes | Name |
visibility | enum: "private", "shared" | No | Visibility |
Example request:
curl -X POST "http://localhost:4000/analytics/saved" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"definition": {
"kind": "sql",
"sql": "SELECT model, count(*) AS requests, round(sum(spend)::numeric, 4) AS spend FROM spend_logs WHERE start_time > now() - interval '\''7 days'\'' GROUP BY model ORDER BY spend DESC"
},
"description": "Weekly cost review",
"name": "Spend per model this week",
"visibility": "shared"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The saved item | application/json SavedQuery |
| 400 | The owner already has the most saved items allowed | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The role may not save queries, or the item belongs to someone else | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
created_at | string | Yes | Created At |
definition | SavedSql or SavedChart | Yes | Definition |
description | string | Yes | Description |
name | string | Yes | Name |
owner_user_id | string or null | Yes | Owner User Id |
query_id | string | Yes | Query Id |
updated_at | string | Yes | Updated At |
visibility | enum: "private", "shared" | Yes | Visibility |
GET /analytics/saved/{query_id}
Read one saved query or chart
Read a saved query or chart you own, or one shared with everyone. Nothing changes. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#saved-queries-and-charts)
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
query_id | path | string | Yes |
Example request:
curl -X GET "http://localhost:4000/analytics/saved/query_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The saved item | application/json SavedQuery |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The key has no gateway user, the role may not do this, or analytics are turned off | application/json ErrorDetail |
| 404 | No saved query has that id, or it is private to someone else | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
created_at | string | Yes | Created At |
definition | SavedSql or SavedChart | Yes | Definition |
description | string | Yes | Description |
name | string | Yes | Name |
owner_user_id | string or null | Yes | Owner User Id |
query_id | string | Yes | Query Id |
updated_at | string | Yes | Updated At |
visibility | enum: "private", "shared" | Yes | Visibility |
PATCH /analytics/saved/{query_id}
Change a saved query or chart
Rename, describe, share, unshare or replace the query or chart of an item you own. Proxy admins may change any item. Fields left out keep their value, and the before and after values go to the audit log. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#saved-queries-and-charts)
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
query_id | path | string | Yes |
Request body (application/json, schema SavedQueryUpdate):
| Field | Type | Required | Description |
|---|---|---|---|
definition | SavedSql or SavedChart or null | No | Definition |
description | string or null | No | Description |
name | string or null | No | Name |
visibility | enum: "private", "shared" or null | No | Visibility |
Example request:
curl -X PATCH "http://localhost:4000/analytics/saved/query_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"visibility": "private"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The changed item | application/json SavedQuery |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The role may not save queries, or the item belongs to someone else | application/json ErrorDetail |
| 404 | No saved query has that id, or it is private to someone else | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
created_at | string | Yes | Created At |
definition | SavedSql or SavedChart | Yes | Definition |
description | string | Yes | Description |
name | string | Yes | Name |
owner_user_id | string or null | Yes | Owner User Id |
query_id | string | Yes | Query Id |
updated_at | string | Yes | Updated At |
visibility | enum: "private", "shared" | Yes | Visibility |
DELETE /analytics/saved/{query_id}
Delete a saved query or chart
Delete an item you own. Proxy admins may delete any item. The deleted item is returned and written to the audit log. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#saved-queries-and-charts)
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
query_id | path | string | Yes |
Example request:
curl -X DELETE "http://localhost:4000/analytics/saved/query_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The deleted item | application/json SavedQuery |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The role may not save queries, or the item belongs to someone else | application/json ErrorDetail |
| 404 | No saved query has that id, or it is private to someone else | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
created_at | string | Yes | Created At |
definition | SavedSql or SavedChart | Yes | Definition |
description | string | Yes | Description |
name | string | Yes | Name |
owner_user_id | string or null | Yes | Owner User Id |
query_id | string | Yes | Query Id |
updated_at | string | Yes | Updated At |
visibility | enum: "private", "shared" | Yes | Visibility |
GET /analytics/trends
Rank what moved most against the previous period
Compare the last days days with the days before them for one metric and one dimension, and list the groups that changed most, for example the models whose spend grew fastest this week. Same role scope as SQL queries, and nothing changes. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#trends)
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
metric | query | enum: "spend", "requests", "successful_requests", "failed_requests", "tokens", "prompt_tokens", "completion_tokens", "avg_latency_ms" | No | What to compare |
dimension | query | enum: "date", "model", "model_group", "provider", "api_key", "endpoint", "mcp_tool", "user", ... | No | What to rank |
days | query | integer | No | Length of each period in days |
limit | query | integer | No | Most movers returned |
Example request:
curl -X GET "http://localhost:4000/analytics/trends" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The biggest movers first | application/json TrendsResult |
| 400 | date is not a dimension trends can rank | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The key has no gateway user, the role may not do this, or analytics are turned off | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 429 | Too many queries are running on this gateway instance | application/json ErrorDetail |
| 500 | The gateway runs without a database | application/json ErrorDetail |
| 503 | The database did not answer | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
current_start | string | Yes | Current Start |
days | integer | Yes | Days |
dimension | enum: "date", "model", "model_group", "provider", "api_key", "endpoint", "mcp_tool", "user", ... | Yes | Dimension |
metric | enum: "spend", "requests", "successful_requests", "failed_requests", "tokens", "prompt_tokens", "completion_tokens", "avg_latency_ms" | Yes | Metric |
movers | array of TrendRow | Yes | Movers |
previous_start | string | Yes | Previous Start |
GET /analytics/users/active
Daily active and new end users of your application
Count the end users of your application per day, from the x-spanway-end-user-id header (or the user field) your application sends with each request: how many were active and how many were seen for the first time. Same role scope as SQL queries, and nothing changes. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#end-user-analytics)
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
start_date | query | string or null | No | First day, defaults to 29 days ago |
end_date | query | string or null | No | Last day, defaults to today |
Example request:
curl -X GET "http://localhost:4000/analytics/users/active" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | One entry per day | application/json ActiveUsersResult |
| 400 | The dates are reversed or too far apart | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The key has no gateway user, the role may not do this, or analytics are turned off | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 429 | Too many queries are running on this gateway instance | application/json ErrorDetail |
| 500 | The gateway runs without a database | application/json ErrorDetail |
| 503 | The database did not answer | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
days | array of ActiveUsersDay | Yes | Days |
total_users | integer | Yes | Distinct end users over the whole window |
GET /analytics/users/retention
Weekly retention of your application's end users
Group end users by the week they first sent a request, from the x-spanway-end-user-id header, and show which share of each group came back in each later week. Same role scope as SQL queries, and nothing changes. Docs (https://docs.spanway.ai/docs/proxy/analytics_query#end-user-analytics)
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
weeks | query | integer | No | Number of weekly cohorts |
Example request:
curl -X GET "http://localhost:4000/analytics/users/retention" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | One cohort per week, oldest first | application/json RetentionResult |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | The key has no gateway user, the role may not do this, or analytics are turned off | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 429 | Too many queries are running on this gateway instance | application/json ErrorDetail |
| 500 | The gateway runs without a database | application/json ErrorDetail |
| 503 | The database did not answer | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
cohorts | array of RetentionCohort | Yes | Cohorts |
weeks | integer | Yes | Weeks |