Documentation

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.

Try it with your account

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:

OPENAI-COMPATIBLE
https://api.tokenlayer.net/v1

Chat Completions, model listing, and streaming — a drop-in replacement for api.openai.com/v1.

ANTHROPIC-COMPATIBLE
https://api.tokenlayer.net/v1/messages

The 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

  1. 1Create an account and open the API Keys page in your dashboard.
  2. 2Click Generate key. The full key (sk-…) is shown once — store it somewhere safe.
  3. 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:

OPENAI STYLE (BEARER)
Authorization: Bearer sk-your-key
ANTHROPIC STYLE (X-API-KEY)
x-api-key: sk-your-key

Keys 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

POSThttps://api.tokenlayer.net/v1/chat/completions

Send 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
  }'
modelstringrequired

ID 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.

messagesarrayrequired

The conversation so far. Each item is { "role": "system" | "user" | "assistant", "content": "..." }.

streambooleanoptional

When true, tokens are sent as server-sent events as they're generated. Defaults to false.

max_tokensintegeroptional

Upper bound on the number of tokens to generate.

temperaturenumberoptional

Sampling temperature, 0–2. Lower is more deterministic.

top_pnumberoptional

Nucleus sampling. Use this or temperature, not both.

toolsarrayoptional

Tool/function definitions for tool calling, in the OpenAI tools format.

response_formatobjectoptional

Set { "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

GEThttps://api.tokenlayer.net/v1/models

Returns 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

POSThttps://api.tokenlayer.net/v1/messages
curl 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." }
    ]
  }'
modelstringrequired

Any model ID on the platform — Claude models natively, and other vendors' models are translated for you.

max_tokensintegerrequired

Maximum tokens to generate. Required by the Messages format.

messagesarrayrequired

Alternating user / assistant turns: { "role": "user", "content": "..." }.

systemstringoptional

System prompt — a top-level field here, not a message role.

streambooleanoptional

When true, responds with Anthropic-format SSE events (message_start, content_block_delta, …).

temperaturenumberoptional

Sampling 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:

openai (Python / JS)
Base URL
https://api.tokenlayer.net/v1
API key
sk-… (TokenLayer)
anthropic (Python / JS)
Base URL
https://api.tokenlayer.net
API key
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:

Windows: %USERPROFILE%\.claude\settings.json
macOS / Linux: ~/.claude/settings.json
{
  "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:

Windows: %USERPROFILE%\.codex\config.toml
macOS / Linux: ~/.codex/config.toml
# 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:

  1. 1Open Cursor Settings → Models.
  2. 2Under API Keys, enable OpenAI API Key and paste your TokenLayer key.
  3. 3Expand the override option and set Override OpenAI Base URL to https://api.tokenlayer.net/v1.
  4. 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": "..." } }.

STATUSMEANING
401Missing or invalid API key, or the key has been revoked.
402Insufficient credits — your balance is exhausted. Top up from the dashboard. type is insufficient_quota.
403The requested model isn't available on TokenLayer right now.
404Unknown endpoint path.
429Rate limited — slow down and retry with exponential backoff.
5xxUpstream 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 402 until 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.