TokenLayer API
One key, every model. TokenLayer exposes an OpenAI-compatible and an Anthropic-compatible API over the same base URL — point your existing SDK at it and keep your code unchanged.
Checking your session…
Introduction
TokenLayer is a unified inference gateway. You authenticate every request with a single TokenLayer API key and can reach models from OpenAI, Anthropic, Google, DeepSeek, Meta, and more — all through one endpoint, with one balance and one usage dashboard.
The API speaks two dialects, so whichever ecosystem your code already lives in keeps working:
https://api.tokenlayer.net/v1Chat Completions, model listing, and streaming — a drop-in replacement for api.openai.com/v1.
https://api.tokenlayer.net/v1/messagesThe Messages API — a drop-in replacement for api.anthropic.com/v1/messages.
Each model is served on the API dialects it declares — most are OpenAI-only, Claude models speak both. The model picker below and GET /v1/models (the supported_endpoint_types field) show which dialects every model supports, and our code samples only ever pair a model with an endpoint it actually serves.
Quickstart
- 1Create an account and open the API Keys page in your dashboard.
- 2Click Generate key. The full key (
sk-…) is shown once — store it somewhere safe. - 3Send your first request:
curl https://api.tokenlayer.net/v1/chat/completions \ -H "Authorization: Bearer YOUR_TOKENLAYER_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-5.4", "messages": [{ "role": "user", "content": "Say hello!" }] }'
That's it. The response is identical in shape to what the upstream provider would return, so existing parsers, SDKs, and frameworks work unchanged.
Authentication
Every request must carry your TokenLayer API key. Two header styles are accepted on all endpoints:
Authorization: Bearer sk-your-keyx-api-key: sk-your-keyKeys are created and revoked from the dashboard. Revocation takes effect immediately. All keys on your account draw from the same credit balance.
Keep keys server-side. Never embed an API key in client-side code, mobile apps, or public repositories. If a key leaks, revoke it and generate a new one.
OpenAI-compatible API
A faithful implementation of the OpenAI API. Use the official openai SDK in any language — just change the base URL and the key.
Chat completions
https://api.tokenlayer.net/v1/chat/completionsSend a list of messages, get a model-generated reply. Supports multi-turn conversations, system prompts, tool calling, structured outputs, and streaming.
curl https://api.tokenlayer.net/v1/chat/completions \ -H "Authorization: Bearer YOUR_TOKENLAYER_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-5.4", "messages": [ { "role": "system", "content": "You are a concise assistant." }, { "role": "user", "content": "Explain HTTP caching in two sentences." } ], "max_tokens": 200, "temperature": 0.7 }'
modelstringrequiredID of the model to use, e.g. gpt-5.4 or claude-opus-4-6. Call GET /v1/models for the exact IDs your key can access.
messagesarrayrequiredThe conversation so far. Each item is { "role": "system" | "user" | "assistant", "content": "..." }.
streambooleanoptionalWhen true, tokens are sent as server-sent events as they're generated. Defaults to false.
max_tokensintegeroptionalUpper bound on the number of tokens to generate.
temperaturenumberoptionalSampling temperature, 0–2. Lower is more deterministic.
top_pnumberoptionalNucleus sampling. Use this or temperature, not both.
toolsarrayoptionalTool/function definitions for tool calling, in the OpenAI tools format.
response_formatobjectoptionalSet { "type": "json_object" } for JSON-mode output (model support varies).
A successful response:
{ "id": "chatcmpl-abc123", "object": "chat.completion", "created": 1718000000, "model": "gpt-5.4", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "HTTP caching stores copies of responses..." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 28, "completion_tokens": 52, "total_tokens": 80 } }
Streaming
Pass "stream": true and the response becomes a server-sent-event stream of deltas, terminated by data: [DONE] — the same wire format as OpenAI, so every SDK's streaming helper works.
curl https://api.tokenlayer.net/v1/chat/completions \ -H "Authorization: Bearer YOUR_TOKENLAYER_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-5.4", "messages": [{ "role": "user", "content": "Write a haiku." }], "stream": true }' # data: {"choices":[{"delta":{"content":"Tok"}}], ...} # data: {"choices":[{"delta":{"content":"ens"}}], ...} # data: [DONE]
List models
https://api.tokenlayer.net/v1/modelsReturns every model your key can reach, in the standard OpenAI list format. Use the returned id values as the model parameter.
curl https://api.tokenlayer.net/v1/models -H "Authorization: Bearer YOUR_TOKENLAYER_KEY"
Anthropic-compatible API
If your code is built on the Anthropic SDK or the Messages API format, point it at TokenLayer instead — authentication, request shape, and streaming all match.
Messages
https://api.tokenlayer.net/v1/messagescurl https://api.tokenlayer.net/v1/messages \ -H "x-api-key: YOUR_TOKENLAYER_KEY" \ -H "anthropic-version: 2023-06-01" \ -H "Content-Type: application/json" \ -d '{ "model": "claude-opus-4-6", "max_tokens": 1024, "system": "You are a concise assistant.", "messages": [ { "role": "user", "content": "Explain HTTP caching in two sentences." } ] }'
modelstringrequiredAny model ID on the platform — Claude models natively, and other vendors' models are translated for you.
max_tokensintegerrequiredMaximum tokens to generate. Required by the Messages format.
messagesarrayrequiredAlternating user / assistant turns: { "role": "user", "content": "..." }.
systemstringoptionalSystem prompt — a top-level field here, not a message role.
streambooleanoptionalWhen true, responds with Anthropic-format SSE events (message_start, content_block_delta, …).
temperaturenumberoptionalSampling temperature, 0–1.
A successful response:
{ "id": "msg_abc123", "type": "message", "role": "assistant", "model": "claude-opus-4-6", "content": [ { "type": "text", "text": "HTTP caching stores copies of responses..." } ], "stop_reason": "end_turn", "usage": { "input_tokens": 31, "output_tokens": 47 } }
Using official SDKs
There's no TokenLayer-specific SDK to install — use the official OpenAI or Anthropic libraries and override two settings:
https://api.tokenlayer.net/v1sk-… (TokenLayer)https://api.tokenlayer.netsk-… (TokenLayer)| SDK | BASE URL | API KEY |
|---|---|---|
| openai (Python / JS) | https://api.tokenlayer.net/v1 | sk-… (TokenLayer) |
| anthropic (Python / JS) | https://api.tokenlayer.net | sk-… (TokenLayer) |
The Anthropic SDKs append /v1/messages to their base URL themselves, which is why their base URL stops at /api.
from openai import OpenAI client = OpenAI( base_url="https://api.tokenlayer.net/v1", api_key="YOUR_TOKENLAYER_KEY", )
Coding tools
AI coding tools are the most common way to use TokenLayer. Each of these speaks one of our two API dialects, so setup is just pointing the tool at our base URL with your key.
Claude Code
Claude Code talks the Anthropic Messages API, which TokenLayer serves natively. Two environment variables redirect it — set them in your Claude Code settings file so they apply to every session:
{ "env": { "ANTHROPIC_BASE_URL": "https://api.tokenlayer.net", "ANTHROPIC_AUTH_TOKEN": "YOUR_TOKENLAYER_KEY", "ANTHROPIC_MODEL": "claude-opus-4-6" } }
Or set the same values as environment variables for a one-off session:
export ANTHROPIC_BASE_URL="https://api.tokenlayer.net" export ANTHROPIC_AUTH_TOKEN="YOUR_TOKENLAYER_KEY" export ANTHROPIC_MODEL="claude-opus-4-6" claude
Restart Claude Code after the change. ANTHROPIC_MODEL pins the default model (you can still switch with /model in-session); shell environment variables take precedence over the env block if both are set. Only models tagged Anthropic in the picker above work here.
Use your user-level settings file (~/.claude/settings.json), not a project's .claude/settings.json — project settings are meant to be committed and shared, and your key would leak with them.
OpenAI Codex CLI
Codex uses the OpenAI API format. Add TokenLayer as a model provider in its config file:
# Use TokenLayer as the model provider model = "gpt-5.4" model_provider = "tokenlayer" [model_providers.tokenlayer] name = "TokenLayer" base_url = "https://api.tokenlayer.net/v1" env_key = "TOKENLAYER_API_KEY" wire_api = "responses"
Codex never stores keys in config.toml — env_key names the environment variable to read at runtime (sent as a Bearer token), so export it before launching:
export TOKENLAYER_API_KEY="YOUR_TOKENLAYER_KEY" codex
wire_api = "responses" tells Codex to use the OpenAI Responses protocol — the only value current Codex CLI accepts (it dropped "chat" in Feb 2026), and what TokenLayer serves. Switch models per-session with codex -m <model-id> or permanently via the model line.
Cursor
Cursor supports custom OpenAI-compatible endpoints through its settings UI — no config file needed:
- 1Open Cursor Settings → Models.
- 2Under API Keys, enable OpenAI API Key and paste your TokenLayer key.
- 3Expand the override option and set Override OpenAI Base URL to
https://api.tokenlayer.net/v1. - 4Click Verify, then add the model IDs you want (e.g. from the model list above) as custom models.
Note: with a custom base URL, Cursor routes chat through your endpoint, but Cursor-native features (Tab autocomplete, some agent modes) may still require a Cursor subscription.
Errors
Errors use conventional HTTP status codes with a JSON body: { "error": { "message": "...", "type": "..." } }.
| STATUS | MEANING |
|---|---|
| 401 | Missing or invalid API key, or the key has been revoked. |
| 402 | Insufficient credits — your balance is exhausted. Top up from the dashboard. type is insufficient_quota. |
| 403 | The requested model isn't available on TokenLayer right now. |
| 404 | Unknown endpoint path. |
| 429 | Rate limited — slow down and retry with exponential backoff. |
| 5xx | Upstream or gateway error. Safe to retry idempotent requests. |
{ "error": { "message": "Insufficient credits. Top up to continue.", "type": "insufficient_quota" } }
Credits & limits
TokenLayer is prepaid. Usage is metered per request against your account's credit balance at the per-model rates shown on the pricing page. All of your API keys share one balance; per-key spend is broken out on the API Keys page so you can see which key is consuming what.
- When the balance reaches zero, requests return
402until you top up — nothing is queued or silently dropped. - Live usage, token counts, and spend-per-model are on the Usage page.
- Top up from the Topup page; credits never expire.
Questions, missing models, or higher limits? Reach us from the Support page or the Telegram channel — we usually respond within a few hours.