SDKs

Drop-in snippets for every SDK we support

Tokenwise isn't an SDK — it's a baseURL change. Paste the snippet, set TW_LIVE_KEY, ship. We add < 50 ms of edge latency.

Before you start

  • Sign in and create a workspace at /onboarding. You’ll get a tw_live_* key — store it as TW_LIVE_KEY.
  • Your existing provider API keys (OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.) keep working — Tokenwise forwards them upstream.
  • Optional: encrypt provider keys at rest by adding them in Settings → API Keys instead of env vars.

Vercel AI SDK

ts
import { generateText } from "ai";
import { createOpenAI } from "@ai-sdk/openai";

const openai = createOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: "https://proxy.tokenwisehq.com/openai/v1",
  headers: { "x-tw-key": process.env.TW_LIVE_KEY! },
});

const { text } = await generateText({
  model: openai("gpt-4o-mini"),
  prompt: "Say hello",
});

OpenAI (JavaScript)

ts
import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: "https://proxy.tokenwisehq.com/openai/v1",
  defaultHeaders: { "x-tw-key": process.env.TW_LIVE_KEY },
});

await openai.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "Say hello" }],
});

OpenAI (Python)

py
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["OPENAI_API_KEY"],
    base_url="https://proxy.tokenwisehq.com/openai/v1",
    default_headers={"x-tw-key": os.environ["TW_LIVE_KEY"]},
)

client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Say hello"}],
)

Anthropic (JavaScript)

ts
import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
  baseURL: "https://proxy.tokenwisehq.com/anthropic",
  defaultHeaders: { "x-tw-key": process.env.TW_LIVE_KEY },
});

await anthropic.messages.create({
  model: "claude-sonnet-4-6",
  max_tokens: 256,
  messages: [{ role: "user", content: "Say hello" }],
});

Anthropic (Python)

py
from anthropic import Anthropic

client = Anthropic(
    api_key=os.environ["ANTHROPIC_API_KEY"],
    base_url="https://proxy.tokenwisehq.com/anthropic",
    default_headers={"x-tw-key": os.environ["TW_LIVE_KEY"]},
)

client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=256,
    messages=[{"role": "user", "content": "Say hello"}],
)

Google Gemini (OpenAI-compatible)

ts
import OpenAI from "openai";

const gemini = new OpenAI({
  apiKey: process.env.GOOGLE_API_KEY,
  baseURL: "https://proxy.tokenwisehq.com/google",
  defaultHeaders: { "x-tw-key": process.env.TW_LIVE_KEY },
});

await gemini.chat.completions.create({
  model: "gemini-2.0-flash",
  messages: [{ role: "user", content: "Say hello" }],
});

xAI Grok

ts
import OpenAI from "openai";

const xai = new OpenAI({
  apiKey: process.env.XAI_API_KEY,
  baseURL: "https://proxy.tokenwisehq.com/xai/v1",
  defaultHeaders: { "x-tw-key": process.env.TW_LIVE_KEY },
});

await xai.chat.completions.create({
  model: "grok-2",
  messages: [{ role: "user", content: "Say hello" }],
});

Groq

ts
import OpenAI from "openai";

const groq = new OpenAI({
  apiKey: process.env.GROQ_API_KEY,
  baseURL: "https://proxy.tokenwisehq.com/groq/openai/v1",
  defaultHeaders: { "x-tw-key": process.env.TW_LIVE_KEY },
});

await groq.chat.completions.create({
  model: "llama-3.3-70b-versatile",
  messages: [{ role: "user", content: "Say hello" }],
});

DeepSeek

ts
import OpenAI from "openai";

const deepseek = new OpenAI({
  apiKey: process.env.DEEPSEEK_API_KEY,
  baseURL: "https://proxy.tokenwisehq.com/deepseek/v1",
  defaultHeaders: { "x-tw-key": process.env.TW_LIVE_KEY },
});

await deepseek.chat.completions.create({
  model: "deepseek-chat",
  messages: [{ role: "user", content: "Say hello" }],
});

Mistral

ts
import OpenAI from "openai";

const mistral = new OpenAI({
  apiKey: process.env.MISTRAL_API_KEY,
  baseURL: "https://proxy.tokenwisehq.com/mistral/v1",
  defaultHeaders: { "x-tw-key": process.env.TW_LIVE_KEY },
});

await mistral.chat.completions.create({
  model: "mistral-large-latest",
  messages: [{ role: "user", content: "Say hello" }],
});

OpenRouter

ts
import OpenAI from "openai";

const openrouter = new OpenAI({
  apiKey: process.env.OPENROUTER_API_KEY,
  baseURL: "https://proxy.tokenwisehq.com/openrouter/api/v1",
  defaultHeaders: { "x-tw-key": process.env.TW_LIVE_KEY },
});

await openrouter.chat.completions.create({
  model: "anthropic/claude-sonnet-4.6",
  messages: [{ role: "user", content: "Say hello" }],
});

cURL

bash
curl https://proxy.tokenwisehq.com/openai/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "x-tw-key: $TW_LIVE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role":"user","content":"Say hello"}]
  }'

Don’t see your SDK?

As long as your SDK accepts a custom baseURLand lets you set headers, Tokenwise works. If yours doesn’t, email [email protected]— we’ll either help you wire it up or add a preset.