# Private ticket flow

Optional advanced integration using short-lived, single-use inference tickets.

Source: https://docs.anonrouter.ai/private-ticket-flow

This optional flow separates your API-key identity from inference requests at the relay.
For ordinary SDKs and app integrations, start with [API-key access](https://docs.anonrouter.ai/quickstart);
you do not need to implement ticket handling.

## Before you begin

You need an AnonRouter inference API key. Create one in the dashboard, keep it on
a trusted server, and load it from an environment variable. Inference keys are
prefixed `ar_` and are shown only once at creation.

[Create an API key](https://anonrouter.ai/home/api-keys)

```bash
export ANONROUTER_API_KEY="ar_..."
export ANONROUTER_API_URL="https://api.anonrouter.ai/v1"
export ANONROUTER_CONTROL_URL="https://control.anonrouter.ai/v1"
```

> **Do not expose API keys in browsers**
>
> The examples below are for trusted server-side environments. A browser client
> should call your own backend, which holds the key.

## 1. Request a single-use ticket

The ticket binds the request to its model and output limit. Issue a new ticket
for every inference request. Tickets are valid for 30 seconds, so issue one and
send the completion promptly.

```bash
TICKET=$(curl -s "$ANONROUTER_CONTROL_URL/inference/tickets" \
  -H "Authorization: Bearer $ANONROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "meta-llama/llama-3.3-70b",
    "max_completion_tokens": 256
  }' | jq -r .ticket)
```

The response contains an opaque `ticket` string plus the constraints it was
bound to (`model`, `max_output_tokens`, `operation`, `privacy_class`,
`reasoning`, and `expires_in`).

## 2. Send the completion

Present the ticket to the relay in the `x-anonrouter-ticket` header. The relay
does not need your API key. The `model` and output limit must match the ticket.

```bash
curl "$ANONROUTER_API_URL/chat/completions" \
  -H "Content-Type: application/json" \
  -H "x-anonrouter-ticket: $TICKET" \
  -d '{
    "model": "meta-llama/llama-3.3-70b",
    "max_tokens": 256,
    "messages": [
      {
        "role": "user",
        "content": "Explain private model routing in one paragraph."
      }
    ]
  }'
```

The response is a standard OpenAI chat completion object. Headers report the
selection: `x-anonrouter-selected-model` and `x-anonrouter-routing`, plus
`x-anonrouter-provider`, `x-anonrouter-provider-attempts`,
`x-anonrouter-provider-fallback`, and `x-anonrouter-privacy-class` for the
provider that served it. See [response headers](https://docs.anonrouter.ai/api-reference#response-headers).

## With the OpenAI SDK

The OpenAI SDKs work against the same base URL. Issue the ticket with a plain
HTTP call, then pass it as an extra header on the completion. Use a non-secret placeholder for the OpenAI client's required key setting;
your actual key belongs only on the content-free ticket request. Disable
automatic retries because tickets are single-use and an ambiguous failure can
follow a paid generation.

```python
import os
import httpx
from openai import OpenAI

api_url = os.environ.get("ANONROUTER_API_URL", "https://api.anonrouter.ai/v1").rstrip("/")
control_url = os.environ.get(
    "ANONROUTER_CONTROL_URL",
    "https://control.anonrouter.ai/v1",
).rstrip("/")
api_key = os.environ["ANONROUTER_API_KEY"]

ticket_response = httpx.post(
    f"{control_url}/inference/tickets",
    headers={"Authorization": f"Bearer {api_key}"},
    json={
        "model": "meta-llama/llama-3.3-70b",
        "max_completion_tokens": 256,
    },
)
ticket_response.raise_for_status()
ticket = ticket_response.json()["ticket"]

# The real key was used only for the content-free ticket request. The OpenAI
# client requires a non-empty api_key, so use a non-secret placeholder here.
client = OpenAI(api_key="ticket-only", base_url=api_url, max_retries=0)

completion = client.chat.completions.create(
    model="meta-llama/llama-3.3-70b",
    max_tokens=256,
    messages=[{"role": "user", "content": "Hello from AnonRouter"}],
    extra_headers={"x-anonrouter-ticket": ticket},
)

print(completion.choices[0].message.content)
```

```typescript
import OpenAI from "openai";

const apiURL = process.env.ANONROUTER_API_URL ?? "https://api.anonrouter.ai/v1";
const controlURL =
  process.env.ANONROUTER_CONTROL_URL ?? "https://control.anonrouter.ai/v1";
const apiKey = process.env.ANONROUTER_API_KEY!;

const ticketRes = await fetch(`${controlURL}/inference/tickets`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "meta-llama/llama-3.3-70b",
    max_completion_tokens: 256,
  }),
});
if (!ticketRes.ok) throw new Error(`Ticket request failed: ${ticketRes.status}`);
const { ticket } = await ticketRes.json();

// The real key was used only for the content-free ticket request. The OpenAI
// client requires a non-empty apiKey, so use a non-secret placeholder here.
const client = new OpenAI({ apiKey: "ticket-only", baseURL: apiURL, maxRetries: 0 });

const completion = await client.chat.completions.create(
  {
    model: "meta-llama/llama-3.3-70b",
    max_tokens: 256,
    messages: [{ role: "user", content: "Hello from AnonRouter" }],
  },
  { headers: { "x-anonrouter-ticket": ticket } },
);

console.log(completion.choices[0].message.content);
```

## Next steps

* Read [authentication](https://docs.anonrouter.ai/authentication) to understand keys and tickets.
* Browse the [model catalog](https://docs.anonrouter.ai/models) and privacy labels.
* Use [`/auto` routing](https://docs.anonrouter.ai/routing) to let AnonRouter pick a model.
* Choose or pin the serving provider with [provider routing](https://docs.anonrouter.ai/provider-routing).
* Control [reasoning effort](https://docs.anonrouter.ai/reasoning) per request.
