# Authentication

Your server talks to payitgo with a **secret API key**: 64 hex characters,
a test key or a live key, sent with every request to the API under `/v1`.
That is all your integration ever needs. Your team runs everything else
from the dashboard at [payitgo.co](https://payitgo.co): the team itself,
keys and their limits, payout addresses, settings, balances and payouts,
each behind a person's sign-in and, where it matters, their password.

## API keys

### Sending the key

Send the key on every `/v1` request, in either header:

```
Authorization: Bearer 4f9c2a7e1b8d3f6a0c5e9b2d7a4f1c8e3b6d9a2f5c8e1b4d7a0f3c6e9b2d5a8f
```

```
X-API-Key: 4f9c2a7e1b8d3f6a0c5e9b2d7a4f1c8e3b6d9a2f5c8e1b4d7a0f3c6e9b2d5a8f
```

When both are present, `X-API-Key` wins. A complete request:

```bash
curl -s https://api.payitgo.co/v1/tokens \
  -H "Authorization: Bearer $PAYITGO_KEY"
```

A key is 64 hex characters. Treat it as an opaque string: it does not say
its mode.

### Test keys and live keys

The key decides the mode of everything the request does. There is no
`mode` parameter on `/v1`. You choose the mode when you create a key, and the
dashboard shows it as a Test or Live badge next to the key.

| Key | Mode | What it does |
|---|---|---|
| Test key | Test | Test payments, paid by the outcomes you [simulate](https://payitgo.co/docs/testing.md) (no real money), the **test** webhook and its signing secret |
| Live key | Live | Real payments on mainnet, the **live** webhook and its signing secret |

To ask payitgo which mode a key is in, call `GET /v1/webhook` with it: the
answer's `livemode` is the key's mode ([Webhooks](https://payitgo.co/docs/webhooks.md#read-the-webhook)).

- `GET /v1/tokens` lists the tokens of the key's mode.
- `POST /v1/payments` creates a payment in the key's mode.
- `GET /v1/payments` and `GET /v1/payments/{id}` only see payments of the
  key's mode. A test key gets `404` for a live payment, and the other way round.
- `/v1/webhook` reads and changes the webhook of the key's mode. Each mode has
  its own URL and its own signing secret ([Webhooks](https://payitgo.co/docs/webhooks.md)).

Every payment and every webhook notification carries `"livemode": true` or
`false`, so your code can always tell which mode it is looking at.

### Live mode is off until the owner goes live

No key works until the account owner has confirmed their email with the
link payitgo sent them. Until then every request answers `403 this account's email is not confirmed yet: …`.

A new account starts in test mode. Until the account owner goes live in the
dashboard (Developers › Go live: confirm their email, add a live payout
address, see a test payment through), every request with the live key is
refused:

```
HTTP/1.1 403 Forbidden

{"error": "live mode is off for this account: the owner can go live in the dashboard; until then use a test key"}
```

Pausing live payments
(Settings › Security) refuses the live key again and stops new live
payments; payments already open still complete.

### Storefront keys

Every key belongs to a **store**, one per shop you sell from (a website, an
app, a branch), created in the dashboard under Developers › API keys. Your
**main store** is your account itself, named after your business: its keys
see and do everything below. A key of another store (one of your other
shops) is a **storefront key**. It works like any other key, with these
differences:

- Every payment it creates carries the store's `store_id`.
- It only sees its own store's payments: `GET /v1/payments` lists that
  store's payments (its `store_id` filter is ignored), and
  `GET /v1/payments/{id}` answers `404 {"error": "payment not found"}` for
  another store's payment.
- It cannot read the account's events: `GET /v1/events` answers
  `403 {"error": "a storefront's key cannot read the account's events: use an account key"}`.
- It can read the webhook (`GET /v1/webhook`) and send a test
  (`POST /v1/webhook/test`), but it cannot change the account's webhook URL.
  `PUT` and `DELETE /v1/webhook` answer:

  ```
  HTTP/1.1 403 Forbidden

  {"error": "a storefront's key cannot change the account's webhook: set webhook_url on its payments instead"}
  ```

  A store that runs its own backend sets `webhook_url` on each payment it
  creates, and verifies the notifications with the same signing secret
  ([per-payment webhook URL](https://payitgo.co/docs/webhooks.md#per-payment-webhook-url)).

### Read-only keys

A key created with `"access": "read"` can only read: `GET /v1/payments`,
`GET /v1/payments/{id}`, `GET /v1/tokens`, `GET /v1/webhook`,
`GET /v1/events`, and the
[mass payouts](https://payitgo.co/docs/payouts.md)' batches, payments and payout wallet
(`GET /v1/payout-batches/{id}`, `GET /v1/payouts…` including the list of
payout requests, `GET /v1/payout-wallets`).
Anything else answers:

```
HTTP/1.1 403 Forbidden

{"error": "this api key is read-only: it can only read (GET)"}
```

Give one to a service that reports on payments (an accounting export, a
mobile app's order history) and should never create them.

### Payout keys

A key can pay your recipients from your payout wallet
([Mass payouts](https://payitgo.co/docs/payouts.md)) only once the account owner turns payouts
on for it in the dashboard (Developers › API keys), confirming with their
password and a two-factor code. Every key starts with it off. Without it, a key still reads
batches, payments and the payout wallet, but taking, sending or removing
payout requests, making a quote and a test top-up answer:

```
HTTP/1.1 403 Forbidden

{"error": "this api key cannot make payouts: turn payouts on for it in the dashboard"}
```

With the payout setting **Only IP-limited API keys pay out** on (Pay people ›
Settings), a payout key must also be limited to IP addresses
([below](#allowed-ip-addresses)), or it gets
`403 {"error": "your payout rules require keys limited to IP addresses: set this key's allowed IPs"}`.
A payout key can send the wallet's money to a batch's recipients only:
never anywhere else, and it never approves a batch.

### Allowed IP addresses

A key can be limited to your servers' addresses: up to 20 IP addresses or
CIDR ranges (`203.0.113.7`, `198.51.100.0/24`, IPv6
too). Such a key is refused from any other address, on every `/v1` route:

```
HTTP/1.1 403 Forbidden

{"error": "this api key is not accepted from 192.0.2.44: add it to the key's allowed IPs"}
```

The address in the message is the one payitgo saw your request come from.
A key with no allowed IPs is accepted from anywhere. Set them in the
dashboard (Developers › API keys): they are never changed through the API,
so a leaked key cannot lift its own limits.

### API key errors

| Status | Body | Cause |
|---|---|---|
| `401` | `{"error": "missing api key"}` | Neither `Authorization: Bearer …` nor `X-API-Key` was sent |
| `401` | `{"error": "invalid api key"}` | The key is unknown, mistyped or revoked |
| `403` | `{"error": "live mode is off for this account: …"}` | A live key while live mode is off |
| `403` | `{"error": "merchant account is suspended"}` | payitgo suspended the account |
| `403` | `{"error": "this api key is read-only: it can only read (GET)"}` | A [read-only key](#read-only-keys) used to create or change something |
| `403` | `{"error": "this api key is not accepted from 192.0.2.44: add it to the key's allowed IPs"}` | The key has [allowed IPs](#allowed-ip-addresses), and the request came from another address |
| `403` | `{"error": "this api key cannot make payouts: turn payouts on for it in the dashboard"}` | A [payout](https://payitgo.co/docs/payouts.md) write with a key whose payouts are off |
| `403` | `{"error": "your payout rules require keys limited to IP addresses: set this key's allowed IPs"}` | A payout write with a key without allowed IPs, while your payout settings require them |
| `429` | `{"error": "rate limit exceeded"}` | Too many requests with one key in a short time ([limits](https://payitgo.co/docs/errors.md#rate-limits)) |

## Managing keys

Owners and developers manage keys in the dashboard under **Developers › API
keys**: create one per environment or service, name it, make it read-only,
limit it to your servers' IP addresses, and revoke it the moment it is no
longer needed. Turning payouts on for a key takes the owner's password and
two-factor code.
Every change lands in your activity log, with who made it and from where.

A key's secret is shown when it is created, and your team can copy it again
from the same page with one click. A payout key is the exception: only an
owner can copy it, with their password and a two-factor code.

## Keep keys secret

- Use secret keys from your server only. Never put them in a browser, a
  mobile app, a public repository or a support ticket. The checkout routes
  your customers' browsers call need no key ([Checkout](https://payitgo.co/docs/checkout.md)).
- If a key may have leaked, revoke it at once. An API key cannot withdraw
  your balance (payouts need a signed-in team member with the `owner` or
  `finance` role), but it can create payments and change your webhook URL in
  your name. A key with `payouts` on can also pay recipients from your
  [payout wallet](https://payitgo.co/docs/payouts.md#the-payout-wallet): turn it on only for the
  key of the server that makes payouts, and limit that key to your servers'
  [IP addresses](#allowed-ip-addresses). With approvals in your payout
  settings (and **API batches skip approval** off), no batch leaves until a
  person approves it.
- A secret you lost can be copied again in the dashboard. One that may have
  leaked: create a new key, deploy it, and
  revoke the old one.
