# Check and activate license keys with the API
Use the license keys API to check a buyer's key, switch it on for a device, switch a device off, and revoke or reactivate keys.
When a product sells license keys, each unit sold gets its own key. Your software can ask Crate whether a key is good, and switch it on for a device. Each API key only sees its own store's license keys.
All paths start with `/api/v1/license_keys`. Send the API key as `Authorization: Bearer <key>`, and send fields as JSON or form data.
## Permissions
Create an API key with **Restricted** permissions, then tick a box on the **Licenses** row:
| Tick | The key can |
| --- | --- |
| **Read** | List keys, show one key, and check (validate) a key. |
| **Write** | Everything **Read** can, plus activate, deactivate, revoke and reactivate keys. |
> [!WARNING]
> Don't ship an API key inside software you give to buyers. Anyone can pull it out of the app. Call these endpoints from your own server, and have your app talk to that server.
## Endpoints
| Method and path | Needs | What it does |
| --- | --- | --- |
| `GET /license_keys` | Read | Lists keys. Filter with `product_id`, `status` or `email`. |
| `GET /license_keys/:id` | Read | Shows one key and its devices. |
| `POST /license_keys/validate` | Read | Checks a key. |
| `POST /license_keys/activate` | Write | Switches a key on for a device. |
| `POST /license_keys/deactivate` | Write | Switches a device off and frees its seat. |
| `POST /license_keys/:id/revoke` | Write | Withdraws a key. Send an optional `reason`. |
| `POST /license_keys/:id/reactivate` | Write | Brings back a revoked or expired key. |
Lists are paged. See [Pagination](/docs/developers/pagination).
## Check a key
```bash
curl -X POST https://<crate-address>/api/v1/license_keys/validate \
  -H "Authorization: Bearer $CRATE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"key": "ABCD-EFGH-JKLM-NPQR", "product_id": 31, "instance_id": "laptop-1"}'
```
| Field | Required | Meaning |
| --- | --- | --- |
| `key` | Yes | The buyer's key, as they typed it. Spaces at the ends are trimmed. |
| `product_id` | No | Only accept keys for this product. |
| `instance_id` | No | Also check that the key is switched on for this device. |
A known key answers `200`:
```json
{
  "valid": true, "code": "valid", "status": "issued",
  "expires_at": null, "activations": 1, "activation_limit": 3,
  "product_id": 31, "variant_id": 54,
  "activation": { "id": 501, "instance_id": "laptop-1", "instance_name": "Work laptop", "active": true, "…": "…" }
}
```
`code` tells you why a key isn't valid:
| `code` | Meaning |
| --- | --- |
| `valid` | The key can be used. |
| `revoked` | You withdrew the key. |
| `expired` | The key is past its end date. |
| `not_issued` | The key hasn't been sold to anyone. |
| `not_activated` | You sent an `instance_id`, and the key isn't switched on for that device. |
An unknown key, a key from another store, or a key for a different `product_id` answers `404` with `{ "errors": "Not Found" }`.
## Activate a key on a device
```bash
curl -X POST https://<crate-address>/api/v1/license_keys/activate \
  -H "Authorization: Bearer $CRATE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"key": "ABCD-EFGH-JKLM-NPQR", "instance_id": "laptop-1", "instance_name": "Work laptop"}'
```
| Field | Required | Meaning |
| --- | --- | --- |
| `key` | Yes | The buyer's key. |
| `instance_id` | Yes | Your id for the device, 1 to 200 characters. |
| `instance_name` | No | A name the buyer will recognise. |
| `metadata` | No | A JSON object under 4 KB. |
| `product_id` | No | Only accept keys for this product. |
| Status | `code` | Meaning |
| --- | --- | --- |
| `201` | `activated` | The device now holds a seat. |
| `200` | `already_activated` | The device already held a seat. Nothing changed. |
| `422` | `limit_reached` | Every seat is taken. The buyer must switch off another device first. |
| `422` | `revoked`, `expired` or `not_issued` | The key can't be used. |
Each device takes one seat of the key's `activation_limit`. A key with no limit can be switched on for any number of devices.
## Deactivate a device
Send the same `key` and `instance_id` to `POST /license_keys/deactivate`. A device that was switched on answers `200` with `"code": "deactivated"`, and its seat is free again. A device that wasn't switched on answers `422` with `"code": "not_activated"`.
## Revoke and reactivate
`POST /license_keys/:id/revoke` withdraws a key, and `POST /license_keys/:id/reactivate` brings it back. Both answer with the updated key. If the key is in the wrong state, they answer `422`:
- "can't be revoked from its current state"
- "only a revoked or expired key can be reactivated"
## Key format and states
Keys Crate creates look like `XXXX-XXXX-XXXX-XXXX` by default, and can have a prefix such as `PRO-`. Keys you uploaded are kept exactly as you gave them, so compare them as typed.
| `status` | Meaning |
| --- | --- |
| `issued` | Sold to a buyer. Valid until it expires. |
| `revoked` | Withdrawn by you. Not valid until reactivated. |
| `expired` | Past its end date. Not valid. |
| `available` | In an uploaded pool and not sold yet. The validate, activate and deactivate calls treat it as not found. |
## Limits and errors
- The validate, activate and deactivate calls allow 60 requests a minute for each license key. Over that, they answer `429` with "Too many requests for this key. Try again in a minute." See [Rate limits](/docs/developers/rate-limits).
- A request without `key` answers `400` with `{ "errors": { "key": ["is required"] } }`.
- A `status` filter that isn't one of `available`, `issued`, `revoked` or `expired` answers `422`.
To hear about activations, revocations and expiries as they happen, subscribe to the `license_key.*` [webhook events](/docs/developers/webhook-event-types).
