# Authentication

All API requests require a valid API key passed as a Bearer token.

## API Key Format

Keys start with `sw_live_` followed by a random string:

```
sw_live_a3Bf9kLm2nPq7rSt...
```

There are no separate test keys. Every key works against your live account. See [Testing](testing.md) for how to build an integration without buying labels.

## Creating API Keys

Workspace owners create keys in the ShipWave dashboard under **Settings → API Keys**. Give each key a name, then choose its access:

| Access option | What it allows | Scopes granted |
|---------------|----------------|----------------|
| **Shipping API** | Read and write orders, stores, rates, labels, shipments, tracking, returns, products, warehouses, and webhooks | `read:*`, `write:*` |
| **Address verification** | The [address verification API](addresses.md) (`/api/v1/addresses/*`) | `addresses:verify` |
| **AI label checkout** | Prepare hosted label checkouts and read their status. It cannot buy labels. See [AI label checkout](/docs/ai-label-checkout). | `parcel-checkouts:create`, `parcel-checkouts:status` |

A key can have more than one kind of access. **Shipping API** access does not include address verification; select both options if you need both.

> **Important:** The full key is shown only once, when you create it. Store it securely. ShipWave keeps only a hash and a short prefix (for example `sw_live_a3Bf`) so you can tell your keys apart.

## Using Your API Key

Pass the key in the `Authorization` header:

```bash
curl https://shipwave.app/api/v1/orders \
  -H "Authorization: Bearer sw_live_a3Bf9kLm2nPq7rSt..."
```

```typescript
const res = await fetch("https://shipwave.app/api/v1/orders", {
  headers: {
    Authorization: "Bearer sw_live_a3Bf9kLm2nPq7rSt...",
  },
});
```

Always call the API from your server. Never put a key in browser or mobile app code.

## Scopes

Each endpoint requires a scope. It depends on the resource and the HTTP method: `GET` and `HEAD` need the read scope, and `POST`, `PUT`, and `DELETE` need the write scope.

| Resource | Read scope | Write scope |
|----------|-----------|-------------|
| `/orders` (including `/orders/:id/rates` and `/orders/:id/labels`) | `read:orders` | `write:orders` |
| `/stores` | `read:orders` | — |
| `/shipments` | `read:shipments` | `write:shipments` |
| `/tracking` | `read:shipments` | — |
| `/returns` | `read:returns` | `write:returns` |
| `/products` | `read:inventory` | `write:inventory` |
| `/warehouses` | `read:warehouses` | `write:warehouses` |
| `/webhooks` | `read:*` | `write:*` |
| `/addresses` | `addresses:verify` | `addresses:verify` |

A **Shipping API** key holds `read:*` and `write:*`, which satisfy every `read:` and `write:` scope above. Getting rates (`POST /orders/:id/rates`) is a write call, so it needs `write:orders`.

If your key lacks the scope an endpoint needs, you get `403 FORBIDDEN`:

```json
{
  "error": {
    "code": "FORBIDDEN",
    "message": "API key lacks required scope \"write:orders\""
  }
}
```

## Key Expiration and Rotation

Every key expires **one year after it is created**. The expiry date is shown next to the key in **Settings → API Keys**. ShipWave does not send a reminder before a key expires, so plan a rotation before that date:

1. Create a new key.
2. Deploy it to your integration.
3. Revoke the old key.

Keys can overlap, so you can rotate without downtime.

An expired key returns the same `401 UNAUTHORIZED` response as a missing or invalid key:

```json
{
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Invalid or missing API key"
  }
}
```

## Revoking Keys

Revoke a key under **Settings → API Keys**. It stops working immediately and returns `401 UNAUTHORIZED`.

## Errors

| Status | Code | Cause |
|--------|------|-------|
| 401 | `UNAUTHORIZED` | Missing, invalid, revoked, or expired key |
| 403 | `FORBIDDEN` | The key lacks the scope this endpoint requires |
