For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
ModelsChatRankingsDocs
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
  • Overview
    • Quickstart
    • Principles
    • Models
    • Stripe Projects
    • FAQ
    • Report Feedback
  • Models & Routing
    • Model Fallbacks
    • Provider Selection
    • Auto Exacto
    • Private Models
  • Features
    • Workspaces
    • Presets
    • Response Caching
    • Tool Calling
    • Structured Outputs
    • Message Transforms
    • Zero Completion Insurance
    • ZDR
    • App Attribution
    • Service Tiers
    • Sovereign AI
    • Router Metadata
    • Input & Output Logging
      • For Providers
      • Frameworks and Integrations Overview
      • Awesome OpenRouter
      • Effect AI SDK
      • Arize
      • LangChain
      • LiveKit
      • Langfuse
      • Mastra
      • OpenAI SDK
      • Anthropic Agent SDK
      • PydanticAI
      • Replit
      • TanStack AI
      • Vercel AI SDK
      • Xcode
      • Zapier
      • Infisical
LogoLogo
ModelsChatRankingsDocs
On this page
  • Using the OpenAI SDK
Community

OpenAI SDK

Using OpenRouter with OpenAI SDK
Was this page helpful?
Previous

Anthropic Agent SDK

Using OpenRouter with the Anthropic Agent SDK
Next
Built with

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();