# API keys and budgets

This page covers the API key that eigenkit and your HTTP calls send: how to create one, use it,
give it a spend budget, rename it and revoke it.

## Where keys are managed

You create, rename, budget and revoke keys in the router web app, on the Keys page at
https://router.eigenorbit.com/keys. An API key cannot manage keys itself. To create a key you need
to have accepted the current router terms.

## What a key looks like

A key is `eo_live_` followed by 43 characters from `A` to `Z`, `a` to `z`, `0` to `9`, `_` and `-`:
51 characters in total. Secret scanners can match keys with the pattern `eo_live_[A-Za-z0-9_-]{43}`.

The key is shown exactly once, when you create it. The router stores only a hash of it, its first
16 characters and its last four, so a lost key cannot be retrieved: revoke it and create a new one.

## Create a key

1. Open the Keys page and choose **Create key**.
2. Enter a name of 1 to 100 characters. Names are unique among your active keys, compared without
   regard to case.
3. Optionally turn on **Set a spend budget** and enter an amount in dollars.
4. Copy the key from the next screen. It is not shown again.

## Use a key

Every `/v1/jobs` call and the device calibration call send the key as `Authorization: Bearer <key>`.
The device list (`GET /v1/devices`) needs no key.

With eigenkit, put the key in `EIGENORBIT_ROUTER_API_KEY`. `RouterConfig` reads it from there when
`api_key=...` is not set:

```python
from eigenorbit_router_client import RouterConfig

# The API key is read from EIGENORBIT_ROUTER_API_KEY.
provider = RouterConfig(device="braket/sv1", shots=1000)
```

eigenkit never writes the key to a pickle or a saved model, and the key never appears in `repr`.

Over HTTP, send the key in the `Authorization` header. This call reads a job: `$JOB_ID` is the `id`
of a job you submitted, for example in the [Quickstart](/docs/quickstart).

```bash
curl https://router.eigenorbit.com/v1/jobs/$JOB_ID \
  --header 'EO-Protocol-Version: 1' \
  --header "Authorization: Bearer $EIGENORBIT_ROUTER_API_KEY"
```

Any problem with the key answers `401 unauthenticated` with the same message: no `Authorization`
header, a scheme other than `Bearer`, a malformed key, an unknown key or a revoked key. The answer
does not say which, by design.

## Rename a key

Open the key's menu on the Keys page and choose **Rename**. The new name follows the same rules as
at creation. A revoked key frees its name for a new key.

## Give a key a budget

A budget is a lifetime spend limit for one key. Set it when you create the key, or later with
**Edit budget** in the key's menu: **No limit**, or **Limit to** an amount in dollars.

The budget counts two things:

- what the key has spent: the charges of its settled jobs since the key was created;
- what it holds: the estimates of its jobs whose funds are still held, from submit until the job is
  settled or released.

A new job is refused with `402 key_budget_exceeded` when spent plus held plus the job's estimate
would exceed the budget. No job is created and nothing is held. An estimate reports the same code
in `would_reject_reason`.

Amounts on the wire are integers in micro-USD: 1000000 is $1. The error carries the four numbers:

```json
{
  "error": {
    "code": "key_budget_exceeded",
    "message": "This API key's budget does not cover the job. Raise or remove the key's budget in the router web app.",
    "details": {
      "budget_usd_micro": 1000000,
      "spent_usd_micro": 850000,
      "held_usd_micro": 90000,
      "estimate_usd_micro": 90000
    }
  }
}
```

Here the budget is 1000000 micro-USD ($1.00). The key spent 850000 ($0.85) and holds 90000
($0.09). Another job estimated at 90000 ($0.09) would bring the total to 1030000 ($1.03), so it is
refused. Raise or remove the budget, or wait for jobs in flight to settle.

More about budgets:

- The budget only refuses new jobs. A job already admitted is charged what it cost when it
  settles, even if that takes the key past its budget.
- A budget below what the key already spent is accepted. The key then admits no new job.
- IBM Quantum jobs carry no router charge and skip the budget.

## Revoke a key

Open the key's menu and choose **Revoke key**. Revoking cannot be undone. The key's next request
answers `401 unauthenticated`. Jobs already submitted with it run to the end and are charged as
usual. A revoked key stays on the Keys page with its spend; turn on **Hide revoked keys** to hide
it.

## Reference

- [Submit a job](/docs/api/jobs#submit-a-job)
- [Estimate a job](/docs/api/jobs#estimate-a-job)
- [Get device calibration](/docs/api/devices#get-device-calibration)
