Quickstart

Get started with OpenRouter

OpenRouter provides a unified API that gives you access to hundreds of AI models through a single endpoint, while automatically handling fallbacks and selecting the most cost-effective options. Get started with just a few lines of code using your preferred SDK or framework.

Looking for information about free models and rate limits? Please see the FAQ

In the examples below, the OpenRouter-specific headers are optional. Setting them allows your app to appear on the OpenRouter leaderboards. For detailed information about app attribution, see our App Attribution guide.

Using the OpenRouter SDK (Beta)

TypeScript SDK
1import { OpenRouter } from '@openrouter/sdk';
2
3const openRouter = new OpenRouter({
4 apiKey: '<OPENROUTER_API_KEY>',
5 defaultHeaders: {
6 'HTTP-Referer': '<YOUR_SITE_URL>', // Optional. Site URL for rankings on openrouter.ai.
7 'X-Title': '<YOUR_SITE_NAME>', // Optional. Site title for rankings on openrouter.ai.
8 },
9});
10
11const completion = await openRouter.chat.send({
12 model: 'openai/gpt-4o',
13 messages: [
14 {
15 role: 'user',
16 content: 'What is the meaning of life?',
17 },
18 ],
19 stream: false,
20});
21
22console.log(completion.choices[0].message.content);

Using the OpenRouter API directly

You can use the interactive Request Builder to generate OpenRouter API requests in the language of your choice.

1import { OpenRouter } from '@openrouter/sdk';
2
3const openRouter = new OpenRouter({
4 apiKey: '<OPENROUTER_API_KEY>',
5 defaultHeaders: {
6 'HTTP-Referer': '<YOUR_SITE_URL>', // Optional. Site URL for rankings on openrouter.ai.
7 'X-Title': '<YOUR_SITE_NAME>', // Optional. Site title for rankings on openrouter.ai.
8 },
9});
10
11const response = await openRouter.chat.send({
12 model: 'openai/gpt-4o',
13 messages: [
14 {
15 role: 'user',
16 content: 'What is the meaning of life?',
17 },
18 ],
19 stream: false,
20});

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

The API also supports streaming.

Using third-party SDKs

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