Skip to content

TokenRouter Quickstart Guide - Get Started in 5 Minutes

This quickstart guide will help you make your first TokenRouter API request. We’ll use the TypeScript SDK, but you can also use Python or cURL.

  • A TokenRouter account (sign up here)
  • Node.js 18+ or Python 3.8+ installed
  • At least one AI provider API key (OpenAI, Anthropic, Google, etc.)
  1. Log in to tokenrouter.io
  2. Navigate to Console → API Keys
  3. Click Create API Key
  4. Name it “Quickstart” and copy the key

You need at least one AI provider key to route requests. Let’s add an OpenAI key:

  1. Go to Console → Providers
  2. Click Add ProviderOpenAI
  3. Paste your OpenAI API key (get one from platform.openai.com)
  4. Click Save
Terminal window
npm install tokenrouter

Create a file named index.ts:

import Tokenrouter from 'tokenrouter';
const client = new Tokenrouter({
apiKey: 'tr_your_api_key_here', // Replace with your actual key
});
async function main() {
const response = await client.responses.create({
model: 'auto:balance',
input: 'What is the capital of France?'
});
console.log(response.output[0].content[0].text);
console.log('\nUsage:', response.usage);
console.log('Provider:', response.metadata?.provider);
}
main();

Run it:

Terminal window
npx tsx index.ts
# or with ts-node:
npx ts-node index.ts
{
"id": "resp_...",
"object": "response",
"created_at": 1704067200,
"status": "completed",
"model": "gpt-4o-2024-11-20",
"output": [
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The capital of France is Paris."
}
]
}
],
"usage": {
"input_tokens": 12,
"output_tokens": 8,
"total_tokens": 20
},
"metadata": {
"provider": "openai",
"routing_mode": "balance",
"routing_confidence": 0.95
}
}

Now that you have a working setup, try different routing modes:

const response = await client.responses.create({
model: 'auto:cost',
input: 'Summarize quantum computing in one sentence'
});

Routes to the most cost-effective provider for this request.

Get real-time responses with Server-Sent Events:

const stream = await client.responses.create({
model: 'auto:balance',
input: 'Write a short poem about coding',
stream: true
});
for await (const chunk of stream) {
if (chunk.event === 'content.delta') {
process.stdout.write(chunk.delta.text);
}
}
const response = await client.responses.create({
model: 'auto:balance',
input: userMessage,
instructions: 'You are a helpful assistant',
stream: true
});
const response = await client.responses.create({
model: 'claude-3-7-sonnet-latest:quality',
input: 'Write a Python function to calculate Fibonacci numbers',
temperature: 0.3
});
const response = await client.responses.create({
model: 'gpt-4o:balance',
input: 'Analyze this dataset and provide insights',
max_output_tokens: 2000
});
const response = await client.responses.create({
model: 'auto:cost',
input: `Summarize this article: ${articleText}`,
max_output_tokens: 200
});

For security, always use environment variables for API keys:

.env
TOKENROUTER_API_KEY=tr_your_api_key_here
import 'dotenv/config';
import Tokenrouter from 'tokenrouter';
const client = new Tokenrouter({
apiKey: process.env.TOKENROUTER_API_KEY
});

Instead of configuring provider keys in the console, you can pass them directly in requests using custom headers:

const response = await client.responses.create({
model: 'gpt-4o',
input: 'Hello!',
}, {
headers: {
'X-OpenAI-Key': process.env.OPENAI_API_KEY
}
});

Supported headers:

  • X-OpenAI-Key - OpenAI API key
  • X-Anthropic-Key - Anthropic API key
  • X-Gemini-Key - Google Gemini API key
  • X-Mistral-Key - Mistral API key
  • X-DeepSeek-Key - DeepSeek API key

Congratulations! You’ve made your first TokenRouter request. Here’s what to explore next:

Make sure you’re passing the Authorization header with your TokenRouter API key.

You need to add at least one provider key in the Console → Providers.

Double-check your API key and make sure it’s active in Console → API Keys.

Ensure you’re making requests over HTTPS to https://api.tokenrouter.io/v1/responses.

Need help? Contact support@tokenrouter.io.