Quickstart

Get started with OpenRouter

In the examples below, the OpenRouter-specific headers are optional. Setting them allows your app to appear on the OpenRouter leaderboards.

Using the OpenAI SDK

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-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 {
17 role: 'user',
18 content: 'What is the meaning of life?',
19 },
20 ],
21 });
22
23 console.log(completion.choices[0].message);
24}
25
26main();

Using the OpenRouter API directly

1fetch('https://openrouter.ai/api/v1/chat/completions', {
2 method: 'POST',
3 headers: {
4 Authorization: 'Bearer <OPENROUTER_API_KEY>',
5 'HTTP-Referer': '<YOUR_SITE_URL>', // Optional. Site URL for rankings on openrouter.ai.
6 'X-Title': '<YOUR_SITE_NAME>', // Optional. Site title for rankings on openrouter.ai.
7 'Content-Type': 'application/json',
8 },
9 body: JSON.stringify({
10 model: 'openai/gpt-4o',
11 messages: [
12 {
13 role: 'user',
14 content: 'What is the meaning of life?',
15 },
16 ],
17 }),
18});

Using third-party SDKs

For information about using third-party SDKs and frameworks with OpenRouter, please see our frameworks documentation.

Built with