Install
composer require openai-php/client
Configure
Point the factory at https://api.routeur.ai/v1 and use your routeur.ai key:
<?php
use OpenAI;
$client = OpenAI::factory()
->withApiKey(getenv('ROUTEUR_KEY'))
->withBaseUri('api.routeur.ai/v1')
->make();
Chat completion
$res = $client->chat()->create([
'model' => 'auto',
'messages' => [
['role' => 'user', 'content' => 'hi'],
],
]);
echo $res->choices[0]->message->content;
Streaming
$stream = $client->chat()->createStreamed([
'model' => 'auto',
'messages' => [['role' => 'user', 'content' => 'stream please']],
]);
foreach ($stream as $chunk) {
echo $chunk->choices[0]->delta->content ?? '';
}
Choosing a model
Every request picks an upstream model in one of three ways — in this order of precedence:
- Routeur-* request headers. If
Routeur-ProviderorRouteur-Modelare set, they win, regardless of what is in the body. Useful from middleware or transports that can't change the JSON. - An explicit upstream id in
model. Pass something like'gpt-4o-mini'or'claude-3-5-sonnet'to bypass routing rules and pin the request to that model. 'auto'(or any alias). Lets routeur.ai pick the upstream model from your routing rules. This is the recommended default.
Force a specific provider+model on every call via headers:
$client = OpenAI::factory()
->withApiKey(getenv('ROUTEUR_KEY'))
->withBaseUri('api.routeur.ai/v1')
->withHttpHeader('Routeur-Provider', 'openai')
->withHttpHeader('Routeur-Model', 'gpt-4o')
->make();