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.

The OpenRouter PHP SDK is the official provider package for integrating OpenRouter into applications built with the PHP AI SDK. It connects to OpenRouter’s OpenAI-compatible API, giving you access to models from OpenAI, Anthropic, Google, xAI, and hundreds of other providers — all through one unified interface.

Installation

Install via Composer and get your API key configured in minutes.

Quickstart

Generate your first AI response with just a few lines of PHP.

Text Generation

Generate text completions using any model available on OpenRouter.

Image Generation

Create images with models like GPT-Image and Recraft through OpenRouter.

Streaming

Stream responses token-by-token for real-time user experiences.

API Reference

Full reference for all public classes and methods.

Why OpenRouter?

OpenRouter provides a unified gateway to hundreds of AI models. Rather than managing separate API keys and SDKs for each provider, you route all requests through OpenRouter and let it handle model selection, failover, and load balancing.

370+ Models

Access OpenAI, Anthropic, Google, xAI, and many more from a single API key.

OpenAI-Compatible

Built on a shared OpenAI-compatible wire adapter — familiar API, broad coverage.

Provider Control

Route to specific providers, set reasoning effort, and pass raw provider options.

Getting Started

1

Install the package

composer require aisdk/openrouter
2

Set your API key

export OPENROUTER_API_KEY="or-..."
3

Generate your first response

use AiSdk\Generate;
use AiSdk\OpenRouter;

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

echo $result->text;
You’ll need a free OpenRouter account to obtain an API key. The OPENROUTER_API_KEY environment variable is read automatically — no additional configuration needed to get started.

Build docs developers (and LLMs) love