### OpenAI API Call Example Source: https://swiftrouter.com/docs This snippet demonstrates a standard API call to OpenAI using their SDK. It requires an OpenAI API key and specifies the model and messages for the completion. ```python import openai client = openai.Client( api_key="sk-proj-..." ) response = client.chat.completions.create( model="gpt-5", messages=[{"role": "user", "content": "Hello"}] ) ``` -------------------------------- ### Authentication Example Source: https://swiftrouter.com/docs/api-reference/introduction Authenticate your requests by including your secret API key in the `Authorization` HTTP header as a Bearer token. ```APIDOC ## Authentication Authenticate your requests by including your secret API key in the `Authorization` HTTP header as a Bearer token. ```bash export SWIFTROUTER_API_KEY="sk-..." curl -X GET "https://api.swiftrouter.com/v1/models" \ -H "Authorization: Bearer $SWIFTROUTER_API_KEY" ``` Keep your keys safe Your API keys carry many privileges, so be sure to keep them secure! Do not share your secret API keys in publicly accessible areas such as GitHub, client-side code, and so forth. ``` -------------------------------- ### GET /v1/models Source: https://swiftrouter.com/docs/api-reference/models Lists the currently available models, and provides basic information about each one such as the owner and availability. ```APIDOC ## GET /v1/models ### Description Lists the currently available models, and provides basic information about each one such as the owner and availability. ### Method GET ### Endpoint https://api.swiftrouter.com/v1/models ### Response #### Success Response (200) - **object** (string) - The type of the object, which is 'list'. - **data** (array) - An array of model objects. #### Response Example { "object": "list", "data": [ { "id": "gpt-5", "object": "model", "created": 1715367049, "owned_by": "openai" }, { "id": "claude-sonnet-4.6", "object": "model", "created": 1718844000, "owned_by": "anthropic" } ] } ``` -------------------------------- ### Chat Completion Response Example Source: https://swiftrouter.com/docs/api-reference/chat-completions This is an example of a successful chat completion response object from the SwiftRouter API. It includes metadata and the assistant's message. ```JSON { "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "model": "gpt-5", "system_fingerprint": "fp_44709d6fcb", "choices": [{ "index": 0, "message": { "role": "assistant", "content": "Hello there, how may I assist you today?", }, "finish_reason": "stop" }], "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 } } ``` -------------------------------- ### Get Account Daily Quotas Source: https://swiftrouter.com/docs/api-reference/introduction This JSON object represents the response from the GET /v1/account/daily-quotas endpoint. It provides details on daily token limits, usage, and remaining tokens for different services. ```json { "plan_id": "pro", "quotas": [ { "group": "claude", "label": "Anthropic", "daily_token_limit": 10000000, "used_tokens_today": 1250000, "remaining_tokens_today": 8750000, "reset_at": "2026-03-13T00:00:00Z", "is_unlimited": false }, { "group": "gpt", "label": "OpenAI", "daily_token_limit": 20000000, "used_tokens_today": 2100000, "remaining_tokens_today": 17900000, "reset_at": "2026-03-13T00:00:00Z", "is_unlimited": false }, { "group": "gemini", "label": "Google", "daily_token_limit": 20000000, "used_tokens_today": 350000, "remaining_tokens_today": 19650000, "reset_at": "2026-03-13T00:00:00Z", "is_unlimited": false }, { "group": "other", "label": "Other", "daily_token_limit": 0, "used_tokens_today": 480000, "remaining_tokens_today": 0, "reset_at": "2026-03-13T00:00:00Z", "is_unlimited": true } ] } ``` -------------------------------- ### GET /v1/account/daily-quotas Source: https://swiftrouter.com/docs/api-reference/introduction Use this JWT-only account endpoint to fetch exact live quota state for `claude`, `gpt`, `gemini`, and `other`. It remains the compatibility route for signed-in account flows rather than API-key inference traffic. ```APIDOC ## GET /v1/account/daily-quotas Use this JWT-only account endpoint to fetch exact live quota state for `claude`, `gpt`, `gemini`, and `other`. It remains the compatibility route for signed-in account flows rather than API-key inference traffic. Authenticate this route with a current dashboard JWT. The dashboard UI now prefers `/dashboard/daily-quota`, while `GET /v1/account/daily-quotas` remains the exact live compatibility route. API keys are accepted on public inference endpoints such as `/v1/chat/completions`, not on this JWT account endpoint. ### Response Example ```json { "plan_id": "pro", "quotas": [ { "group": "claude", "label": "Anthropic", "daily_token_limit": 10000000, "used_tokens_today": 1250000, "remaining_tokens_today": 8750000, "reset_at": "2026-03-13T00:00:00Z", "is_unlimited": false }, { "group": "gpt", "label": "OpenAI", "daily_token_limit": 20000000, "used_tokens_today": 2100000, "remaining_tokens_today": 17900000, "reset_at": "2026-03-13T00:00:00Z", "is_unlimited": false }, { "group": "gemini", "label": "Google", "daily_token_limit": 20000000, "used_tokens_today": 350000, "remaining_tokens_today": 19650000, "reset_at": "2026-03-13T00:00:00Z", "is_unlimited": false }, { "group": "other", "label": "Other", "daily_token_limit": 0, "used_tokens_today": 480000, "remaining_tokens_today": 0, "reset_at": "2026-03-13T00:00:00Z", "is_unlimited": true } ] } ``` ``` -------------------------------- ### Validate Configuration and Verify Integration Source: https://swiftrouter.com/docs/agents/opencode Commands to verify the JSON structure, check the model list via API, and test the OpenCode request execution. ```bash cat opencode.json | jq . ``` ```bash curl -X GET "https://api.swiftrouter.com/v1/models" \ -H "Authorization: Bearer $SWIFTROUTER_API_KEY" ``` ```bash opencode run --model gpt-5 "Summarize this repo" ``` -------------------------------- ### Verify Connectivity Source: https://swiftrouter.com/docs/dev-docs/quick-start Test the API key by fetching the list of available models. ```bash curl -X GET "https://api.swiftrouter.com/v1/models" \ -H "Authorization: Bearer $SWIFTROUTER_API_KEY" ``` -------------------------------- ### Configure Agent Profile in JSON Source: https://swiftrouter.com/docs/agents/opencode Define the provider options and model selection in the OpenCode configuration file. ```json { "provider": { "openai": { "options": { "baseURL": "https://api.swiftrouter.com/v1", "apiKey": "${env:OPENAI_API_KEY}" } } }, "model": "claude-sonnet-4.6" } ``` -------------------------------- ### Verify Environment Variable Source: https://swiftrouter.com/docs/agents/claude Check that the base URL environment variable is correctly set. ```bash echo $ANTHROPIC_BASE_URL ``` -------------------------------- ### Configure Agent Profile for SwiftRouter Source: https://swiftrouter.com/docs/agents/codex Set up the connection parameters in the config.toml file. This specifies SwiftRouter as the model provider and configures the base URL and API key environment variable. ```toml model_provider = "swiftrouter" model = "gpt-5" [model_providers.swiftrouter] name = "OpenAI-compatible" base_url = "https://api.swiftrouter.com/v1" env_key = "OPENAI_API_KEY" ``` -------------------------------- ### Advanced Codex Configuration with Tool Features Source: https://swiftrouter.com/docs/agents/codex Configure the agent profile to include tool features like web search, file browser, and terminal access, in addition to specifying the SwiftRouter model provider. ```toml model_provider = "swiftrouter" model = "gpt-5" [model_providers.swiftrouter] name = "OpenAI-compatible" base_url = "https://api.swiftrouter.com/v1" env_key = "OPENAI_API_KEY" [tools] web_search = true file_browser = true terminal = true ``` -------------------------------- ### Make First Chat Completion Request Source: https://swiftrouter.com/docs/dev-docs/quick-start Send a chat completion request to the generation endpoint. ```bash curl -X POST "https://api.swiftrouter.com/v1/chat/completions" \ -H "Authorization: Bearer $SWIFTROUTER_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-5", "messages": [{"role":"user","content":"Hello from SwiftRouter"}] }' ``` -------------------------------- ### List Available Models Source: https://swiftrouter.com/docs/home/models Use this command to retrieve the full list of models available through the SwiftRouter API. This is useful for dynamically populating UI elements or for programmatic model selection. ```bash curl https://api.swiftrouter.com/v1/models \ -H "Authorization: Bearer sk-..." ``` -------------------------------- ### Configure Agent Profile Source: https://swiftrouter.com/docs/agents/claude Define the connection parameters and default model in the agent's JSON configuration file. ```json { "env": { "ANTHROPIC_BASE_URL": "https://api.swiftrouter.com", "ANTHROPIC_API_KEY": "${env:SWIFTROUTER_API_KEY}" }, "model": "claude-sonnet-4.6" } ``` -------------------------------- ### Configure Advanced Kilocode Settings Source: https://swiftrouter.com/docs/agents/kilocode Set advanced model parameters like temperature and max tokens, and enable features like inline chat and code completion. ```json { "kilocode.defaultModel": "claude-sonnet-4.6", "kilocode.modelSettings": { "temperature": 0.3, "maxTokens": 1200 }, "kilocode.enableInlineChat": true, "kilocode.enableCodeActions": true, "kilocode.enableAutoComplete": true } ``` -------------------------------- ### Verify Claude Run Source: https://swiftrouter.com/docs/agents/claude Execute a test command to ensure the Claude agent is functioning correctly. ```bash claude --model claude-sonnet-4.6 "Summarize this repo" ``` -------------------------------- ### Create Chat Completion Source: https://swiftrouter.com/docs/api-reference/introduction Creates a completion for the provided prompt and parameters. ```APIDOC ## POST /v1/chat/completions ### Description Creates a completion for the provided prompt and parameters. ### Method POST ### Endpoint /v1/chat/completions ### Request Body - **model** (string) - Required - The model to use for completion. - **messages** (array) - Required - The messages to send to the model. - **role** (string) - Required - The role of the author of the message (e.g., `system`, `user`, or `assistant`). - **content** (string) - Required - The content of the message. - **temperature** (number) - Optional - Controls randomness. Lower values make the output more deterministic. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate. ### Response #### Success Response (200) - **choices** (array) - A list of completion choices. - **message** (object) - The message content. - **role** (string) - The role of the author of the message. - **content** (string) - The content of the message. ``` -------------------------------- ### Advanced Configuration Profiles Source: https://swiftrouter.com/docs/agents/claude Define multiple execution profiles with specific models and settings for different use cases. ```json { "env": { "ANTHROPIC_BASE_URL": "https://api.swiftrouter.com", "ANTHROPIC_API_KEY": "${env:SWIFTROUTER_API_KEY}" }, "model": "claude-opus-4.6", "profiles": { "fast": { "model": "claude-sonnet-4.6" }, "deep": { "model": "claude-opus-4.6", "temperature": 0.2 } } } ``` -------------------------------- ### Export Environment Variables Source: https://swiftrouter.com/docs/agents/claude Set the required environment variables to map the SwiftRouter API key and base URL for the agent. ```bash export SWIFTROUTER_API_KEY="sk-<64_hex_chars>" export ANTHROPIC_API_KEY="$SWIFTROUTER_API_KEY" export ANTHROPIC_BASE_URL="https://api.swiftrouter.com" ``` -------------------------------- ### Configure Agent Profile with SwiftRouter Source: https://swiftrouter.com/docs/agents/roocode Set up the connection parameters for RooCode to use SwiftRouter. This JSON configuration specifies the provider, base URL, API key (read from environment variable), and the desired model. ```json { "provider": "openai-compatible", "api": { "baseURL": "https://api.swiftrouter.com/v1", "apiKey": "${env:OPENAI_API_KEY}", "model": "gpt-5" } } ``` -------------------------------- ### Configure Cline Agent Profile with SwiftRouter Source: https://swiftrouter.com/docs/agents/cline Set up the connection parameters in VS Code settings to use SwiftRouter as the OpenAI-compatible API provider. This configuration can be applied at the user or workspace level. ```json { "cline.apiProvider": "openai", "cline.openai": { "apiKey": "${env:OPENAI_API_KEY}", "baseURL": "https://api.swiftrouter.com/v1" }, "cline.defaultModel": "gpt-5" } ``` -------------------------------- ### Export SwiftRouter Environment Variables Source: https://swiftrouter.com/docs/agents/overview Configure shell environment variables to map SwiftRouter credentials to standard provider endpoints. ```bash export SWIFTROUTER_API_KEY="sk-..." # Map to standard endpoints export OPENAI_API_KEY="$SWIFTROUTER_API_KEY" export OPENAI_BASE_URL="https://api.swiftrouter.com/v1" ``` -------------------------------- ### Export API Key Source: https://swiftrouter.com/docs/dev-docs/quick-start Set the API key as an environment variable. Ensure this is only stored in server-side environments. ```bash export SWIFTROUTER_API_KEY="sk-<64_hex_chars>" export OPENAI_API_KEY="$SWIFTROUTER_API_KEY" ``` -------------------------------- ### Run Codex with SwiftRouter Model Source: https://swiftrouter.com/docs/agents/codex Execute a Codex command using the configured SwiftRouter model (e.g., gpt-5) to generate a response. This verifies the end-to-end integration. ```bash codex --model gpt-5 "Create a test plan" ``` -------------------------------- ### Export Environment Variables for SwiftRouter Source: https://swiftrouter.com/docs/agents/roocode Map SwiftRouter's API key to the standard provider key expected by the agent and set the base URL for the OpenAI-compatible endpoint. ```bash export SWIFTROUTER_API_KEY="sk-<64_hex_chars>" export OPENAI_API_KEY="$SWIFTROUTER_API_KEY" export OPENAI_BASE_URL="https://api.swiftrouter.com/v1" ``` -------------------------------- ### Define Advanced Configuration Profiles Source: https://swiftrouter.com/docs/agents/opencode Configure specific model profiles and enable tool features within the OpenCode configuration. ```json { "provider": { "openai": { "options": { "baseURL": "https://api.swiftrouter.com/v1", "apiKey": "${env:OPENAI_API_KEY}" }, "models": { "fast": { "id": "gpt-5" }, "analysis": { "id": "claude-opus-4.6" } } } }, "tools": { "bash": true, "edit": true, "read": true, "write": true } } ``` -------------------------------- ### Advanced Agent Profile with Features Source: https://swiftrouter.com/docs/agents/roocode Configure an advanced agent profile that includes specific features like chat interface, code review, and multi-file editing, along with model parameters like temperature. ```json { "api": { "baseURL": "https://api.swiftrouter.com/v1", "apiKey": "${env:OPENAI_API_KEY}", "model": "claude-opus-4.6", "temperature": 0.4 }, "features": { "chatInterface": true, "codeReview": true, "multiFileEdit": true } } ``` -------------------------------- ### SwiftRouter Integration with OpenAI SDK Source: https://swiftrouter.com/docs This snippet shows how to integrate SwiftRouter with the OpenAI SDK by changing the base URL and API key. It allows seamless switching to other models like Claude by updating the 'model' parameter. ```python import openai client = openai.Client( base_url="https://api.swiftrouter.com/v1", api_key="sk-..." ) # Switch to Claude seamlessly response = client.chat.completions.create( model="claude-sonnet-4.6", messages=[{"role": "user", "content": "Hello"}] ) ``` -------------------------------- ### Configure Kilocode Agent Profile for SwiftRouter Source: https://swiftrouter.com/docs/agents/kilocode Set up the connection parameters in your editor's settings JSON to use SwiftRouter as a custom provider. ```json { "kilocode.apiProvider": "custom", "kilocode.customProvider": { "name": "SwiftRouter", "baseURL": "https://api.swiftrouter.com/v1", "apiKey": "${env:OPENAI_API_KEY}" }, "kilocode.defaultModel": "gpt-5" } ``` -------------------------------- ### Advanced Cline Configuration with SwiftRouter Source: https://swiftrouter.com/docs/agents/cline Configure advanced features such as terminal and Git integration, auto-context, and task execution policies like requiring approval per file and setting maximum iterations. This JSON snippet overrides default settings. ```json { "cline.defaultModel": "claude-sonnet-4.6", "cline.features": { "terminalIntegration": true, "gitIntegration": true, "autoContext": true }, "cline.taskMode": { "requireApproval": "per-file", "maxIterations": 8 } } ``` -------------------------------- ### Handle 429 Rate Limit Exceeded (RPM) Source: https://swiftrouter.com/docs/dev-docs/error-handling For 429 responses with 'plan_rpm_exceeded', use the 'Retry-After' header or 'retry_after_seconds' field for backoff. This indicates a user-level rate limit. ```http HTTP/1.1 429 Too Many Requests Retry-After: 60 { "error": "Rate limit exceeded", "code": "plan_rpm_exceeded", "remaining": 0, "reset_at": "2026-02-19T12:00:00Z", "retry_after_seconds": 60, "request_id": "...", "details": { "remaining": 0, "reset_at": "2026-02-19T12:00:00Z" } } ``` -------------------------------- ### List Models Response Format Source: https://swiftrouter.com/docs/api-reference/models The expected JSON response structure containing an array of model objects. ```JSON { "object": "list", "data": [ { "id": "gpt-5", "object": "model", "created": 1715367049, "owned_by": "openai" }, { "id": "claude-sonnet-4.6", "object": "model", "created": 1718844000, "owned_by": "anthropic" } ] } ``` -------------------------------- ### Validate Codex Configuration Source: https://swiftrouter.com/docs/agents/codex Use the Codex CLI to validate the configuration file. This command checks for TOML parse errors. ```bash codex config validate ``` -------------------------------- ### Set SwiftRouter API Key Source: https://swiftrouter.com/docs/api-reference/introduction Set your secret API key as an environment variable for authentication. This key is used in the Authorization header. ```shell export SWIFTROUTER_API_KEY="sk-..." ``` -------------------------------- ### Create Chat Completion Request Source: https://swiftrouter.com/docs/api-reference/chat-completions Use this cURL command to send a chat completion request to the SwiftRouter API. Ensure your API key is set as an environment variable. ```cURL curl https://api.swiftrouter.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $SWIFTROUTER_API_KEY" \ -d '{ \ "model": "gpt-5", \ "messages": [ \ { \ "role": "system", \ "content": "You are a helpful assistant." \ }, \ { \ "role": "user", \ "content": "Hello!" \ } \ ] \ }' ``` -------------------------------- ### POST /v1/chat/completions Source: https://swiftrouter.com/docs/api-reference/chat-completions Creates a model response for the given chat conversation. Returns a chat completion object, or a streamed sequence of chat completion chunk objects if the request is streamed. ```APIDOC ## POST /v1/chat/completions ### Description Creates a model response for the given chat conversation. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Request Body - **model** (string) - Required - ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API. - **messages** (array) - Required - A list of messages comprising the conversation so far. - **stream** (boolean) - Optional - If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events. - **temperature** (number) - Optional - What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. - **max_tokens** (integer) - Optional - The maximum number of tokens that can be generated in the chat completion. ### Request Example ```json { "model": "gpt-5", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ] } ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the completion. - **object** (string) - Type of object returned, e.g., 'chat.completion'. - **created** (integer) - Unix timestamp of when the completion was created. - **model** (string) - The model used for the completion. - **system_fingerprint** (string) - System-generated fingerprint for the model. - **choices** (array) - A list of completion choices. - **index** (integer) - Index of the choice. - **message** (object) - The message content. - **role** (string) - Role of the message sender (e.g., 'assistant'). - **content** (string) - The text content of the message. - **finish_reason** (string) - The reason the model stopped generating tokens. - **usage** (object) - Usage statistics for the completion. - **prompt_tokens** (integer) - Number of tokens in the prompt. - **completion_tokens** (integer) - Number of tokens in the completion. - **total_tokens** (integer) - Total tokens used. #### Response Example ```json { "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "model": "gpt-5", "system_fingerprint": "fp_44709d6fcb", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Hello there, how may I assist you today?", }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 } } ``` ``` -------------------------------- ### Handle 429 Daily Quota Exceeded Source: https://swiftrouter.com/docs/dev-docs/error-handling For 429 responses with 'plan_daily_quota_exceeded', use the 'Retry-After' header or 'retry_after_seconds' field for backoff. This indicates a group-level daily token quota. ```http HTTP/1.1 429 Too Many Requests Retry-After: 900 { "error": "Google daily token cap reached for the current UTC day", "code": "plan_daily_quota_exceeded", "quota_group": "gemini", "daily_token_limit": 2000000, "remaining_tokens": 0, "reset_at": "2026-03-13T00:00:00Z", "retry_after_seconds": 900, "request_id": "...", "details": { "remaining": 0, "reset_at": "2026-03-13T00:00:00Z", "quota_group": "gemini", "daily_token_limit": 2000000, "remaining_tokens": 0 } } ``` -------------------------------- ### SwiftRouter Error Envelope JSON Source: https://swiftrouter.com/docs/dev-docs/error-handling All error responses from SwiftRouter follow this structured JSON envelope. Inspect the 'code' field to determine the specific error. ```json { "error": "Rate limit exceeded", "code": "plan_rpm_exceeded", "request_id": "d9d4...", "details": { "throttle_scope": "user" } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.