Authentication
Overview
Section titled “Overview”TokenRouter uses API keys for authentication. All API requests must include a valid API key in the Authorization header as a Bearer token.
Creating an API Key
Section titled “Creating an API Key”- Sign up or log in at tokenrouter.io
- Navigate to Console → API Keys
- Click Create API Key
- Give your key a descriptive name (e.g., “Production”, “Development”, “Staging”)
- Copy the API key immediately - it will only be shown once
Using API Keys
Section titled “Using API Keys”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!'});import osfrom tokenrouter import Tokenrouter
client = Tokenrouter( api_key=os.environ.get("TOKENROUTER_API_KEY"), # tr_...)
response = client.responses.create( model="auto:balance", input="Hello, world!")curl https://api.tokenrouter.io/v1/responses \ -H "Content-Type: application/json" \ -H "Authorization: Bearer tr_your_api_key_here" \ -d '{ "model": "auto:balance", "input": "Hello, world!" }'API Key Format
Section titled “API Key Format”TokenRouter API keys follow this format:
- Prefix:
tr_ - Example:
tr_1234567890abcdef1234567890abcdef
Environment Variables
Section titled “Environment Variables”Store your API key in environment variables to keep it secure:
# .env fileTOKENROUTER_API_KEY=tr_1234567890abcdef1234567890abcdefLoad with a package like dotenv:
import 'dotenv/config';import Tokenrouter from 'tokenrouter';
const client = new Tokenrouter({ apiKey: process.env.TOKENROUTER_API_KEY});# .env fileTOKENROUTER_API_KEY=tr_1234567890abcdef1234567890abcdefLoad with python-dotenv:
from dotenv import load_dotenvimport osfrom tokenrouter import Tokenrouter
load_dotenv()
client = Tokenrouter( api_key=os.getenv("TOKENROUTER_API_KEY"))# Export in your shellexport TOKENROUTER_API_KEY=tr_1234567890abcdef1234567890abcdef
# Or add to .bashrc / .zshrcecho 'export TOKENROUTER_API_KEY=tr_...' >> ~/.bashrcKey Permissions and Scope
Section titled “Key Permissions and Scope”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
Security Best Practices
Section titled “Security Best Practices”1. Rotate Keys Regularly
Section titled “1. Rotate Keys Regularly”Create new API keys and delete old ones periodically (e.g., every 90 days).
2. Use Environment-Specific Keys
Section titled “2. Use Environment-Specific Keys”Create separate keys for development, staging, and production:
tr_dev_... # Developmenttr_staging_... # Stagingtr_prod_... # Production3. Limit Key Exposure
Section titled “3. Limit Key Exposure”- 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.)
4. Monitor Usage
Section titled “4. Monitor Usage”Check the Logs view regularly to:
- Detect unusual usage patterns
- Identify potentially compromised keys
- Track which keys are being used
Revoking API Keys
Section titled “Revoking API Keys”If an API key is compromised:
- Go to Console → API Keys
- Find the compromised key
- Click Delete or Revoke
- Create a new key
- Update your application with the new key
Revoked keys will immediately stop working for all requests.
Request Metadata
Section titled “Request Metadata”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.
HTTPS Required
Section titled “HTTPS Required”TokenRouter enforces TLS 1.2 or higher for all connections.
Error Responses
Section titled “Error Responses”If authentication fails, you’ll receive one of these error responses:
Missing API Key
Section titled “Missing API Key”{ "error": { "type": "authentication_error", "message": "No API key provided. Include your API key in the Authorization header.", "http_status": 401 }}Invalid API Key
Section titled “Invalid API Key”{ "error": { "type": "authentication_error", "message": "Invalid API key provided", "http_status": 401 }}Revoked API Key
Section titled “Revoked API Key”{ "error": { "type": "authentication_error", "message": "API key has been revoked", "http_status": 401 }}Next Steps
Section titled “Next Steps”After setting up authentication:
- Add Provider Keys - Configure your AI provider credentials
- Quickstart Guide - Make your first request
- API Reference - Explore all available endpoints