CommunityOpenAI SDKCopy pageUsing OpenRouter with OpenAI SDKUsing the OpenAI SDK Using pip install openai: github. Using npm i openai: github. You can also use Grit to automatically migrate your code. Simply run npx @getgrit/launcher openrouter. TypeScriptPython1import OpenAI from "openai"23const openai = new OpenAI({4 baseURL: "https://openrouter.ai/api/v1",5 apiKey: "<OPENROUTER_API_KEY>",6 defaultHeaders: {7 "HTTP-Referer": "<YOUR_SITE_URL>", // Optional. Site URL for rankings on openrouter.ai.8 "X-OpenRouter-Title": "<YOUR_SITE_NAME>", // Optional. Site title for rankings on openrouter.ai.9 },10})1112async function main() {13 const completion = await openai.chat.completions.create({14 model: "openai/gpt-4o",15 messages: [16 { role: "user", content: "Say this is a test" }17 ],18 })1920 console.log(completion.choices[0].message)21}22main();