Install OpenRouter
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):
TypeScript (yarn):
TypeScript (pnpm):
Python:
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:
On Windows (PowerShell):
On Windows (Command Prompt):
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:
Python - create test_openrouter.py:
Run the Test
You must execute the test script to verify the installation works:
TypeScript:
Python:
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:
Python - install and use OpenAI SDK:
Alternative: Using curl
You can also test the API directly with curl:
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