OpenRouter Load Balancer API

This service provides an API for using OpenRouter's model load balancing capabilities with automatic fallback to free models.

GET /free/models

Fetch all available models and free models from OpenRouter.

Example Response:

{
  "all_models": [...],
  "free_models": [...],
  "free_models_count": 5
}

POST /chat/completions

Create a chat completion with automatic fallback to free models.

Example Request:

{
  "model": "openai/gpt-4o",
  "messages": [
    {
      "role": "user",
      "content": "What is the meaning of life?"
    }
  ]
}

The API will automatically add free models as fallbacks.

Streaming Support

This API supports streaming responses for real-time content generation. To use streaming, add stream: true to your request:

Example Streaming Request:

{
  "model": "openai/gpt-3.5-turbo",
  "messages": [
    {
      "role": "user",
      "content": "Tell me a short story"
    }
  ],
  "stream": true
}

When streaming is enabled, the API returns a text/event-stream response with chunks of the completion as they are generated.

Streaming Response Format:

// Each line is a separate event
{"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"role":"assistant"},"index":0}]}
{"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"content":"Once"},"index":0}]}
{"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"content":" upon"},"index":0}]}
// ... more chunks ...
{"id":"...","object":"chat.completion.chunk","choices":[{"delta":{},"finish_reason":"stop","index":0}]}

Cross-Origin Support

This API supports Cross-Origin Resource Sharing (CORS), allowing it to be called from any domain. The following CORS headers are included in all responses:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE, PATCH
Access-Control-Allow-Headers: Content-Type, Authorization, Accept, X-Requested-With, Origin
Access-Control-Expose-Headers: Content-Type, X-Request-ID
Access-Control-Max-Age: 86400