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/completionsStart building with nRouter
One API key, three steps. Choose your tool to begin.
Get a key
Create API keySet your API key
export NROUTER_API_KEY="<YOUR_API_KEY>"Configure Claude Code
Choose a tool, then copy its setup from the panel.
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 Value | Type | Behavior |
|---|---|---|
gpt-5.4-mini | Direct | Direct 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/auto | nrouter/auto | Automatic 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):
| Field | Type | Limit | Behavior |
|---|---|---|---|
nrouter_fallbacks | string[] | 1–4 entries | Models 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_guardrails | string[] | 1–8 entries | Guardrail IDs added to the pre-call chain. Add-only; never weakens platform safety. Unknown entries return 400 guardrail_not_found. |
nrouter_cache | boolean | — | 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.
| Header | Type | Description |
|---|---|---|
x-nr-request-id | string | Unique correlation identifier for tracing. |
x-nr-latency-ms | integer | Gateway edge turnaround time in milliseconds (TTFB for streams). |
x-nr-request-cost | float | Exact USD inference cost from provider token rates (absent when unpriced). |
x-nr-cost-status | string | exact when priced, unpriced when cost cannot be calculated. |
x-nr-model | string | Physical model that served the request. |
x-nr-routing | string | direct or fallback:<index> (absent on cache hits and refusals). |
x-nr-attempts | integer | Total provider attempts made (≥ 1). |
x-nr-guardrails | string | Posture: none, monitor, pass, redacted, partial, blocked, or unavailable. |
x-nr-limit-source | string | Rate/budget ceiling: key, plan, team, user, budget, or capacity. |
x-nr-allowance-reset | integer | Seconds until usage allowance resets. |
Retry-After | integer | Seconds before retrying a 429 response. |
x-nr-auth-reason | string | Reason key was refused (unauthorized, key_blocked, key_expired, etc.). |
x-nr-budget-warning | string | Emitted when soft budget threshold is crossed. |
x-nr-response-cache | string | Cache status: hit, miss, or bypass. |
x-nr-response-cache-age | integer | Age of cache entry in seconds. |
x-nr-compression | string | Compression 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.
Authorization
NRouterApiKey Your nRouter virtual key (sk-nrouter-…). Sent as Authorization: Bearer sk-nrouter-….
In: header
Header Parameters
Request prompt compression: on to compress eligible prompts, off to skip
Value in
- "on"
- "off"
Target MCP server ID when routing MCP tool calls or prompts through the gateway
Caller SDK runtime language identification (e.g. typescript, python, rust, go)
Caller session identifier allowed at edge for grouping related client turns
Inbound trace detail level override for request lifecycle spans
Value in
- "off"
- "stages"
- "verbose"
Inbound stage-level trace emission override
Value in
- "off"
- "stages"
- "verbose"
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 }}