# Errors

Errors from `/api/v1` endpoints use a single JSON envelope, with a machine-readable `code` and a human-readable `message`.

## Error Format

```json
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "orderNumber is required",
    "details": {
      "field": "orderNumber"
    }
  }
}
```

`details` is optional. When present, its shape depends on the endpoint. Base your code on `code` and the HTTP status, not on the `message` text, which may change.

## Error Codes

| Code | HTTP Status | Description |
|------|-------------|-------------|
| `VALIDATION_ERROR` | 400, 422 | The request body or query parameters are invalid. `422` means the request was understood but refused by a data policy. |
| `UNAUTHORIZED` | 401 | Missing, invalid, revoked, or expired API key |
| `PAYMENT_REQUIRED` | 402 | The endpoint is billed per use, and the account has no payment method on file |
| `FORBIDDEN` | 403 | The API key lacks the scope this endpoint requires, or a purchase token was issued to a different key or order |
| `NOT_FOUND` | 404 | The resource doesn't exist or belongs to another account, or no endpoint matches the path |
| `CONFLICT` | 409 | The operation conflicts with the resource's current state |
| `RATE_LIMITED` | 429 | Rate limit exceeded; see [Rate Limiting](rate-limiting.md) |
| `UPSTREAM_ERROR` | — | Used per item in batch responses when a provider lookup fails for that item |
| `INTERNAL_ERROR` | 500, 502 | Unexpected server error (`500`), or a carrier or provider was unavailable (`502`) |

ShipWave returns `404`, not `403`, for a resource in another account, so a response never reveals whether an ID exists elsewhere.

Calling an existing endpoint with an HTTP method it doesn't support returns `405 Method Not Allowed` with an empty body.

## Common Scenarios

### Invalid or Missing API Key (401)

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

**Fix:** Send an `Authorization: Bearer <key>` header with an active, unexpired key.

### Missing Scope (403)

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

**Fix:** Use a key with the needed access. See [Authentication](authentication.md) for the scope each endpoint needs.

### Resource Not Found (404)

```json
{
  "error": {
    "code": "NOT_FOUND",
    "message": "Order not found"
  }
}
```

**Fix:** Check the ID, and that the resource belongs to the account that owns your key.

### Validation Error (400)

```json
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "orderNumber is required"
  }
}
```

**Fix:** Check the request against the endpoint's required fields.

### Conflict (409)

Returned when an operation can't proceed because of the resource's current state.

```json
{
  "error": {
    "code": "CONFLICT",
    "message": "Cannot cancel order with active shipments. Void the labels first."
  }
}
```

**Fix:** Resolve the conflicting state first. For example, void the active label before cancelling the order.

### Rate Limited (429)

```json
{
  "error": {
    "code": "RATE_LIMITED",
    "message": "Rate limit exceeded. Try again later."
  }
}
```

**Fix:** Wait the number of seconds in the `Retry-After` header, then retry. See [Rate Limiting](rate-limiting.md).

### Internal Error (500 / 502)

```json
{
  "error": {
    "code": "INTERNAL_ERROR",
    "message": "Internal server error"
  }
}
```

**Fix:** Retry with backoff. For writes, retry with the same idempotency key, or check the resource's state before trying again. If the error keeps happening, contact support@shipwave.app.
