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.

OpenRouterImageModel implements ImageModelInterface and handles image generation requests routed through OpenRouter. It serialises requests with ImageRequestBuilder from aisdk/openai-compatible, applying OpenRouter-specific builder options: aspect ratio is sent under the aspect_ratio key, size is not inferred from the aspect ratio, and the response_format field is omitted from the request body entirely. Responses are parsed by ImageResponseParser, which extracts the base64-encoded image, MIME type, and token usage from the API payload. Namespace: AiSdk\OpenRouter\Models
File: src/Models/OpenRouterImageModel.php

Constructor

public function __construct(
    private readonly string $modelId,
    private readonly OpenRouterOptions $options,
    private readonly ?ModelRegistry $registry = null,
)
modelId
string
required
The OpenRouter model identifier for an image-capable model, e.g. 'recraft/recraft-v4.1-vector'.
options
OpenRouterOptions
required
The provider configuration, including API key, base URL, and extra headers. See OpenRouterOptions.
registry
ModelRegistry|null
default:"null"
An optional model registry for runtime capability overrides. When null, capability lookups use the bundled resources/models.json catalog.
OpenRouterImageModel instances are not normally created directly. Use OpenRouter::image() or OpenRouterProvider::imageModel() instead.

Methods

provider()

public function provider(): string
Returns the canonical provider identifier. Returns: string — always 'openrouter'.

modelId()

public function modelId(): string
Returns the model identifier supplied at construction. Returns: string — the model ID, e.g. 'recraft/recraft-v4.1-vector'.

capabilities()

public function capabilities(): array<int, Capability>
Resolves the full list of capabilities supported by this model. The lookup order is:
  1. The injected ModelRegistry, if one was provided.
  2. The bundled resources/models.json catalog.
Returns: array<int, Capability> — an array of Capability enum cases supported by the model.

capability()

public function capability(Capability $capability): CapabilitySupport
Returns the support level for a single capability. The resolution order is:
  1. Any capability override configured directly on the model instance.
  2. The injected ModelRegistry.
  3. The bundled resources/models.json catalog.
capability
Capability
required
The Capability enum case to check, e.g. Capability::ImageGeneration.
Returns: CapabilitySupport — the support level for the requested capability.

generate()

public function generate(ImageRequest $request): ImageResponse
Sends a POST request to the image generation endpoint and returns a parsed response. Internally:
  1. ImageRequestBuilder::build() serialises $request into a JSON body using the following OpenRouter-specific options:
    OptionValueEffect
    aspectRatioParameter'aspect_ratio'Aspect ratio is serialised under the aspect_ratio key instead of size.
    inferSizeFromAspectRatiofalseThe builder does not attempt to derive a size value from the aspect ratio.
    includeResponseFormatfalseThe response_format field is not included in the request body.
  2. The body is POSTed to Url::joinPath($options->baseUrl, '/images') with the headers from $options->authHeaders().
  3. ImageResponseParser::parse() maps the JSON payload to an ImageResponse.
request
ImageRequest
required
The image generation request, including the prompt, count, aspect ratio, and any provider-specific options.
Returns: ImageResponse — contains:
output.base64
string
The base64-encoded image data.
output.mimeType
string
The MIME type of the generated image, e.g. 'image/svg+xml' or 'image/png'.
usage.totalTokens
int
The total number of tokens consumed by the request (prompt + completion combined).

Endpoint

All requests are sent to:
POST {baseUrl}/images
The default resolved URL is:
POST https://openrouter.ai/api/v1/images

Usage Example

<?php

use AiSdk\Generate;
use AiSdk\OpenRouter;

OpenRouter::create(['apiKey' => 'or-...']);

$result = Generate::image()
    ->model(OpenRouter::image('recraft/recraft-v4.1-vector'))
    ->prompt('A vector illustration of a mountain range at sunset')
    ->count(1)
    ->aspectRatio('16:9')
    ->run();

echo $result->output->mimeType;   // e.g. 'image/svg+xml'
echo $result->usage->totalTokens; // e.g. 14
// Decode and save the image
file_put_contents('output.svg', base64_decode($result->output->base64));

Passing Provider-specific Options

OpenRouter supports forwarding raw provider options (for example, Recraft style settings) via providerOptions():
<?php

use AiSdk\Generate;
use AiSdk\OpenRouter;

OpenRouter::create(['apiKey' => 'or-...']);

$result = Generate::image()
    ->model(OpenRouter::image('recraft/recraft-v4.1-vector'))
    ->prompt('A vector bird')
    ->count(1)
    ->aspectRatio('1:1')
    ->providerOptions('openrouter', [
        'raw' => [
            'provider' => [
                'options' => [
                    'recraft' => ['style' => 'vector'],
                ],
            ],
        ],
    ])
    ->run();
For a broader introduction to image generation, see the Image Generation usage guide.

Build docs developers (and LLMs) love