OpenAI SDK

Using OpenRouter with OpenAI SDK

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: "<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})
11
12async 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 })
19
20 console.log(completion.choices[0].message)
21}
22main();