API Reference

TokenSense is a transparent proxy — you call it the same way you'd call your AI provider directly. This page covers the endpoint format, authentication, and code examples.

Base URL

Your TokenSense endpoint is shown on your dashboard home page. All requests go through this URL:

https://api.tokensense.io/chat/completions

Replace /chat/completions with the appropriate path for your provider and model type.

Supported endpoints

EndpointMethodDescription
/v1/chat/completionsPOSTChat completions (OpenAI format, all providers)
/v1/messagesPOSTNative Anthropic Messages API
/v1beta/models/{model}:{action}POSTNative Gemini API (generateContent, streamGenerateContent)
/v1/embeddingsPOSTText embeddings (OpenAI, xAI, Mistral)
/v1/images/generationsPOSTImage generation (OpenAI, Google Imagen, fal Flux)
/v1/audio/speechPOSTText-to-speech (OpenAI)
/v1/audio/transcriptionsPOSTAudio transcription (OpenAI Whisper)
/v1/modelsGETList available models
/healthGETHealth check (no auth required)

The most common endpoint is /v1/chat/completions — it accepts OpenAI-format requests and automatically translates for Anthropic, Gemini, xAI, and Mistral. Use the native endpoints (/v1/messages, /v1beta/models/...) when you need provider-specific features that don't map to the OpenAI format.

Authentication

Every request must include a Bearer token with your TokenSense API key:

Authorization: Bearer YOUR_TOKENSENSE_KEY

Your TokenSense API key is not your provider key. You add provider keys separately in Settings → Providers. TokenSense uses them to forward requests on your behalf.

Supported formats

TokenSense detects the provider from the model name in your request and routes automatically. You can send requests in any of these formats:

  • OpenAI format/v1/chat/completions with any model. TokenSense translates automatically for all providers.
  • Native Anthropic/v1/messages for the Anthropic Messages API (passthrough, no translation).
  • Native Gemini/v1beta/models/{model}:{action} for the Gemini API (passthrough, no translation).

xAI and Mistral are natively OpenAI-compatible — requests pass through directly via /v1/chat/completions with zero translation.

Attribution headers

Add these optional headers to track costs per workflow and step in your dashboard:

HeaderPurposeExample
x-workflow-tagIdentifies the workflowlead-enrichment
x-stepIdentifies the step within a workflowsummarize
x-execution-idGroups requests from the same runexec-12345
x-sourceIdentifies the tool or platformmake
x-projectAssociates with a projectclient-a

Response format

TokenSense returns the exact same response as the upstream provider. Your code doesn't need any changes to parse the response — it's identical to calling the provider directly.

Code examples

cURL

curl https://api.tokensense.io/chat/completions \
  -H "Authorization: Bearer YOUR_TOKENSENSE_KEY" \
  -H "Content-Type: application/json" \
  -H "x-workflow-tag: my-workflow" \
  -d '{
    "model": "gpt-5-mini",
    "messages": [
      {"role": "user", "content": "Hello, world!"}
    ]
  }'

Python

import requests

response = requests.post(
    "https://api.tokensense.io/chat/completions",
    headers={
        "Authorization": "Bearer YOUR_TOKENSENSE_KEY",
        "Content-Type": "application/json",
        "x-workflow-tag": "my-workflow",
    },
    json={
        "model": "gpt-5-mini",
        "messages": [
            {"role": "user", "content": "Hello, world!"}
        ],
    },
)

print(response.json())

Node.js

const response = await fetch(
  "https://api.tokensense.io/chat/completions",
  {
    method: "POST",
    headers: {
      "Authorization": "Bearer YOUR_TOKENSENSE_KEY",
      "Content-Type": "application/json",
      "x-workflow-tag": "my-workflow",
    },
    body: JSON.stringify({
      model: "gpt-5-mini",
      messages: [
        { role: "user", content: "Hello, world!" },
      ],
    }),
  }
);

const data = await response.json();
console.log(data);

Error responses

CodeMeaningFix
400Invalid request body or missing fieldsCheck your request payload format
401Invalid or missing API keyCheck key in dashboard
402Budget exceeded or subscription lockedIncrease budget or update billing
403Missing provider key or routing policy blockAdd provider key in Settings → Providers
404Model not found in catalogCheck model name, use /v1/models to list
429Rate limit exceeded (tiered by plan)Check Retry-After header, slow down
502Upstream provider errorRetry with backoff
504Provider timeoutTry a faster model

See the full Error Codes reference for detailed troubleshooting.

Related