Browse documentation

Chat Completions

Create streaming and non-streaming chat completions using nRouter. OpenAI-compatible request format with automated fallbacks, guardrails, and cost headers.

The /v1/chat/completions endpoint serves unified OpenAI- and Anthropic-compatible chat completions across provider clouds at exact provider list prices with zero percent (0%) markup. The request and response shape adheres to the OpenAI Chat Completions API specification with enterprise reliability, safety, and telemetry controls.

Key capabilities:

  • Unified Wire: Routes across OpenAI, Anthropic, Google Vertex AI, Azure Foundry, and Alibaba US using a single virtual key (sk-nrouter-*).
  • 4-Phase Preflight: Validates key ACLs, checks rate limits (RPM/TPM), executes concurrent content moderation and prompt injection defense, and reserves credit before upstream dispatch.
  • Direct & Automatic Selection: Target a specific model (Direct) or use nrouter/auto, with custom per-request failover chains (nrouter_fallbacks).
  • Edge Telemetry: Returns real-time latency, upstream routing, and exact spend via x-nr-* headers (x-nr-request-cost, x-nr-latency-ms, x-nr-request-id).
POST https://api.nrouter.ai/v1/chat/completions

Start building with nRouter

One API key, three steps. Choose your tool to begin.

  1. Set your API key

    export NROUTER_API_KEY="<YOUR_API_KEY>"
  2. Configure Claude Code

    Choose a tool, then copy its setup from the panel.

npm install -g @anthropic-ai/claude-code
Claude Code · gpt-5.4-mini
# Point Claude Code to nRouter gateway
export ANTHROPIC_BASE_URL="https://api.nrouter.ai"
export ANTHROPIC_API_KEY="$NROUTER_API_KEY"

# Launch with gpt-5.4-mini
claude --model gpt-5.4-mini

Examples

import { nRouter } from "@nrouter_ai/sdk";
const client = new nRouter({ apiKey: process.env.NROUTER_API_KEY });
const res = await client.chat.completions.create({
  model: "gpt-5.4-mini",
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(res.choices[0].message.content, client.lastResponse?.cost);

Streaming & Routing

Pass "stream": true to stream chunks formatted as Server-Sent Events (data: {...}). Latency (x-nr-latency-ms) measures edge Time-To-First-Byte (TTFB); spend settles atomically at end-of-stream.

Two ways to ask for a model in the "model" field:

Parameter ValueTypeBehavior
gpt-5.4-miniDirectDirect routing. Never swapped; answers only through a fallback you configured yourself (in Router Settings or nrouter_fallbacks on request) when provider cannot take request.
nrouter/autonrouter/autoAutomatic selection (Best for the task or Lowest cost). Heavy signals: reasoning, long input, multi-turn tools. Up to two fallbacks. nrouter_fallbacks replaces the fallbacks nrouter/auto would choose. Reports served model in x-nr-model billed at list price.

Smart Router aliases were withdrawn on 2026-10-15; a request naming one returns the unknown-model error.

Request Options

Optional body fields configure single-call routing, guardrails, and caching (stripped before upstream forwarding):

FieldTypeLimitBehavior
nrouter_fallbacksstring[]1–4 entriesModels tried in order if primary admission fails. Replaces configured fallbacks for this call. Every target must be routable by your key or returns 400 fallback_not_allowed. Failover only on 429, 503, Anthropic 529, 401/404 from a misconfigured deployment, a connect-phase failure, or a stalled first-byte timeout. Uses one credit reservation and one rate-limit slot (at most 3 provider calls).
nrouter_guardrailsstring[]1–8 entriesGuardrail IDs added to the pre-call chain. Add-only; never weakens platform safety. Unknown entries return 400 guardrail_not_found.
nrouter_cacheboolean—When false, bypasses the cache (x-nr-response-cache: bypass).
curl -X POST https://api.nrouter.ai/v1/chat/completions \
  -H "Authorization: Bearer $NROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-5.4-mini","messages":[{"role":"user","content":"Hello!"}],"nrouter_fallbacks":["claude-sonnet-4-5"],"nrouter_guardrails":["pii-strict"],"nrouter_cache":false}'

See Per-Request Options and Guardrails.

Headers Reference

Inbound: Authorization: Bearer sk-nrouter-... and Content-Type: application/json required; optional: x-nr-tags, x-nr-compress, x-nr-mcp-server.

HeaderTypeDescription
x-nr-request-idstringUnique correlation identifier for tracing.
x-nr-latency-msintegerGateway edge turnaround time in milliseconds (TTFB for streams).
x-nr-request-costfloatExact USD inference cost from provider token rates (absent when unpriced).
x-nr-cost-statusstringexact when priced, unpriced when cost cannot be calculated.
x-nr-modelstringPhysical model that served the request.
x-nr-routingstringdirect or fallback:<index> (absent on cache hits and refusals).
x-nr-attemptsintegerTotal provider attempts made (≥ 1).
x-nr-guardrailsstringPosture: none, monitor, pass, redacted, partial, blocked, or unavailable.
x-nr-limit-sourcestringRate/budget ceiling: key, plan, team, user, budget, or capacity.
x-nr-allowance-resetintegerSeconds until usage allowance resets.
Retry-AfterintegerSeconds before retrying a 429 response.
x-nr-auth-reasonstringReason key was refused (unauthorized, key_blocked, key_expired, etc.).
x-nr-budget-warningstringEmitted when soft budget threshold is crossed.
x-nr-response-cachestringCache status: hit, miss, or bypass.
x-nr-response-cache-ageintegerAge of cache entry in seconds.
x-nr-compressionstringCompression status: applied, not_requested, off, or skipped.

Prompt Compression & Guardrails

Prompt compression is enabled by default for qualified models (opt out with x-nr-compress: off or "nrouter_compress": false). Preflight executes (1) key validation, (2) rate limits (RPM/TPM), (3) content inspection, and (4) credit reservation. Blocked requests return HTTP 400 (x-nr-guardrails: blocked) with $0 spent and $0 held. See Guardrails.

POST
/v1/chat/completions

Authorization

NRouterApiKey
AuthorizationBearer <token>

Your nRouter virtual key (sk-nrouter-…). Sent as Authorization: Bearer sk-nrouter-….

In: header

Header Parameters

x-nr-compress?string

Request prompt compression: on to compress eligible prompts, off to skip

Value in

  • "on"
  • "off"
x-nr-tags?string

Custom spend and attribution tags (comma-separated key=value pairs)

x-nr-mcp-server?string

Target MCP server ID when routing MCP tool calls or prompts through the gateway

x-nr-client-language?string

Caller SDK runtime language identification (e.g. typescript, python, rust, go)

x-nr-session-id?string

Caller session identifier allowed at edge for grouping related client turns

x-nr-trace?string

Inbound trace detail level override for request lifecycle spans

Value in

  • "off"
  • "stages"
  • "verbose"
x-nr-trace-stages?string

Inbound stage-level trace emission override

Value in

  • "off"
  • "stages"
  • "verbose"
x-nr-trace-id?string

Accepted so a client sending it is not refused; the value is not recorded and not propagated. Every response carries the gateway's own trace ID in this header, never the one sent.

Request Body

application/json

TypeScript Definitions

Use the request body type in TypeScript.

Response Body

application/json

application/json

application/json

application/json

application/json

application/json

application/json

application/json

application/json

curl -X POST "https://example.com/v1/chat/completions" \  -H "Content-Type: application/json" \  -d '{    "model": "gpt-5.4-mini",    "messages": [      {        "role": "system",        "content": "You are a helpful AI assistant powered by nRouter."      },      {        "role": "user",        "content": "What models does nRouter support?"      }    ],    "temperature": 0.7,    "max_tokens": 1024  }'
{  "id": "chatcmpl-abc123",  "object": "chat.completion",  "model": "gpt-4o",  "choices": [    {      "index": 0,      "message": {        "role": "assistant",        "content": "Hello!"      },      "finish_reason": "stop"    }  ],  "usage": {    "prompt_tokens": 9,    "completion_tokens": 2,    "total_tokens": 11  }}
Was this page helpful?