# Authentication

> How merchant API keys work, their format and abilities, how to rotate and revoke them, and which endpoints need which ability.

Every API request needs a bearer token in the `Authorization` header:

```http
GET /api/v1/me HTTP/1.1
Host: northechpay.test
Authorization: Bearer 1|ntp_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Accept: application/json
```

There are two kinds of token:

| Token            | Who uses it                                 | How to get it                              |
| ---------------- | ------------------------------------------- | ------------------------------------------ |
| Merchant API key | Your website or backend, server to server   | Dashboard, **Developers → API keys**       |
| User token       | First-party mobile apps signing in a person | `POST /auth/token` with email and password |

A merchant website should always use an API key. This page is mostly about API keys.

## API key format

A key looks like this:

```text
{id}|ntp_test_{48 characters}
1|ntp_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

- Send the whole string, including the number and the `|`.
- The secret is shown once, when you create or rotate the key. NorthechPay stores only a SHA-256 hash of it.
- The dashboard shows the last 6 characters (`key_hint`) so you can tell keys apart.
- Every key is a test key (`environment: "test"`).
- A key belongs to one merchant. It can never read another merchant's data. IDs from another merchant return `404 not_found`.
- A key never exposes Moov credentials.

## Create a key

In **Developers → API keys**, choose:

| Field     | Rules                                                                 |
| --------- | --------------------------------------------------------------------- |
| Name      | Required, up to 64 characters. Name it after the system that uses it. |
| Abilities | At least one, from the list below.                                    |
| Expiry    | 30, 90, 180 or 365 days, or no expiry.                                |

## Abilities

Each key carries only the abilities you grant. A request without the needed ability fails with `403 insufficient_scope` and a message such as `This credential is missing the [refunds:write] ability.`

| Ability               | Allows                                         | API keys                 |
| --------------------- | ---------------------------------------------- | ------------------------ |
| `merchant:read`       | View the business profile                      | Yes                      |
| `checkouts:read`      | View checkouts and payment links               | Yes                      |
| `checkouts:write`     | Create and disable checkouts and payment links | Yes                      |
| `transactions:read`   | View transactions                              | Yes                      |
| `refunds:write`       | Issue refunds                                  | Yes                      |
| `wallets:read`        | View wallet balances                           | Yes                      |
| `bank-accounts:read`  | View bank accounts                             | Yes                      |
| `payouts:read`        | View payouts                                   | Yes                      |
| `payouts:write`       | Start payouts                                  | Yes                      |
| `disputes:read`       | View disputes and evidence                     | Yes                      |
| `disputes:respond`    | Upload and submit dispute evidence             | Yes                      |
| `merchant:manage`     | Manage business onboarding                     | No, signed-in users only |
| `bank-accounts:write` | Link and verify bank accounts                  | No, signed-in users only |
| `disputes:accept`     | Accept liability for a dispute                 | No, signed-in users only |
| `developers:manage`   | Manage API keys and webhooks                   | No, dashboard only       |

Grant the fewest abilities that work:

| Integration                       | Abilities                                                                              |
| --------------------------------- | -------------------------------------------------------------------------------------- |
| Website checkout                  | `merchant:read`, `checkouts:read`, `checkouts:write`, `transactions:read`              |
| Add refunds from your back office | The above plus `refunds:write`                                                         |
| Read-only reporting               | `merchant:read`, `checkouts:read`, `transactions:read`, `wallets:read`, `payouts:read` |
| AI agent or MCP client            | `merchant:read`, `checkouts:read` (add `checkouts:write` only if needed)               |

> **Warning:** `payouts:write` and `refunds:write` move money out of the merchant wallet. Do not grant them to a key that lives on a public website server unless that server really needs them.

## Which endpoints need which ability

Some endpoints check two abilities: the read ability to find the record, then the write ability to change it.

| Endpoint                                                                                           | Abilities needed                                   |
| -------------------------------------------------------------------------------------------------- | -------------------------------------------------- |
| `GET /me`                                                                                          | Any valid token                                    |
| `GET /merchant`, `POST /merchant/refresh`                                                          | `merchant:read`                                    |
| `GET /merchant/checkouts`, `GET /merchant/checkouts/{id}`, `POST /merchant/checkouts/{id}/refresh` | `checkouts:read`                                   |
| `POST /merchant/checkouts`, `POST /merchant/payment-links`                                         | `checkouts:write`                                  |
| `DELETE /merchant/checkouts/{id}`, `DELETE /merchant/payment-links/{id}`                           | `checkouts:read` and `checkouts:write`             |
| `GET /merchant/payment-links`, `GET /merchant/payment-links/{id}`, `GET …/qr-code`                 | `checkouts:read`                                   |
| `GET /merchant/transactions`, `GET /merchant/transactions/{id}`                                    | `transactions:read`                                |
| `POST /merchant/transactions/{id}/refunds`                                                         | `transactions:read` and `refunds:write`            |
| `GET /merchant/disputes`, `GET /merchant/disputes/{id}`, `GET …/evidence`, `POST …/refresh`        | `disputes:read`                                    |
| `POST …/evidence`, `DELETE …/evidence/{evidence}`, `POST …/submit`                                 | `disputes:read` and `disputes:respond`             |
| `POST /merchant/disputes/{id}/accept`                                                              | `disputes:read` and `disputes:accept` (users only) |
| `GET /merchant/wallets`, `GET /merchant/wallets/{id}/transactions`                                 | `wallets:read`                                     |
| `GET /merchant/bank-accounts`                                                                      | `bank-accounts:read`                               |
| `POST /merchant/bank-accounts`, micro-deposit endpoints                                            | `bank-accounts:write` (users only)                 |
| `GET /merchant/payouts`, `GET /merchant/payouts/{id}`, `POST …/refresh`                            | `payouts:read`                                     |
| `POST /merchant/payouts`                                                                           | `payouts:write`                                    |
| Onboarding endpoints (`PATCH /merchant`, `/merchant/submit`, `/merchant/onboarding/*`, …)          | `merchant:manage` (users only)                     |

## Check a key

`GET /me` returns the key's merchant and its effective abilities.

```json
{
    "data": {
        "object": "api_key",
        "id": "01a2083c-1f4e-7b9a-8c2d-5e6f7a8b9c0d",
        "name": "Website integration",
        "environment": "test",
        "abilities": [
            "merchant:read",
            "checkouts:read",
            "checkouts:write",
            "transactions:read"
        ],
        "merchant": {
            "id": "…",
            "object": "merchant",
            "name": "…",
            "status": "active"
        }
    }
}
```

Call it at deploy time to fail fast if a key is wrong or missing an ability.

## Rotate, revoke and expire

| Action | Effect                                                                          |
| ------ | ------------------------------------------------------------------------------- |
| Rotate | Issues a new secret for the same key. Every older secret stops working at once. |
| Revoke | The key stops working at once. It cannot be restored.                           |
| Expire | After the expiry date the key stops working.                                    |

A revoked, expired or unknown key gets `401 unauthenticated`. Rotation has no overlap window, so deploy the new secret right after you rotate.

## Request IDs

Every response has an `X-Request-Id` header. You may send your own `X-Request-Id` (8 to 64 characters from `A-Z a-z 0-9 . _ -`) and it is echoed back. Otherwise NorthechPay generates one. Error bodies repeat it as `error.request_id`. Log it, and quote it when you ask for help.

## User tokens (first-party mobile apps)

Mobile apps that sign in a person use `POST /auth/token`:

```bash
curl -sS -X POST "$NORTHECHPAY_API_BASE/auth/token" \
  -H "Accept: application/json" -H "Content-Type: application/json" \
  -d '{"email":"owner@example.com","password":"your-password","device_name":"Test phone"}'
```

| Field             | Rules                                            |
| ----------------- | ------------------------------------------------ |
| `email`           | Required                                         |
| `password`        | Required                                         |
| `device_name`     | Required, up to 100 characters                   |
| `two_factor_code` | Required when the user has two-factor sign-in on |

The response is `201` with `token_type`, `access_token` and `expires_at` (30 days). The token acts with the user's role in the merchant. If the user belongs to several merchants, send `X-Merchant-ID: {merchant id}`. `DELETE /auth/token` signs the app out: it revokes only the mobile token that makes the call. The user's other devices and all API keys keep working. Sign-in is limited to 10 attempts per minute per IP address.

> **Note:** `DELETE /auth/token` never revokes an API key. Called with an API key (or a browser session) it returns `403` with `error.code` `logout_not_supported` and nothing changes. To revoke or rotate an API key, use **Developers → API keys**.

> **Warning:** Never ask a merchant's staff to type their NorthechPay password into your website. Websites use API keys.
