API Keys
Users or developers can cover model costs with normal API keys. This allows you to use curl
or the OpenAI SDK directly with OpenRouter. Just create an API key, set the api_base
, and optionally set a referrer header to make your app discoverable to others on OpenRouter.
Note: API keys on OpenRouter are more powerful than keys used directly for model APIs. They allow users to set credit limits for apps, and they can be used in OAuth flows.
Example code:
import openai
openai.api_base = "https://openrouter.ai/api/v1"
openai.api_key = $OPENROUTER_API_KEY
response = openai.ChatCompletion.create(
model="openai/gpt-3.5-turbo",
messages=[...],
headers={
"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.
},
)
reply = response.choices[0].message
To stream with Python, see this example from OpenAI.
If your key has been exposed
You must protect your API keys and never commit them to public repositories.
OpenRouter is a GitHub secret scanning partner, and has other methods to detect exposed keys. If we determine that your key has been compromised, you will receive an email notification.
If you receive such a notification or suspect your key has been exposed, immediately visit:
https://openrouter.ai/settings/keys
to delete the compromised key and create a new one. Using environment variables and keeping keys out of your codebase is strongly recommended.