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.

By the end of this page you will have generated a text completion using openai/gpt-4o and a raster image using openai/gpt-image-1 — both routed through OpenRouter from your own PHP script. The only prerequisite is a Composer-managed PHP 8.3+ project and an OpenRouter API key.
1

Install the package

Add aisdk/openrouter to your project with Composer. This also installs aisdk/core and aisdk/openai-compatible automatically.
composer require aisdk/openrouter
2

Set your API key

Export your OpenRouter API key into the shell environment. The SDK picks it up from OPENROUTER_API_KEY automatically — no additional configuration required.
export OPENROUTER_API_KEY="or-..."
You can get a key from openrouter.ai/keys. Keys begin with the or- prefix.
3

Generate your first text response

Create a file called generate_text.php and paste the following code. It builds a text-generation request against openai/gpt-4o, attaches a system instruction and a user prompt, then prints the model’s reply.
<?php

require __DIR__ . '/vendor/autoload.php';

use AiSdk\Generate;
use AiSdk\OpenRouter;

$result = Generate::text()
    ->model(OpenRouter::model('openai/gpt-4o'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;
Run the script:
php generate_text.php
You should see a concise explanation of PHP closures printed to your terminal. The $result->text property contains the raw string returned by the model.
4

Generate your first image

Create a file called generate_image.php. This example calls the image-generation endpoint using openai/gpt-image-1, requests a 1024 × 1024 output, and saves the result to disk.
<?php

require __DIR__ . '/vendor/autoload.php';

use AiSdk\Generate;
use AiSdk\OpenRouter;

$result = Generate::image()
    ->model(OpenRouter::image('openai/gpt-image-1'))
    ->prompt('A clean app icon for a PHP AI SDK')
    ->size('1024x1024')
    ->run();

$result->output->save(__DIR__ . '/icon.png');

echo 'Image saved to icon.png' . PHP_EOL;
Run the script:
php generate_image.php
The SDK fetches the image data from OpenRouter and the save() helper writes it to icon.png in the same directory. You can open the file immediately to verify the output.

What’s Next

Text Generation

Learn how to pass system instructions, control temperature and token limits, and parse structured output from text models.

Streaming

Stream tokens as they are generated for lower time-to-first-token and real-time display in CLI tools and web apps.

Image Generation

Explore image model options including size, count, aspect ratio, and provider-specific parameters for models like recraft/recraft-v4.1-vector.

Authentication

Configure API keys, custom base URLs, and per-request HTTP headers for proxies, analytics, and multi-tenant setups.

Build docs developers (and LLMs) love