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.

This page walks you through installing aisdk/openrouter into your project, configuring your API key, and confirming that the package loads without errors so you are ready to make your first request.
aisdk/openrouter requires PHP 8.3 or higher. Check your version with php -v before proceeding.
1

Install the package

Run the following command in your project root. Composer will resolve and install aisdk/openrouter along with its dependencies — aisdk/core and aisdk/openai-compatible.
composer require aisdk/openrouter
2

Set your API key

The SDK reads your OpenRouter API key from the OPENROUTER_API_KEY environment variable. You can obtain a key from openrouter.ai/keys.In a .env file (recommended for local development):
OPENROUTER_API_KEY=or-...
In your shell (for scripts and CI):
export OPENROUTER_API_KEY="or-..."
Via the config array (for runtime or framework integration):
use AiSdk\OpenRouter;

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

Configure the base URL (optional)

By default the SDK sends requests to https://openrouter.ai/api/v1. If you need to override this — for example to point at a proxy or a staging environment — set the OPENROUTER_BASE_URL environment variable:
OPENROUTER_BASE_URL=https://your-proxy.example.com/api/v1
You can also pass it directly in the config array:
use AiSdk\OpenRouter;

OpenRouter::create([
    'apiKey'  => 'or-...',
    'baseUrl' => 'https://your-proxy.example.com/api/v1',
]);
If neither the environment variable nor the config key is set, the SDK defaults to https://openrouter.ai/api/v1.
4

Verify the install

Create a small PHP script to confirm the autoloader resolves the package and the default provider initialises correctly. A successful run with no exceptions means the installation is complete.
<?php

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

use AiSdk\OpenRouter;

$provider = OpenRouter::default();

echo 'OpenRouter provider ready: ' . $provider->name() . PHP_EOL;
// Output: OpenRouter provider ready: openrouter
Run it with:
php verify.php
To pass additional HTTP headers on every request — such as an HTTP-Referer for OpenRouter analytics — supply a headers array when calling OpenRouter::create():
OpenRouter::create([
    'apiKey'  => 'or-...',
    'headers' => [
        'HTTP-Referer'       => 'https://example.com',
        'X-OpenRouter-Title' => 'My App',
    ],
]);

Build docs developers (and LLMs) love