Skip to content

Authentication

TokenRouter uses API keys for authentication. All API requests must include a valid API key in the Authorization header as a Bearer token.

  1. Sign up or log in at tokenrouter.io
  2. Navigate to Console → API Keys
  3. Click Create API Key
  4. Give your key a descriptive name (e.g., “Production”, “Development”, “Staging”)
  5. Copy the API key immediately - it will only be shown once

Include your API key in the Authorization header of every request:

import Tokenrouter from 'tokenrouter';
const client = new Tokenrouter({
apiKey: process.env.TOKENROUTER_API_KEY, // tr_...
});
const response = await client.responses.create({
model: 'auto:balance',
input: 'Hello, world!'
});

TokenRouter API keys follow this format:

  • Prefix: tr_
  • Example: tr_1234567890abcdef1234567890abcdef

Store your API key in environment variables to keep it secure:

Terminal window
# .env file
TOKENROUTER_API_KEY=tr_1234567890abcdef1234567890abcdef

Load with a package like dotenv:

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

API keys inherit your workspace settings including:

  • Quota Limits: Monthly token limits and rate limits
  • Firewall Rules: Content filtering and security policies
  • Routing Rules: Custom routing logic and preferences
  • Provider Access: Which providers you have configured

Create new API keys and delete old ones periodically (e.g., every 90 days).

Create separate keys for development, staging, and production:

tr_dev_... # Development
tr_staging_... # Staging
tr_prod_... # Production
  • Never log API keys
  • Never expose keys in client-side code
  • Use server-side proxy for browser applications
  • Store keys in secure secret managers (AWS Secrets Manager, HashiCorp Vault, etc.)

Check the Logs view regularly to:

  • Detect unusual usage patterns
  • Identify potentially compromised keys
  • Track which keys are being used

If an API key is compromised:

  1. Go to Console → API Keys
  2. Find the compromised key
  3. Click Delete or Revoke
  4. Create a new key
  5. Update your application with the new key

Revoked keys will immediately stop working for all requests.

All authenticated requests automatically include:

  • Request ID: Unique identifier for each request
  • Idempotency Key: Prevents duplicate requests
  • Latency Telemetry: Response time tracking
  • User Agent: SDK version and platform information

You can view this metadata in the Console → Logs view.

TokenRouter enforces TLS 1.2 or higher for all connections.

If authentication fails, you’ll receive one of these error responses:

{
"error": {
"type": "authentication_error",
"message": "No API key provided. Include your API key in the Authorization header.",
"http_status": 401
}
}
{
"error": {
"type": "authentication_error",
"message": "Invalid API key provided",
"http_status": 401
}
}
{
"error": {
"type": "authentication_error",
"message": "API key has been revoked",
"http_status": 401
}
}

After setting up authentication:

  1. Add Provider Keys - Configure your AI provider credentials
  2. Quickstart Guide - Make your first request
  3. API Reference - Explore all available endpoints