页面简介
An overview of OpenRouter's API
正文摘录
OpenRouter’s request and response schemas are very similar to OpenAI’s APIs, with a few small differences. At a high level, OpenRouter normalizes the schema across models and providers so you only need to learn one. OpenRouter supports two request shapes: the Responses API (POST /api/v1/responses) and the Chat Completions API (POST /api/v1/chat/completions). Where this page shows both, the Responses API comes first. The TypeScript schemas below describe Chat Completions; the Responses API schema is in the Responses API reference. OpenAPI specification The complete OpenRouter API is documented using the OpenAPI specification. You can access the specification in either YAML or JSON format: YAML: https://openrouter.ai/openapi.yaml JSON: https://openrouter.ai/openapi.json These specifications can be used with tools like Swagger UI, Postman, or any OpenAPI-compatible code generator to explore the API or generate client libraries. Requests Responses API request format A Responses API request takes input (a string or an array of input items), optional instructions, and the same routing fields as Chat Completions (models, provider, plugins, user). The full request schema is in the Responses API reference and the OpenAPI specification. { "model": "openai/gpt-5.2", "instructions": "Answer in one sentence.", "input": "What is the meaning of life?", "max_output_tokens": 256 } Completions request format Here is the Chat Completions request schema as a TypeScript type. This will be the body of your POST request to the /api/v1/chat/completions endpoint. The Responses API request format is above. For a complete list of parameters, see the Parameters. Request Schema// Definitions of subtypes are below type Request = { // Either "messages" or "prompt" is required messages?: Message[]; prompt?: string; // If "model" is unspecified, uses the user's default model?: string; // See "Supported Models" section // Allows to force the model to produce specific output format. // See "Structured Outputs" section below and models page for which models support it. response_format?: ResponseFormat; stop?: string | string[]; stream?: boolean; // Enable streaming // Plugins to extend model capabilities (PDF parsing, response healing) // See "Plugins" section: openrouter.ai/docs/guides/features/plugins plugins?: Plugin[]; // See LLM Parameters (openrouter.ai/docs/api_reference/parameters) max_tokens?: number; // Range: [1, context_length) temperature?: number; // Range: [0, 2] // Tool calling // Will be passed down as-is for providers implementing OpenAI's interface. // For providers with custom interfaces, we transform and map the properties. // Otherwise, we transform the tools into a YAML template. The model responds with an assistant message. // See models supporting tool calling: openrouter.ai/models?supported_parameters=tools tools?: Tool[]; tool_choice?: ToolChoice; // Advanced optional parameters seed?: number; // Integer only top_p?: number; // Range: (0, 1] top_k?: number; // Range: [1, Infinity) Not available for OpenAI models frequency_penalty?: number; // Range: [-2, 2] presence_penalty?: number; // Range: [-2, 2] repetition_penalty?: number; // Range: (0, 2] logit_bias?: { [key: number]: number }; top_logprobs: number; // Integer only min_p?: number; // Range: [0, 1] top_a?: number; // Range: [0, 1] // Reduce latency by providing the model with a predicted output // https://platform.openai.com/docs/guides/latency-optimization#use-predicted-outputs prediction?: { type: 'content'; content: string }; // OpenRouter-only parameters // See "Model Routing" section: openrouter.ai/docs/guides/features/model-routing models?: string[]; route?: 'fallback'; // See "Provider Routing" section: openrouter.ai/docs/guides/routing/provider-selection provider?: ProviderPreferences; user?: string; // A stable identifier for your end-users, included in the upstream identity on endpoints that require one. // Debug options (streaming only) debug?: { echo_upstream_body?: boolean; // If true, returns the transformed request body sent to the provider }; }; // Subtypes: type TextContent = { type: 'text'; text: string; }; type ImageContentPart = { type: 'image_url'; image_url: { url: string; // URL or base64 encoded image data detail?: string; // Optional, defaults to "auto" }; }; type ContentPart = TextContent | ImageContentPart; type Message = | { role: 'user' | 'assistant' | 'system'; // ContentParts are only for the "user" role: content: string | ContentPart[]; // If "name" is included, it will be prepended like this // for non-OpenAI models: `{name}: {content}` name?: string; } | { role: 'tool'; content: string; tool_call_id: string; name?: string; }; type FunctionDescription = { description?: string; name: string; parameters: object; // JSON Schema object }; type Tool = { type: 'function'; function: FunctionDescription; }; type ToolChoice = | 'none' | 'auto' | { type: 'function'; function: { name: string; }; }; // Response format for structured outputs type ResponseFormat = | { type: 'json_object' } | { type: 'json_schema'; json_schema: { name: string; strict?: boolean; schema: object; // JSON Schema object }; }; // Plugin configuration type Plugin = { id: string; // 'web', 'file-parser', 'response-healing', 'context-compression' enabled?: boolean; // Additional plugin-specific options [key: string]: unknown; }; See all 135 lines Structured outputs The response_format parameter allows you to enforce structured JSON responses from the model. OpenRouter supports two modes: { type: 'json_object' }: Basic JSON mode - the model will return valid JSON { type: 'json_schema', json_schema: { ... } }: Strict schema mode - the model will return JSON matching your exact schema For detailed usage and examples, see Structured Outputs. To find models that support structured outputs, check the models page. Plugins OpenRouter plugins extend model capabilities with features like web search, PDF processing, response healing, and context compression. Enable plugins by adding a plugins array to your request: { "plugins": [ { "id": "web" }, { "id": "response-healing" } ] } Available plugins include web (real-time web search), file-parser (PDF processing), response-healing (automatic JSON repair), and context-compression (middle-out prompt compression). For detailed configuration options, see Plugins Headers OpenRouter allows you to specify some optional headers to identify your app and make it discoverable to users on our site. HTTP-Referer: Identifies your app on openrouter.ai X-OpenRouter-Title: Sets/modifies your app’s title (X-Title also accepted) X-OpenRouter-Categories: Assigns marketplace categories (see App Attribution) Responses APIChat CompletionsPythonGocURLfetch('https://openrouter.ai/api/v1/responses', { method: 'POST', headers: { Authorization: 'Bearer <OPENROUTER_API_KEY>', 'HTTP-Referer': '<YOUR_SITE_URL>', // Optional. Site URL for rankings on openrouter.ai. 'X-OpenRouter-Title': '<YOUR_SITE_NAME>', // Optional. Site title for rankings on openrouter.ai. 'Content-Type': 'application/json', }, body: JSON.stringify({ model: 'openai/gpt-5.2', input: 'What is the meaning of life?', }), }); OutputStatus: 200 { "id": "gen-1794947561-lErZowkClrPNycYNs17c", "object": "response", "created_at": 1794947561, "model": "openai/gpt-5.2", "status": "completed", "completed_at": 1794947570, "output": [ { "id": "rs_04c0a1456b3e4c66016afcb9f1f9f887d1be35c915ee85f388", "type": "reasoning", "status": "completed", "summary": [ { "type": "summary_text", "text": "**Crafting meaningful reflections**\n\nI'm thinking about how to approach the topic with a concise and meaningful perspective. It’s important to keep it philosophical, without getting too verbose. I want to present multiple viewpoints and highlight ideas like constructing meaning, well-being, relationships, personal growth, contribution, and curiosity. I also need to emphasize that there’s no single answer and provide actionable reflection prompts for deeper thinking. This way, I can guide the user in exploring the subject more thoughtfully." } ], "encrypted_content": "gAAAAABq_LnxW5uxuX_otngGFsSO5p_FFv06S3YJXOWsaacGZuNGVtn-N4_Z_16z_ahi4VBHLNC7MLGdRkokwsCIeWYlv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