OpenRouter provides an OpenAI-compatible completion API to 0 models & providers that you can call directly, or using the OpenAI SDK. Additionally, some third-party SDKs are available.
import OpenAI from "openai"
const openai = new OpenAI({
baseURL: "https://openrouter.ai/api/v1",
apiKey: $OPENROUTER_API_KEY,
defaultHeaders: {
"HTTP-Referer": $YOUR_SITE_URL, // Optional, for including your app on openrouter.ai rankings.
"X-Title": $YOUR_APP_NAME, // Optional. Shows in rankings on openrouter.ai.
}
})
async function main() {
const completion = await openai.chat.completions.create({
model: "meta-llama/llama-3.1-405b",
messages: [
{
"role": "user",
"content": "What is the meaning of life?"
}
]
})
console.log(completion.choices[0].message)
}
main()
fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${OPENROUTER_API_KEY}`,
"HTTP-Referer": `${YOUR_SITE_URL}`, // Optional, for including your app on openrouter.ai rankings.
"X-Title": `${YOUR_SITE_NAME}`, // Optional. Shows in rankings on openrouter.ai.
"Content-Type": "application/json"
},
body: JSON.stringify({
"model": "meta-llama/llama-3.1-405b",
"messages": [
{
"role": "user",
"content": "What is the meaning of life?"
}
]
})
});
For information about using third-party SDKs and frameworks with OpenRouter, please see our frameworks documentation.