Back to docs

Getting started

5-minute API quickstart with OpenAI SDK examples

Getting started

PrivateRouter is an OpenAI-compatible API gateway backed by self-hosted open models. If you can call the OpenAI API, you can call PrivateRouter — change two lines (base URL and key) and you're done.

This page gets you from zero to a working chat completion in under five minutes.

Step 1 — Get an API key

  1. Sign up at /signup (free tier — no card required).
  2. Open /keys in your dashboard.
  3. Click Create key, give it a label (e.g. local-dev), and copy the pr-… token. This is the only time the full key is shown. Paste it into your secret manager / .env immediately.

Keys are scoped to your account and inherit your plan's spend caps and rate limits.

Step 2 — Point your client at PrivateRouter

There are exactly two things to configure:

SettingValue
Base URLhttps://api.privaterouter.com/v1
API keypr-… (from step 1, as OPENAI_API_KEY)

Every OpenAI SDK we've tested honours base_url / OPENAI_BASE_URL. No fork or wrapper required.

Step 3 — Make your first call

curl

curl -fsS https://api.privaterouter.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "privaterouter/qwen-pro",
    "messages": [
      {"role": "user", "content": "Write a haiku about retries."}
    ]
  }'

Python

pip install openai
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.privaterouter.com/v1",
    api_key=os.environ["OPENAI_API_KEY"],
)

resp = client.chat.completions.create(
    model="privaterouter/qwen-pro",
    messages=[{"role": "user", "content": "Write a haiku about retries."}],
)
print(resp.choices[0].message.content)

Node / TypeScript

npm install openai
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.privaterouter.com/v1",
  apiKey: process.env.OPENAI_API_KEY,
});

const resp = await client.chat.completions.create({
  model: "privaterouter/qwen-pro",
  messages: [{ role: "user", content: "Write a haiku about retries." }],
});
console.log(resp.choices[0].message.content);

Migrating from OpenAI / OpenRouter

There are only two changes:

  1. Set base_url to https://api.privaterouter.com/v1.
  2. Set OPENAI_API_KEY to your PrivateRouter pr-… key.

Then swap any openai/gpt-* or openrouter/* model strings for the privaterouter/* equivalents below. Streaming, tool calls, and JSON mode all follow the OpenAI wire format unchanged.

Available models

ModelBest for
privaterouter/qwen-proGeneral chat, long context, reasoning
privaterouter/qwen-fastCheap & quick — drafts, classifiers
privaterouter/deepseek-codeCode generation, autocomplete
privaterouter/deepseek-reasonMulti-step reasoning, math
privaterouter/embedEmbeddings (/v1/embeddings)

Per-token rates and live availability are on the pricing page.

Streaming

Set stream: true. The response is a standard Server-Sent Events stream of data: {…chunk…} lines terminated by data: [DONE], exactly as OpenAI emits.

stream = client.chat.completions.create(
    model="privaterouter/qwen-pro",
    messages=[{"role": "user", "content": "Count to five slowly."}],
    stream=True,
)
for chunk in stream:
    delta = chunk.choices[0].delta.content or ""
    print(delta, end="", flush=True)

Rate limits

  • Burst limit: 60 requests per minute per API key on /v1/* endpoints. Exceeding it returns 429 Too Many Requests with a Retry-After header (seconds).
  • Spend caps: Each plan has a monthly USD ceiling. Hitting it returns 402 Payment Required; top up at /billing or upgrade your plan. See the billing doc for full details on overage behaviour and Stripe top-ups.

Troubleshooting

StatusMeaningWhat to do
401Missing or invalid API keyRe-check the Authorization: Bearer pr-… header.
402Insufficient credits / spend cap reachedTop up at /billing or upgrade your plan.
429Rate limit (60 RPM burst on /v1/*)Honour the Retry-After header; back off and retry.
503Requested model is temporarily unavailableRetry shortly, or fall back to privaterouter/qwen-fast.

Errors follow the OpenAI shape:

{
  "error": {
    "message": "You have exceeded your monthly spend cap.",
    "type": "insufficient_quota",
    "code": "spend_cap_reached"
  }
}

Next steps

  • API quickstart — full endpoint reference for /v1/chat/completions, /v1/completions, and /v1/embeddings.
  • Billing & Stripe — plans, top-ups, and how invoicing works.
  • Status — live health of the API, database, and GPU fleet.

Questions, bug reports, or stuck on something? Reach us at /contact.