PHP

routeur.ai is OpenAI-compatible, so the widely-used openai-php/client package works as-is — just override the base URI and pass your routeur.ai key. There is no separate routeur.ai SDK to install.

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:

  1. Routeur-* request headers. If Routeur-Provider or Routeur-Model are set, they win, regardless of what is in the body. Useful from middleware or transports that can't change the JSON.
  2. 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.
  3. '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();