All requests to the OpenRouter API are authenticated with a Bearer token derived from your API key. The SDK resolves the key automatically — it reads from theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openrouter/llms.txt
Use this file to discover all available pages before exploring further.
OPENROUTER_API_KEY environment variable by default, or accepts an explicit apiKey entry in the config array passed to OpenRouter::create().
Obtaining an API key
Visit openrouter.ai, create an account, and navigate to the Keys section of your dashboard to generate a new key. OpenRouter API keys begin with the prefixor-.
Setting up authentication
Generate your API key
Log in to openrouter.ai and generate an API key from your account dashboard. Copy the key — it will only be shown once.
Set the environment variable
The recommended approach is to store your key in an environment variable so it is never hard-coded in source files.Add it to your Or export it in your shell before running your application:The SDK reads this variable automatically via
.env file:Env::loadApiKey(), so no additional code is required.Programmatic configuration
You can supply the API key directly in code by passing it toOpenRouter::create(). This is useful when you manage secrets through a dedicated secrets manager and inject them at runtime.
OpenRouter::create() stores the resulting provider as the default singleton, so subsequent calls to OpenRouter::model() and OpenRouter::image() automatically use the key you provided.How authentication works
Internally, the key is attached to every outgoing HTTP request via theOpenRouterOptions::authHeaders() method:
Authorization: Bearer <key> header is merged with any custom headers you configure and sent on every request to the OpenRouter API.