Skip to main content

Documentation 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.

OpenRouterOptions holds all configuration for a provider instance. It is the single source of truth for the API key, base URL, custom HTTP headers, and optional HTTP client override used by every model that the provider creates. Instances are constructed via the static fromArray() factory, which resolves values from the supplied array and falls back to environment variables where appropriate. Namespace: AiSdk\OpenRouter
File: src/OpenRouterOptions.php

Constants

DEFAULT_BASE_URL
string
'https://openrouter.ai/api/v1' — the base URL used when neither the baseUrl config key nor the OPENROUTER_BASE_URL environment variable is set.
PROVIDER_NAME
string
'openrouter' — the canonical provider identifier used in model metadata and error messages throughout the SDK.

Constructor

public function __construct(
    public readonly string $apiKey,
    public readonly string $baseUrl = self::DEFAULT_BASE_URL,
    public readonly array $headers = [],
    public readonly ?Sdk $sdk = null,
)
All four properties are exposed as public readonly fields after construction. Prefer fromArray() over direct construction so that environment-variable fallbacks are applied automatically.

fromArray()

public static function fromArray(array $config = []): self
The recommended way to create an OpenRouterOptions instance. Resolves each option from the supplied $config array, falling back to environment variables and built-in defaults.

Parameters

apiKey
string
Your OpenRouter API key. When omitted, the value of the OPENROUTER_API_KEY environment variable is used. An exception is thrown if neither is present.
baseUrl
string
The base URL for all API requests. When omitted, the OPENROUTER_BASE_URL environment variable is checked first; if that is also unset, DEFAULT_BASE_URL (https://openrouter.ai/api/v1) is used. Trailing slashes are stripped automatically.
headers
array<string, string>
An associative array of additional HTTP headers sent with every request. Common uses include HTTP-Referer (required by some OpenRouter policies) and X-OpenRouter-Title (displayed in the OpenRouter dashboard). These are merged with the Authorization header produced by authHeaders().
sdk
Sdk|null
An AiSdk\Support\Sdk instance wrapping a PSR-18 HTTP client, PSR-17 request factory, and PSR-17 stream factory. When provided, it overrides the default HTTP transport. This option exists primarily for unit testing — supply a FakeHttpClient here instead of making real network calls.
Returns: self — a fully configured OpenRouterOptions instance.

authHeaders()

public function authHeaders(): array<string, string>
Builds the complete set of HTTP headers required for an authenticated request. It merges the Authorization: Bearer <apiKey> header with any entries from the headers array supplied at construction time. Model classes call this method when assembling each outbound HTTP request. Returns: array<string, string> — an associative array of HTTP header names to values, always containing at minimum Authorization.

Full Configuration Example

<?php

use AiSdk\OpenRouter;

OpenRouter::create([
    'apiKey'  => 'or-...',
    'baseUrl' => 'https://openrouter.ai/api/v1',
    'headers' => [
        'HTTP-Referer'       => 'https://example.com',
        'X-OpenRouter-Title' => 'My App',
    ],
]);

Environment Variable Configuration

# .env or shell environment
OPENROUTER_API_KEY=or-...
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
<?php

use AiSdk\OpenRouter;

// No explicit config needed — fromArray() reads the environment variables above.
OpenRouter::create();

Testing with a Fake HTTP Client

<?php

use AiSdk\OpenRouter;
use AiSdk\Support\Sdk;
use Nyholm\Psr7\Factory\Psr17Factory;

$factory = new Psr17Factory;
$sdk = new Sdk(
    httpClient: $fakeHttpClient,
    requestFactory: $factory,
    streamFactory: $factory,
);

OpenRouter::create([
    'apiKey' => 'or-test',
    'sdk'    => $sdk,
]);
The sdk option is intended for testing only. In production, omit it and let the SDK use its default PSR-18 HTTP client.

Build docs developers (and LLMs) love