OpenAI SDK


title: OpenAI SDK subtitle: Using OpenRouter with OpenAI SDK headline: ‘OpenAI SDK Integration | OpenRouter SDK Support’ canonical-url: https://openrouter.ai/docs/guides/community/openai-sdk og:site_name: OpenRouter Documentation og:title: ‘OpenAI SDK Integration - OpenRouter SDK Support’ og:description: ‘Integrate OpenRouter using the official OpenAI SDK. Complete guide for OpenAI SDK integration with OpenRouter for Python and TypeScript.’ og:image: https://openrouter.ai/dynamic-og?title=OpenAI%20SDK&description=OpenAI%20SDK%20Integration og:image:width: 1200 og:image:height: 630 twitter:card: summary_large_image twitter:site: ‘@OpenRouterAI’ noindex: false nofollow: false

Using 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.

1import OpenAI from "openai"
2
3const openai = new OpenAI({
4 baseURL: "https://openrouter.ai/api/v1",
5 apiKey: "${API_KEY_REF}",
6 defaultHeaders: {
7 ${getHeaderLines().join('\n ')}
8 },
9})
10
11async function main() {
12 const completion = await openai.chat.completions.create({
13 model: "${Model.GPT_4_Omni}",
14 messages: [
15 { role: "user", content: "Say this is a test" }
16 ],
17 })
18
19 console.log(completion.choices[0].message)
20}
21main();