PHP SDK

Use Nemo Router with openai-php/client

Last updated

Nemo Router works with openai-php/client. Set the base URI to https://api.nemorouter.ai/v1 and pass your sk-nemo-... key — guardrails, caching, and rate-limits auto-apply from your org config.

Installation

composer require openai-php/client

Setup

<?php
require 'vendor/autoload.php';

$client = OpenAI::factory()
    ->withApiKey(getenv('NEMOROUTER_API_KEY'))
    ->withBaseUri('https://api.nemorouter.ai/v1')
    ->make();

Chat Completion

<?php
$response = $client->chat()->create([
    'model' => 'claude-sonnet-4-20250514',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!'],
    ],
]);

echo $response->choices[0]->message->content . "\n";

Per-Request Overrides

openai-php/client forwards unknown keys, so nemo_* fields work inline:

<?php
// Apply only specific guardrails
$response = $client->chat()->create([
    'model' => 'gpt-4o',
    'messages' => [['role' => 'user', 'content' => 'Summarize Q1 earnings...']],
    'nemo_guardrail_ids' => ['guardrail-uuid-1', 'guardrail-uuid-2'],
]);

// Run a prompt template with variables
$response = $client->chat()->create([
    'model' => 'gpt-4o',
    'messages' => [['role' => 'user', 'content' => 'Q1 revenue was $4.2M...']],
    'nemo_prompt_template_id' => 'your-summarizer-id',
    'nemo_prompt_variables' => ['language' => 'Spanish', 'max_length' => '100'],
]);

// Bypass cache for a single call
$response = $client->chat()->create([
    'model' => 'gpt-4o',
    'messages' => [['role' => 'user', 'content' => 'What is the latest news?']],
    'nemo_cache' => false,
]);

Error Handling

<?php
try {
    $client->chat()->create([
        'model' => 'gpt-4o',
        'messages' => [['role' => 'user', 'content' => 'My SSN is 123-45-6789']],
    ]);
} catch (\Exception $e) {
    // "guardrail_blocked" — guardrail rejected the request
    // "insufficient_credits" — top up to continue
    echo "Error: {$e->getMessage()}\n";
}

Response Headers

Every successful response carries:

  • x-nemo-request-cost — exact USD spend for this call
  • x-nemo-guardrails-applied — comma-separated guardrail names that ran
  • x-nemo-prompt-version — version of the prompt template applied (if any)

Next Steps

Was this page helpful?