Install OpenRouter

Install OpenRouter on your machine

openrouter

Documentation and setup instructions for openrouter

I want you to install OpenRouter for me. Execute all the steps below autonomously.

OBJECTIVE: Install the OpenRouter SDK and verify it works by making an API call to an AI model.

DONE WHEN: You have successfully made a chat completion request and received a response from an AI model through OpenRouter.

TODO

  • Get an OpenRouter API key
  • Install the OpenRouter SDK (TypeScript or Python)
  • Set up the API key as an environment variable
  • Create a test script that makes a chat completion request
  • Run the script and verify a response is returned

Get an API Key

You must obtain an OpenRouter API key before proceeding. Visit https://openrouter.ai/settings/keys to create one.

If you don’t have an OpenRouter account, sign up at https://openrouter.ai first.

Installation

You need to install the OpenRouter SDK. Choose TypeScript or Python based on your project:

TypeScript (npm):

$npm install @openrouter/sdk

TypeScript (yarn):

$yarn add @openrouter/sdk

TypeScript (pnpm):

$pnpm add @openrouter/sdk

Python:

$pip install openrouter

API Key Setup

You must set your OpenRouter API key as an environment variable. The API key is required for all requests.

On macOS/Linux:

$export OPENROUTER_API_KEY="sk-or-v1-YOUR-API-KEY"

On Windows (PowerShell):

1$env:OPENROUTER_API_KEY="sk-or-v1-YOUR-API-KEY"

On Windows (Command Prompt):

1set OPENROUTER_API_KEY=sk-or-v1-YOUR-API-KEY

Create Test Script

Your task is to create a test script that makes a chat completion request and outputs the result.

TypeScript - create test_openrouter.ts:

1import { OpenRouter } from '@openrouter/sdk';
2
3const openRouter = new OpenRouter({
4 apiKey: process.env.OPENROUTER_API_KEY,
5});
6
7async function main() {
8 const completion = await openRouter.chat.send({
9 model: 'openai/gpt-4o-mini',
10 messages: [
11 {
12 role: 'user',
13 content: 'Say "Hello from OpenRouter!" and nothing else.',
14 },
15 ],
16 stream: false,
17 });
18
19 console.log(completion.choices[0].message.content);
20}
21
22main();

Python - create test_openrouter.py:

1import os
2from openrouter import OpenRouter
3
4client = OpenRouter(api_key=os.environ.get("OPENROUTER_API_KEY"))
5
6completion = client.chat.completions.create(
7 model="openai/gpt-4o-mini",
8 messages=[
9 {
10 "role": "user",
11 "content": "Say 'Hello from OpenRouter!' and nothing else."
12 }
13 ]
14)
15
16print(completion.choices[0].message.content)

Run the Test

You must execute the test script to verify the installation works:

TypeScript:

$npx tsx test_openrouter.ts

Python:

$python test_openrouter.py

The response should contain the text “Hello from OpenRouter!” confirming that the SDK is properly installed and your API key is working.

Alternative: Using the OpenAI SDK

If you prefer to use the OpenAI SDK with OpenRouter, you can configure it to use OpenRouter’s API endpoint:

TypeScript - install and use OpenAI SDK:

$npm install openai
1import OpenAI from 'openai';
2
3const openai = new OpenAI({
4 baseURL: 'https://openrouter.ai/api/v1',
5 apiKey: process.env.OPENROUTER_API_KEY,
6});
7
8async function main() {
9 const completion = await openai.chat.completions.create({
10 model: 'openai/gpt-4o-mini',
11 messages: [
12 {
13 role: 'user',
14 content: 'Say "Hello from OpenRouter!" and nothing else.',
15 },
16 ],
17 });
18
19 console.log(completion.choices[0].message.content);
20}
21
22main();

Python - install and use OpenAI SDK:

$pip install openai
1import os
2from openai import OpenAI
3
4client = OpenAI(
5 base_url="https://openrouter.ai/api/v1",
6 api_key=os.environ.get("OPENROUTER_API_KEY"),
7)
8
9completion = client.chat.completions.create(
10 model="openai/gpt-4o-mini",
11 messages=[
12 {
13 "role": "user",
14 "content": "Say 'Hello from OpenRouter!' and nothing else."
15 }
16 ]
17)
18
19print(completion.choices[0].message.content)

Alternative: Using curl

You can also test the API directly with curl:

$curl https://openrouter.ai/api/v1/chat/completions \
> -H "Content-Type: application/json" \
> -H "Authorization: Bearer $OPENROUTER_API_KEY" \
> -d '{
> "model": "openai/gpt-4o-mini",
> "messages": [
> {
> "role": "user",
> "content": "Say Hello from OpenRouter! and nothing else."
> }
> ]
>}'

Next Steps

Once installation is verified, you can explore these additional capabilities:

Use different models: OpenRouter provides access to hundreds of AI models. Browse available models at https://openrouter.ai/models

Enable streaming: Add stream: true to your request to receive responses as they are generated.

Use tool calling: Many models support function/tool calling for structured interactions.

Set up app attribution: Add HTTP-Referer and X-OpenRouter-Title headers to have your app appear on OpenRouter leaderboards.

For full documentation, visit https://openrouter.ai/docs

EXECUTE NOW: Complete the above TODO list to achieve: Successfully made a chat completion request and received a response from an AI model through OpenRouter.


If you think the above is not enough to accomplish the task, you can find navigation and other pages relevant to this product by using the llms.txt file at: https://openrouter.ai/llms.txt