# Authentication

> Send your key as a bearer token. Scopes decide what it can change, and the account's subscription decides whether it works.

Source: https://rivo.markets/docs/authentication

Send your API key in the `Authorization` header of every request. You create keys in [Settings](https://rivo.markets/settings?tab=developer), and every key starts with `rivo_live_`.

```
Authorization: Bearer rivo_live_...
```

## Scopes

| Scope | Allows |
| --- | --- |
| `read` | The key can call every `GET` route: trades, positions, moves, the stream, markets, traders, the paper account and bots. It can also call `POST /v2/backtest`, which reads history and changes nothing. |
| `write` | The key can do everything a `read` key can. It can also place paper orders and create, change and delete bots. |

You choose a key's scopes when you create it, and you cannot change them later. To add a scope, create a new key and revoke the old one. A v1 key with the `trade` scope works as a `write` key on v2.

## Subscription

A key works only while the account that created it has an active Rivo subscription. If the subscription lapses, the API answers every request with `403 SUBSCRIPTION_INACTIVE`. The same key starts working again as soon as the subscription is renewed, so you never need to replace it.

## Failures

| Status | Code | Means |
| --- | --- | --- |
| 401 | `UNAUTHORIZED` | The request has no `Authorization` header, or the key is wrong or revoked. |
| 403 | `SUBSCRIPTION_INACTIVE` | The key is valid, but its account has no active subscription. |
| 403 | `SCOPE_REQUIRED` | The route changes data, and the key does not have the `write` scope. |

## Streaming to a browser

Never send your API key to a browser. Anyone who opens the page can read it. Instead, your server opens the stream with the key and passes the frames on to the browser. The API accepts the key only in the `Authorization` header, never in the URL.

Your server (a Next.js route handler):

```typescript
// app/api/rivo-stream/route.ts
export async function GET(request: Request): Promise<Response> {
  // Check that the visitor is signed in to your app here.
  const upstream = await fetch(`https://api.rivo.markets/v2/stream${new URL(request.url).search}`, {
    headers: { Authorization: `Bearer ${process.env.RIVO_API_KEY}` },
    signal: request.signal,
  });
  return new Response(upstream.body, {
    status: upstream.status,
    headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache" },
  });
}
```

The browser:

```typescript
const events = new EventSource("/api/rivo-stream?channels=trades&venue=kalshi");
events.addEventListener("trade", (event) => {
  const trade = JSON.parse(event.data);
  console.log(trade.market.title, trade.outcome, trade.usd);
});
```

Each account can hold three streams open at once. If many visitors watch at the same time, open one stream on your server and send its frames to every visitor.

## Keeping keys safe

- Store keys in environment variables or a secrets manager. Never put a key in client-side code or a repository.
- Create one key for each app or agent, with the smallest scope it needs, so you can revoke one key without breaking the others.
- Revoke a key as soon as you think it may have leaked. Revoking takes effect immediately.
