Skip to main content

API Key Authentication

The YouRouter API uses API keys for authentication. You can generate and manage your API keys from the YouRouter Dashboard.
Keep your API key secure and never expose it in client-side code or public repositories.

Getting Your API Key

  1. Sign up for an account at YouRouter Platform
  2. Navigate to the Dashboard and go to API Keys
  3. Generate a new API key
  4. Copy and store your API key securely

Making Authenticated Requests

Include your API key in the Authorization header of your requests:
curl https://api.yourouter.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Using Environment Variables

For security, set your API key as an environment variable:
  • macOS/Linux
  • Windows
  • Python (.env)
export YOUROUTER_API_KEY="your-api-key-here"

SDK Configuration

Configure the OpenAI SDKs with your API key and the YouRouter base URL:
from openai import OpenAI

client = OpenAI(
    api_key="your-api-key-here",
    base_url="https://api.yourouter.ai/v1"
)

Security Best Practices

  • Use environment variables or secure credential management systems
  • Never commit API keys to version control
  • Rotate your API keys regularly
  • Use different API keys for different environments (development, staging, production)
  • Monitor your API usage to avoid hitting rate limits
  • Implement exponential backoff for retries
  • Cache responses when possible to reduce API calls
  • Set up monitoring and alerting for unusual API usage patterns
  • Track costs and set spending limits in the YouRouter Dashboard
  • Monitor for potential security breaches or unauthorized access

Error Handling

Handle authentication errors gracefully in your applications:
from openai import OpenAI, AuthenticationError

try:
    client = OpenAI(
        api_key="your-api-key-here",
        base_url="https://api.yourouter.ai/v1"
    )
    
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
except AuthenticationError as e:
    print(f"Authentication failed: {e}")

Rate Limits and Usage

API rate limits and pricing information can be found in your YouRouter Dashboard. Monitor your usage to avoid hitting limits.
Common HTTP status codes for authentication:
Status CodeDescription
200Success
401Invalid API key
429Rate limit exceeded
500Server error
I