OpenRouter PHP for Laravel is a community-maintained PHP API client that allows you to interact with the OpenRouter API. If you or your business relies on this package, it's important to support the developers who have contributed their time and effort to create and maintain this valuable tool:
- Nuno Maduro: github.com/sponsors/nunomaduro
- Sandro Gehri: github.com/sponsors/gehrisandro
Note: This repository contains the integration code of the OpenRouter PHP for Laravel. If you want to use the OpenAI PHP client in a framework-agnostic way, take a look at the openai-php/client repository.
Requires PHP 8.2+
First, install OpenRouter via the Composer package manager:
composer require atarim/openrouter-php-laravelNext, execute the install command:
php artisan openrouter:installThis will create a config/openrouter.php configuration file in your project, which you can modify to your needs
using environment variables.
Blank environment variables for the OpenAI API key and organization id are already appended to your .env file.
OPENROUTER_API_KEY=sk-...
OPENROUTER_ORGANIZATION=org-...Finally, you may use the OpenRouter facade to access the OpenRouter API:
use OpenRouter\Laravel\Facades\OpenRouter;
$response = OpenRouter::responses()->create([
'model' => 'gpt-5',
'input' => 'Hello!',
]);
echo $response->outputText; // Hello! How can I assist you today?
Configuration is done via environment variables or directly in the configuration file (config/openrouter.php).
Specify your OpenRouter API Key and organization. This will be used to authenticate with the OpenRouter API - you can find your API key and organization on your OpenRouter dashboard.
OPENROUTER_API_KEY=
OPENROUTER_ORGANIZATION=For implementations that require a project ID, you can specify the OpenAI project ID in your environment variables.
OPENROUTER_PROJECT=proj_...The base URL for the OpenRouter API.
OPENROUTER_BASE_URL=The timeout may be used to specify the maximum number of seconds to wait for a response. By default, the client will time out after 30 seconds.
OPENROUTER_REQUEST_TIMEOUT=For usage examples, take a look at the openai-php/client repository.
The OpenRouter facade comes with a fake() method that allows you to fake the API responses.
The fake responses are returned in the order they are provided to the fake() method.
All responses are having a fake() method that allows you to easily create a response object by only providing the parameters relevant for your test case.
use OpenRouter\Laravel\Facades\OpenRouter;
use OpenAI\Responses\Responses\CreateResponse;
OpenRouter::fake([
CreateResponse::fake([
'choices' => [
[
'text' => 'awesome!',
],
],
]),
]);
$response = OpenRouter::responses()->create([
'model' => 'gpt-5',
'input' => 'PHP is ',
]);
expect($response->outputText)->toBe('awesome!');After the requests have been sent there are various methods to ensure that the expected requests were sent:
use OpenAI\Resources\Responses;
// assert completion create request was sent
OpenAI::assertSent(Responses::class, function (string $method, array $parameters): bool {
return $method === 'create' &&
$parameters['model'] === 'gpt-5' &&
$parameters['prompt'] === 'PHP is ';
});For more testing examples, take a look at the openai-php/client repository.
OpenRouter PHP for Laravel is an open-sourced software licensed under the MIT license.