# Authentication

Source: https://amanucci.com/docs/developers/authentication

> How to authenticate to the Amanucci API with an API key or a session, and how the x-amanucci-hat header selects which of your seats a request acts in.

Every request to `/v1` carries one credential, and every request into a firm's or a business's books names one **hat**.

## API keys

An API key belongs to one person and acts as that person, with exactly their access. Keys start with `amanucci_sk_`.

Send it in the `x-api-key` header:

```bash
curl https://api.amanucci.com/v1/me \
  -H "x-api-key: $AMANUCCI_API_KEY"
```

Create, list and revoke your own keys from the CLI, signed in with a session:

```bash
am keys create --input key-name.json --json   # {"name":"Build server"}
am keys list --json
am keys revoke --input key-id.json --json     # {"keyId":"..."}
```

Creating a key also needs a fresh step-up: unless you stepped up in the last 15 minutes, the CLI emails you a code and asks for it in the terminal. The key's secret is shown once, at creation, and is not saved anywhere by the CLI. Store it yourself.

## Sessions

The web app uses a session cookie. The CLI can hold a session too: `am login` asks for your email, sends you a one-time code and verifies it in the terminal. A session is required to manage keys, and a fresh step-up to create one or to perform a signed act.

If a request carries both an API key and a session cookie, the key wins. To act with your session, leave `x-api-key` out.

## The hat: `x-amanucci-hat`

One person can hold several seats: an accountant at a firm who also owns a business, for example. The hat says which seat a request is made in. Its value is one of:

| Hat | Means |
| --- | --- |
| `firm:<firmId>` | your seat at that accounting firm |
| `organisation:<organisationId>` | your seat at that business |

```bash
curl "https://api.amanucci.com/v1/entities/$ENTITY_ID/documents?limit=50" \
  -H "x-api-key: $AMANUCCI_API_KEY" \
  -H "x-amanucci-hat: organisation:$ORGANISATION_ID"
```

Find your seats and their ids with `GET /v1/me` (in `firms[].id` and `organisations[].id`), or `am workspace inspect self --json`.

Rules:

- Hats never add up. A request has the access of that one seat and nothing more, and a refusal never falls through to another seat.
- A request that needs a hat and sends none, or sends a malformed one, is refused with `422 invalid-input`. The hat is never inferred.
- A hat for a seat you do not hold is refused with `403 seat-required`.
- A few operations about yourself, such as `GET /v1/me`, take no hat.

## Signed acts and step-up

Some acts are a person's legal act: submitting a filing, posting an entry by hand, signing a period close, reopening a period. They require:

1. a **session**, never an API key, and
2. a **fresh step-up**: a one-time code sent to your email, valid for 15 minutes.

Without it the API answers `403 step-up-required`, with instructions in the body. From the terminal:

```bash
am login
am step-up
am filings sign --input submit.json --json
```

The MCP server lists signed tasks but never runs them; it tells the agent to hand them to a person.

## Environment variables

The CLI and the MCP server read:

| Variable | Use |
| --- | --- |
| `AMANUCCI_BASE_URL` | the API origin, such as `https://api.amanucci.com` (or `--base-url`) |
| `AMANUCCI_API_KEY` | an API key |
| `AMANUCCI_SESSION_COOKIE` | an existing session cookie (CLI only) |
| `AMANUCCI_HAT` | the hat, `firm:<id>` or `organisation:<id>` |
| `AMANUCCI_CONFIG_DIR` | where the CLI stores saved sessions (default `~/.config/amanucci`) |

The CLI never reads a `.env` file.
