Skip to main content

Documentation Index

Fetch the complete documentation index at: https://narev.ai/docs/llms.txt

Use this file to discover all available pages before exploring further.

Endpoint

POST /api/router/{router_id}/v1/chat/completions

Authentication

Include your Narev API key in the Authorization header:
Authorization: Bearer YOUR_API_KEY
You can generate API keys in the Narev Cloud dashboard under Settings → API Keys.

Setup

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://narev.ai/api/router/{router_id}/v1"
)

Request parameters

Required

messages
array
required
Array of message objects, each with a role (system, user, or assistant) and content string.

Optional

stream
boolean
default:"false"
When true, Narev streams the response as server-sent events (SSE).
metadata
object
Custom key-value pairs used by routing filters to match the request to the correct A/B test.
The Router API ignores model, temperature, top_p, max_tokens, and other generation parameters. The production variant of the matched A/B test controls these values, not the request.

Routing logic

You configure routing rules in the Narev Cloud dashboard. When a request arrives, the router evaluates the filters against the request’s messages and metadata, then forwards the request to the production variant of the matched A/B test. Use metadata to pass contextual signals that your routing filters depend on:
response = client.chat.completions.create(
    messages=[
        {"role": "user", "content": "Analyze this code"}
    ],
    extra_body={
        "metadata": {
            "user_tier": "premium",
            "task_type": "code_review",
            "complexity": "high"
        }
    }
)

Request examples

Basic request

response = client.chat.completions.create(
    messages=[
        {"role": "user", "content": "What is the capital of France?"}
    ]
)

With system prompt

response = client.chat.completions.create(
    messages=[
        {"role": "system", "content": "You are a helpful geography expert."},
        {"role": "user", "content": "What is the capital of France?"}
    ]
)

Streaming

stream = client.chat.completions.create(
    messages=[
        {"role": "user", "content": "Tell me a story."}
    ],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Response format

Non-streaming

The model field reflects the model used by the matched A/B test’s production variant.
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "openai:gpt-4",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Paris is the capital of France."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 13,
    "completion_tokens": 7,
    "total_tokens": 20
  }
}

Streaming

Narev sends each token as a server-sent event (SSE) with a data: prefix:
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"openai:gpt-4","choices":[{"index":0,"delta":{"content":"Paris"},"finish_reason":null}]}

data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"openai:gpt-4","choices":[{"index":0,"delta":{"content":" is"},"finish_reason":null}]}

data: [DONE]

Error responses

All errors return a JSON object with an error field:
{
  "error": {
    "message": "Error description",
    "code": "error_code"
  }
}
StatusCodeDescription
400bad_requestInvalid request format or parameters
400no_filters_configuredRouter has no filters configured
400no_production_variantMatched A/B test has no production variant
401invalid_api_keyInvalid or missing API key
402insufficient_creditsInsufficient credits to complete the request
404router_not_foundRouter ID not found
500internal_errorInternal server error