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.

OpenRouterProvider extends BaseProvider and is responsible for constructing model instances bound to a specific configuration. It is normally obtained via OpenRouter::create() or OpenRouter::default(), but can be instantiated directly when you need multiple named provider configurations — for example, routing different requests through different API keys or base URLs within the same process. Namespace: AiSdk\OpenRouter
File: src/OpenRouterProvider.php

Constructor

public function __construct(public readonly OpenRouterOptions $options)
Accepts a fully-constructed OpenRouterOptions instance and exposes it as a public readonly property.
options
OpenRouterOptions
required
The options object that carries the API key, base URL, extra headers, and optional HTTP client override for all models created by this provider. See OpenRouterOptions for details.

Methods

name()

public function name(): string
Returns the canonical string identifier for this provider. Returns: string — always 'openrouter' (the value of OpenRouterOptions::PROVIDER_NAME).

textModel()

public function textModel(string $modelId): TextModelInterface
Constructs and returns an OpenRouterTextModel configured with this provider’s options and model registry.
modelId
string
required
The OpenRouter model identifier, e.g. 'openai/gpt-4o' or 'anthropic/claude-sonnet-4'.
Returns: TextModelInterface — an OpenRouterTextModel instance ready to call generate() or stream().

imageModel()

public function imageModel(string $modelId): ImageModelInterface
Constructs and returns an OpenRouterImageModel configured with this provider’s options and model registry.
modelId
string
required
The OpenRouter model identifier for an image-capable model, e.g. 'recraft/recraft-v4.1-vector'.
Returns: ImageModelInterface — an OpenRouterImageModel instance ready to call generate().

Direct Instantiation Example

Using OpenRouterProvider directly is useful when you want to maintain multiple independent provider configurations — for example, one per environment or tenant.
<?php

use AiSdk\OpenRouter\OpenRouterOptions;
use AiSdk\OpenRouter\OpenRouterProvider;

$provider = new OpenRouterProvider(
    OpenRouterOptions::fromArray(['apiKey' => 'or-...'])
);

$model = $provider->textModel('openai/gpt-4o');
$result = $model->generate($request);

Multiple Configurations

<?php

use AiSdk\OpenRouter\OpenRouterOptions;
use AiSdk\OpenRouter\OpenRouterProvider;

// Production provider using default base URL
$production = new OpenRouterProvider(
    OpenRouterOptions::fromArray([
        'apiKey'  => 'or-prod-key',
        'headers' => ['HTTP-Referer' => 'https://myapp.com'],
    ])
);

// Staging provider pointing at a custom endpoint
$staging = new OpenRouterProvider(
    OpenRouterOptions::fromArray([
        'apiKey'  => 'or-staging-key',
        'baseUrl' => 'https://staging.openrouter.ai/api/v1',
    ])
);

$prodModel    = $production->textModel('openai/gpt-4o');
$stagingModel = $staging->textModel('openai/gpt-4o');
When you only need a single global provider, prefer the OpenRouter facade, which manages the singleton lifecycle for you.

Build docs developers (and LLMs) love