# Authentication

> Send a secret API key in the `Authorization` header of every request: `Authorization: Bearer fara_sk_...`. Keys are created on the website and act as their owner, with the `read` and/or `write` scope.

## At a glance

- Header: `Authorization: Bearer <key>` on every request
- Key format: `fara_sk_` + 46 letters and digits (54 characters). Regex: `^fara_sk_[0-9A-Za-z]{46}$`
- Where to create: https://faraatarjomeh.com/profile → **API access** tab (login required). The full key is shown once.
- Scopes: `read` (GET/HEAD) and `write` (POST, PUT, PATCH, DELETE)
- Limit: 10 active keys per account (revoked and expired keys do not count)
- Server-to-server only. Browsers cannot call the API with a key (cross-origin requests are refused).

## Create a key

1. Sign in at https://faraatarjomeh.com.
2. Open **Profile → API access → Create a key**.
3. Give it a name, choose scopes, and optionally an expiry (30, 90 or 365 days, or never).
4. Copy the key. It is shown only once; only a hash is stored.

Keep the key in an environment variable or a secret store. These docs use `FARA_API_KEY`.

```bash
export FARA_API_KEY="fara_sk_..."
curl -s https://faraatarjomeh.com/api/v1/account -H "Authorization: Bearer $FARA_API_KEY"
```

## Sign an agent in with a code (no key to copy)

An agent can get its own key without the user copying one: the OAuth 2.0 device flow ([RFC 8628](https://www.rfc-editor.org/rfc/rfc8628)). The user only needs an account with Fara.

1. The agent asks for a code. `client_name` is the name the user sees (names of known assistants are refused).

```bash
curl -s -X POST "https://faraatarjomeh.com/api/v1/oauth/device" -d "client_name=My site builder" -d "scope=read write"
```

Response `200 OK`:

```json
{
  "device_code": "Zx3…(secret, keep it)",
  "user_code": "BCDF-GHJK",
  "verification_uri": "https://faraatarjomeh.com/connect?device",
  "verification_uri_complete": "https://faraatarjomeh.com/connect?device=BCDF-GHJK",
  "expires_in": 900,
  "interval": 5
}
```

2. Show the user the code and `verification_uri_complete`. They open it, sign in or create an account, check the code, choose a monthly Fara limit (500 is suggested) and allow it.
3. Poll the token endpoint every `interval` seconds:

```bash
curl -s -X POST "https://faraatarjomeh.com/api/v1/oauth/token" \
  -d "grant_type=urn:ietf:params:oauth:grant-type:device_code" -d "device_code=$DEVICE_CODE"
```

Until the user acts the answer is `400` with `error`: `authorization_pending` (keep polling), `slow_down` (add 5 seconds to the interval), `access_denied` (the user said no: stop), or `expired_token` (start again). When the user allows it, the answer is `200`:

```json
{ "access_token": "fara_sk_…", "token_type": "Bearer", "expires_in": 31536000, "scope": "read write", "spend_limit": { "fara": 500, "period": "month" } }
```

`access_token` is an API key of the account, valid for a year, with the monthly limit the user chose. It is listed in the user's API tab, where they can change the limit or revoke it. Store it like any key. A request over the limit answers `402 SPEND_LIMIT_REACHED`.

Like a connected AI assistant, this key cannot manage webhooks, change account settings, delete anything, cancel jobs, or retry, recover or re-run jobs (`403 CONNECTION_NOT_ALLOWED`); the account owner does those on the website. It stops working when the account's password is changed.

AI assistants that speak MCP do not need this: they connect to `https://faraatarjomeh.com/api/v1/mcp` and sign in through the browser (see [AI assistants (MCP)](https://faraatarjomeh.com/api/v1/docs/guides/mcp.md)).

## Scopes

| Scope | Allows | Typical use |
|---|---|---|
| `read` | GET and HEAD: see jobs, balance, ledger, download results | dashboards, reporting |
| `write` | POST, PUT, PATCH, DELETE: create, change, approve, cancel jobs — **spends Fara** | integrations that start work |

A request without the needed scope gets `403` with code `INSUFFICIENT_SCOPE` and `requiredScope` in the body. Most integrations need both scopes.

## What a key can reach

A key can call every customer endpoint in [the reference](https://faraatarjomeh.com/api/v1/docs/reference.md). It cannot:

- call admin endpoints (a key never has admin rights, even if its owner is an admin);
- sign in, change the password or profile, or buy Fara;
- create, list or revoke API keys.

Those return `403` with code `API_KEY_ROUTE_NOT_ALLOWED`.

## Authentication errors

| HTTP | code | Meaning | What to do |
|---|---|---|---|
| 401 | `INVALID_API_KEY` | Missing, malformed, revoked or unknown key | Check the header format and the key. If revoked, ask the owner for a new key. |
| 401 | `API_KEY_EXPIRED` | The key's expiry date passed | Ask the owner for a new key. |
| 403 | `ACCOUNT_DISABLED` | The owner's account is disabled | Contact support. |
| 403 | `INSUFFICIENT_SCOPE` | Key lacks `read` or `write` | Use a key with the scope in `requiredScope`. |
| 403 | `API_KEY_ROUTE_NOT_ALLOWED` | Endpoint is website-only | Do it on the website. |
| 403 | `EMAIL_NOT_VERIFIED` | Owner's email is not verified | The owner verifies it on the website. |
| 403 | `API_DISABLED` | API access is switched off right now | Retry later. |

A `401` response also carries `WWW-Authenticate: Bearer realm="api", error="invalid_token"`.

## Rotating a key

1. Create a new key on the website.
2. Deploy it to your server.
3. Revoke the old key (Profile → API access → Revoke). Revocation takes effect on the next request.

Idempotency keys are stored per account, not per API key, so a retry still works if you switch keys in the middle of it.

## Security

- Never put the key in a web page, a mobile app, a URL, a log, or a code repository.
- Use one key per integration so you can revoke one without stopping the others.
- If a key leaks, revoke it at once. Anyone with the key can spend the account's Fara.
- The key prefix `fara_sk_` is fixed so secret scanners can find leaked keys.
