### Install OpenRouter SDK (npm, yarn, pnpm) Source: https://openrouter.ai/docs/quickstart/index Installs the OpenRouter SDK package for use in your project. Supports npm, yarn, and pnpm package managers. ```bash npm install @openrouter/sdk ``` ```bash yarn add @openrouter/sdk ``` ```bash pnpm add @openrouter/sdk ``` -------------------------------- ### Python OpenAI SDK with OpenRouter Source: https://openrouter.ai/docs/quickstart/index This snippet demonstrates how to configure the OpenAI Python SDK to use OpenRouter as the API endpoint. It covers initializing the client with the base URL and API key, and includes optional headers for site ranking. The example then performs a chat completion. ```python from openai import OpenAI client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key="", ) completion = client.chat.completions.create( extra_headers={ "HTTP-Referer": "", # Optional. Site URL for rankings on openrouter.ai. "X-Title": "", # Optional. Site title for rankings on openrouter.ai. }, model="openai/gpt-5.2", messages=[ { "role": "user", "content": "What is the meaning of life?" } ] ) print(completion.choices[0].message.content) ``` -------------------------------- ### TypeScript OpenAI SDK with OpenRouter Source: https://openrouter.ai/docs/quickstart/index This snippet shows how to initialize the OpenAI TypeScript SDK to communicate with the OpenRouter API. It includes setting the base URL, API key, and optional headers for site ranking. The example makes a chat completion request to a specified model. ```typescript import OpenAI from 'openai'; const openai = new OpenAI({ baseURL: 'https://openrouter.ai/api/v1', apiKey: '', defaultHeaders: { 'HTTP-Referer': '', // Optional. Site URL for rankings on openrouter.ai. 'X-Title': '', // Optional. Site title for rankings on openrouter.ai. }, }); async function main() { const completion = await openai.chat.completions.create({ model: 'openai/gpt-5.2', messages: [ { role: 'user', content: 'What is the meaning of life?', }, ], }); console.log(completion.choices[0].message); } main(); ``` -------------------------------- ### Make Direct API Calls for Chat Completions (Python, TypeScript, Shell) Source: https://openrouter.ai/docs/quickstart/index Provides examples of making direct POST requests to the OpenRouter API for chat completions using Python's requests library, TypeScript's fetch API, and curl (shell). Requires an API key and optionally accepts site URL and name for leaderboards. ```python import requests import json response = requests.post( url="https://openrouter.ai/api/v1/chat/completions", headers={ "Authorization": "Bearer ", "HTTP-Referer": "", # Optional. Site URL for rankings on openrouter.ai. "X-Title": "", # Optional. Site title for rankings on openrouter.ai. }, data=json.dumps({ "model": "openai/gpt-5.2", # Optional "messages": [ { "role": "user", "content": "What is the meaning of life?" } ] }) ) ``` ```typescript fetch('https://openrouter.ai/api/v1/chat/completions', { method: 'POST', headers: { Authorization: 'Bearer ', 'HTTP-Referer': '', // Optional. Site URL for rankings on openrouter.ai. 'X-Title': '', // Optional. Site title for rankings on openrouter.ai. 'Content-Type': 'application/json', }, body: JSON.stringify({ model: 'openai/gpt-5.2', messages: [ { role: 'user', content: 'What is the meaning of life?', }, ], }), }); ``` ```shell curl https://openrouter.ai/api/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENROUTER_API_KEY" \ -d '{ "model": "openai/gpt-5.2", "messages": [ { "role": "user", "content": "What is the meaning of life?" } ] }' ``` -------------------------------- ### OpenAI SDK Integration (Python) Source: https://openrouter.ai/docs/quickstart/index This snippet demonstrates how to initialize and use the OpenAI SDK with OpenRouter's API in Python. It covers setting the base URL, API key, and optional headers for site ranking. ```APIDOC ## POST /v1/chat/completions ### Description This endpoint allows you to generate chat completions using various AI models available through OpenRouter. It supports streaming responses. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Query Parameters None #### Request Body - **model** (string) - Required - The ID of the model to use for generation. - **messages** (list) - Required - A list of messages comprising the conversation so far. - **role** (string) - Required - The role of the author of this message ('user', 'assistant', or 'system'). - **content** (string) - Required - The content of the message. - **stream** (boolean) - Optional - Whether to stream back partial message deltas as they are generated. ### Request Example ```python { "model": "openai/gpt-5.2", "messages": [ { "role": "user", "content": "What is the meaning of life?" } ] } ``` ### Response #### Success Response (200) - **id** (string) - The ID of the completion. - **choices** (list) - A list of completion choices. - **index** (int) - The index of the choice. - **message** (object) - The message content and role. - **role** (string) - The role of the author. - **content** (string) - The content of the message. - **created** (int) - Unix timestamp of when the completion was created. - **model** (string) - The model used for the completion. - **usage** (object) - Usage statistics for the completion. - **prompt_tokens** (int) - Number of tokens in the prompt. - **completion_tokens** (int) - Number of tokens in the completion. - **total_tokens** (int) - Total tokens used. #### Response Example ```json { "id": "chatcmpl-123", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "The meaning of life is a philosophical question that has been debated for centuries." } } ], "created": 1677652288, "model": "openai/gpt-5.2", "usage": { "prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30 } } ``` ``` -------------------------------- ### Use OpenRouter SDK for Chat Completions (TypeScript) Source: https://openrouter.ai/docs/quickstart/index Demonstrates how to use the OpenRouter SDK in TypeScript to send a chat completion request. Requires an API key and optionally accepts site URL and name for leaderboards. ```typescript import { OpenRouter } from '@openrouter/sdk'; const openRouter = new OpenRouter({ apiKey: '', defaultHeaders: { 'HTTP-Referer': '', // Optional. Site URL for rankings on openrouter.ai. 'X-Title': '', // Optional. Site title for rankings on openrouter.ai. }, }); const completion = await openRouter.chat.send({ model: 'openai/gpt-5.2', messages: [ { role: 'user', content: 'What is the meaning of life?', }, ], stream: false, }); console.log(completion.choices[0].message.content); ``` -------------------------------- ### Chat Completions API Source: https://openrouter.ai/docs/quickstart/index This endpoint allows you to send messages to an AI model and receive a text completion. It supports various models and configurations, including streaming responses. ```APIDOC ## POST /api/v1/chat/completions ### Description Sends messages to an AI model to receive a text completion. Supports various models and configurations. ### Method POST ### Endpoint https://openrouter.ai/api/v1/chat/completions ### Parameters #### Header Parameters - **Authorization** (string) - Required - Bearer token for authentication. - **HTTP-Referer** (string) - Optional - Site URL for rankings on openrouter.ai. - **X-Title** (string) - Optional - Site title for rankings on openrouter.ai. - **Content-Type** (string) - Required - application/json #### Request Body - **model** (string) - Optional - The AI model to use (e.g., "openai/gpt-5.2"). - **messages** (array) - Required - An array of message objects, each with a `role` (user, system, assistant) and `content`. - **stream** (boolean) - Optional - If set to true, the response will be streamed. ### Request Example ```json { "model": "openai/gpt-5.2", "messages": [ { "role": "user", "content": "What is the meaning of life?" } ] } ``` ### Response #### Success Response (200) - **choices** (array) - An array of completion choices. Each choice contains: - **message** (object) - The message from the model, with `role` and `content`. #### Response Example ```json { "choices": [ { "message": { "role": "assistant", "content": "The meaning of life is a philosophical question that has been debated for centuries..." } } ] } ``` ``` -------------------------------- ### OpenAI SDK Integration (TypeScript) Source: https://openrouter.ai/docs/quickstart/index This snippet shows how to initialize and use the OpenAI SDK with OpenRouter's API in TypeScript. It includes setting the base URL, API key, and optional headers for site ranking. ```APIDOC ## POST /v1/chat/completions ### Description This endpoint allows you to generate chat completions using various AI models available through OpenRouter. It supports streaming responses. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Query Parameters None #### Request Body - **model** (string) - Required - The ID of the model to use for generation. - **messages** (array) - Required - A list of messages comprising the conversation so far. - **role** (string) - Required - The role of the author of this message ('user', 'assistant', or 'system'). - **content** (string) - Required - The content of the message. - **stream** (boolean) - Optional - Whether to stream back partial message deltas as they are generated. ### Request Example ```json { "model": "openai/gpt-5.2", "messages": [ { "role": "user", "content": "What is the meaning of life?" } ] } ``` ### Response #### Success Response (200) - **id** (string) - The ID of the completion. - **choices** (array) - A list of completion choices. - **index** (integer) - The index of the choice. - **message** (object) - The message content and role. - **role** (string) - The role of the author. - **content** (string) - The content of the message. - **created** (integer) - Unix timestamp of when the completion was created. - **model** (string) - The model used for the completion. - **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", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "The meaning of life is a philosophical question that has been debated for centuries." } } ], "created": 1677652288, "model": "openai/gpt-5.2", "usage": { "prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30 } } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.