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.