# Multi-use license keys (/docs/license-keys)

A `generated` license service makes a new, random license key for every sale of a product. Each key is a multi-use license key: it works on as many devices at once as you allow, and no more. Comecero keeps each key up to date: a subscription renewal extends it, a refund or chargeback revokes it, and a partial refund lowers the number of devices it works on. Your app activates the key on a device and checks it with three routes on the usage meter. You don't run a license server or upload keys.

It is the third kind of [license service](/docs/basic-concepts/license-services), next to `list` (keys you upload) and `remote_url` (your own license server). Those two only hand a key to the buyer; Comecero does not check them later.

<Callout>
Comecero turns license keys on for each account. Until it is on, creating a `generated` service, or changing a service to `generated`, answers `409` with the error code `feature_not_enabled`. Ask Comecero support to turn it on. If it is turned off later, services and keys you already have keep working.
</Callout>

## What a key looks like [#what-a-key-looks-like]

A key is 25 random letters and digits in five groups, after your optional prefix:

```text
ACME-7K2QF-M9X4T-0BHRW-C3NDP-YV8JE
```

Keys use letters and digits that are hard to misread (no I, L, O or U). Case, spaces and dashes don't matter when a customer types a key, and an `O`, `I` or `L` they type is read as `0`, `1` or `1`.

## Create a generated license service [#create-a-generated-license-service]

Call [Create a license service](/api/license-services/create-license-service) with `type` set to `generated`. Every setting in `configuration` is optional.

```bash title="Request"
curl -X POST "https://api.comecero.com/api/v1/license_services" \
  -H "Authorization: Bearer $COMECERO_TEST_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Widget Pro keys",
    "label": "License Key",
    "type": "generated",
    "instructions": "Open Widget Pro, choose Help, then Enter License, and paste the key.",
    "configuration": {
      "prefix": "ACME",
      "activation_limit": 2,
      "duration": { "value": 1, "unit": "year" },
      "subscription_grace_days": 3,
      "revoke_on_refund": true,
      "revoke_on_chargeback": true
    }
  }'
```

| Setting | Default | What it does |
| --- | --- | --- |
| `prefix` | none | Up to 8 letters or digits put in front of every key. Saved in upper case. |
| `activation_limit` | unlimited | Devices a key can be active on at once, for each unit bought. 1 to 100, or `null` for unlimited. |
| `duration` | never expires | How long a key works from the purchase: `value` 1 to 1000 and `unit` `day`, `month` or `year`. `null` for keys that never expire. Not used for subscriptions. |
| `subscription_grace_days` | 3 | For subscriptions: days a key keeps working after the end of the last paid period. 0 to 30. |
| `revoke_on_refund` | `true` | A full refund revokes the key. See [Refunds and chargebacks](#refunds-and-chargebacks). |
| `revoke_on_chargeback` | `true` | A chargeback revokes the key. |
| `over_limit` | `block` | Only `block` or `null` is accepted. A full key refuses a new device. `rotate` is planned and answers `400` on create and update. See [When a key is full](#when-a-key-is-full). |

Then set the product's `license_service_id` to the new service with [Update a product](/api/products/update-product). One service can serve many products.

To change the settings later, call [Update a license service](/api/license-services/update-license-service) with only the fields you want to change; `null` clears `prefix`, `activation_limit` or `duration`, and resets `subscription_grace_days`, `revoke_on_refund` or `revoke_on_chargeback` to its default. `prefix`, `activation_limit`, `duration` and `subscription_grace_days` apply to keys sold from then on: a key keeps the devices per unit and the grace days it was sold with, even on renewals. `revoke_on_refund` and `revoke_on_chargeback` apply to every refund and chargeback handled after the change, on existing keys too.

A value outside these rules answers `400` with the error code `invalid_input`, and the message names the setting, for example `configuration.activation_limit must be a whole number from 1 to 100, or null for unlimited.`

You can't create a key of a `generated` service by hand with [Create a license](/api/licenses/create-license): it answers `400`. Comecero makes them when the order is paid.

## Find issued keys [#find-issued-keys]

[List licenses](/api/licenses/list-licenses) uses `q` as an exact license id filter. An unknown id returns an empty list. Use `after_item` or `before_item` with `limit` for cursor paging, and `desc=true` for newest first. The cursor itself is excluded from the page.

## What the buyer gets [#what-the-buyer-gets]

The key is made while the payment is recorded, so it is on the order from the first moment. Each order item gets one key. In the API it is a [license](/api/licenses/get-license) whose `text` is the key. A generated key also has `status`, `activation_limit`, `expires_at`, `key_last4`, `customer_id`, `subscription_id` and `source` (`generated`). The [`order:licenses_assigned`](/api/webhooks/order-licenses-assigned) webhook fires as for any license service, and [`license:issued`](/api/webhooks/license-issued) fires for the key.

The key reaches the usage meter within seconds, so the buyer can activate it straight from the receipt. If making a key fails while the payment is recorded, Comecero tries again in the background and adds the key to the order when it is made.

Test mode works the same: a test purchase makes a test key, and the usage meter answers for it with `"test": true`.

## Where shoppers see their keys [#where-shoppers-see-their-keys]

Your customers find their keys in these places, each with the service's `label` and `instructions`. On a page, a key that is not revoked has a button to copy it.

| Where | What the customer sees |
| --- | --- |
| Receipt page, after paying (store and pop-up checkout) | The key for each item they just bought. |
| Order page in their [customer account](/docs/integrations/customer-account) (`/account/orders/{id}`) | The keys of that order. |
| Licenses page in their customer account (`/account/licenses`) | Every key from all their orders, newest order first, with the order each came from. |
| Receipt email | The key of each item, generated keys included. |

On the receipt page and in the customer account, a key Comecero generated also shows whether it is active, expired or revoked, when it expires (or expired, or was revoked), and how many devices it works on at a time. The receipt email has the key only: not its status, expiry or device limit, which can change after the email is sent. Point customers to their account for the current state.

A key that is still being made shows "Your license key will be emailed to you shortly." until it is ready. Keys from `list` and `remote_url` services show the same way, without status, expiry or devices.

## Seats [#seats]

A key's `activation_limit` is the service's `activation_limit` times the quantity bought. With `activation_limit` 2, a buyer of 3 units gets one key that works on 6 devices at once. A part quantity counts as a whole unit. With no limit on the service, the key is unlimited.

A device counts against the limit from when your app activates it until your app deactivates it. To move to a new computer, the customer deactivates the old one first, or you offer a way to do it in your app.

### When a key is full [#when-a-key-is-full]

Today an activation on a key with every seat taken answers `403` with the error `activation_limit_reached`, and the devices already active keep working.

Set `configuration.over_limit` to `block`, leave it out, or send `null`. All three use the same blocking behavior. `rotate` is planned and is refused with `400` (`invalid_input`, reference `LkG01cf`) on both create and update. No active device is removed to make room for another.

## Subscriptions [#subscriptions]

When the product is sold with a subscription, the subscription has one key for its whole life:

- **First order.** The key works until the end of the first paid period, plus `subscription_grace_days`. The service's `duration` is not used.
- **Each renewal** extends the same key to the end of the period it paid for, plus the grace days. The seats follow the renewal's quantity. The customer keeps the same key and their devices stay active. [`license:extended`](/api/webhooks/license-extended) fires.
- **A missed or failed renewal** doesn't revoke the key. It keeps working for the grace days after the paid period ends, then expires. If the customer pays later, the key is extended again.
- **A cancelled subscription** gets no more renewals, so the key expires at the end of the last paid period plus the grace days.

Paying an older invoice late never shortens a key or moves it past a period that is not paid.

## Refunds and chargebacks [#refunds-and-chargebacks]

Comecero works out each key again every time a refund or chargeback completes, or is reversed:

| What happens | What it does to the key |
| --- | --- |
| Full refund of the order item (all its units, or the whole order) | Revoked, `revoked_reason` `refund`. |
| Partial refund by quantity | Seats lowered to the units still paid for. Devices already active stay active, but no new ones can be activated until there are fewer than the new limit. |
| Partial refund by amount | Nothing changes. |
| Chargeback on any payment of the order | Every key the order bought is revoked, `revoked_reason` `chargeback`. |
| Full refund or chargeback of a subscription renewal | The time that renewal paid for is taken back: `expires_at` goes back to the end of the latest period still paid, plus the grace days, and the seats follow that period's quantity. The key is not revoked. |
| Partial refund of a subscription renewal by quantity | That period's seats are lowered to the units still paid for. When it is the latest period still paid, the key's seats follow. A partial refund by amount changes nothing. |
| Chargeback won, or a refund that failed afterwards | The key is reinstated (`active` again), or the renewal's time is given back. |

`revoke_on_refund` and `revoke_on_chargeback` switch the first and fourth rows off, and the renewal rollback with them. A partial refund by quantity still lowers the seats.

A revoked key stops working on every device at the next check.

## Webhooks [#webhooks]

Subscribe to these events to keep your own records in step with each key. The event's `data` is the license.

| Event | When it fires |
| --- | --- |
| [`license:issued`](/api/webhooks/license-issued) | A key is made. |
| [`license:extended`](/api/webhooks/license-extended) | A renewal moves `expires_at` later, or a rolled-back renewal's time is given back. |
| [`license:expiry_changed`](/api/webhooks/license-expiry-changed) | A full refund or chargeback of a renewal moves `expires_at` earlier. If the new expiry is already past, `license:expired` is sent too. |
| [`license:limit_changed`](/api/webhooks/license-limit-changed) | A partial refund by quantity, or its reversal, changes `activation_limit` without changing `expires_at`. |
| [`license:revoked`](/api/webhooks/license-revoked) | The key is revoked. |
| [`license:reinstated`](/api/webhooks/license-reinstated) | The key is active again. If its expiry passed while it was revoked, `license:expired` follows. |
| [`license:expired`](/api/webhooks/license-expired) | The key's `expires_at` passed while it was active. Checked about once an hour. |

- **Skip repeats.** Like any event, one can be delivered more than once. Skip an `event_id` you have already handled.
- **Keep the highest `version`.** Each change to a key raises the license's `version`. Events can arrive out of order, and changes made close together can be sent as one event with the latest state, so `version` can skip numbers. Ignore an event whose `version` is lower than the one you have, and treat an equal one as current: an expiry does not change the key, so `license:expired` can carry the same `version` as `license:issued` or `license:reinstated`. Skip repeats by `event_id`, never by `version`.
- **Keep `license_id` and `version`.** If your subscription uses `show` or `hide`, make sure both are still sent.
- **Not every change sends an event.** A change to none of `status`, `activation_limit` and `expires_at`, such as `revoked_reason` moving from `refund` to `chargeback`, raises `version` but sends no event.
- **A late renewal gets no `license:expired`.** A key renewed after its expiry passed, but before the hourly check ran, already works again, so it gets only `license:extended`. If the check saw the expiry first, `license:expired` comes before `license:extended`.
- **No keys in webhooks.** A webhook never contains a generated key, in any event and however deeply you `expand` it: `text` and `html` are null. Use `key_last4` to tell keys apart.

## Check keys from your app [#check-keys-from-your-app]

Your app checks a key with three routes on the usage meter, `https://usage.comecero.com`. They need no API token: the customer's key and your shop's `account_id` (public, for example `AB1234`) are the credential. Put the `account_id` in your app; never put a Comecero API token in it.

1. **Activate** when the customer enters their key. Your app sends the key and a fingerprint of the device and gets an `instance_id`. Save the key and the `instance_id` on the device.
2. **Validate** when the app starts, and then now and again (once a day is plenty). Send the saved key and `instance_id`. Keep the app working while `valid` is `true`.
3. **Deactivate** when the customer signs out or moves to another device. That frees the seat.

```bash title="Activate"
curl -X POST https://usage.comecero.com/v1/licenses/activate \
  -H "Content-Type: application/json" \
  -d '{
    "account_id": "AB1234",
    "license_key": "ACME-7K2QF-M9X4T-0BHRW-C3NDP-YV8JE",
    "fingerprint": "3f6c1d0e9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d",
    "instance_name": "Studio Mac"
  }'
```

### The device fingerprint [#the-device-fingerprint]

`fingerprint` is a stable id of the device. The same device must always send the same one: activating a device that is already active returns the same `instance_id` and takes no extra seat, which makes an activation safe to resend after a timeout.

- **Send a SHA-256 hash** of the device's machine id (in hex), **or a UUID** your app makes once and saves.
- **Never send a raw serial number**, or anything else that identifies the person. A fingerprint that looks like a card number is refused.
- Printable ASCII, no spaces, up to 255 characters.

### Validate result codes [#validate-result-codes]

[Validate](/api/license-activation/validate-license) always answers `200` for a well-formed request. Read `valid` and `code`:

| `code` | `valid` | Meaning |
| --- | --- | --- |
| `VALID` | `true` | The key works, and the device (if you sent `instance_id`) is active on it. |
| `NOT_FOUND` | `false` | No such key for this `account_id`. Check what the customer typed. |
| `REVOKED` | `false` | The key was revoked, after a refund or chargeback. |
| `EXPIRED` | `false` | The key's `expires_at` has passed. For a subscription, the customer should renew. |
| `INSTANCE_NOT_FOUND` | `false` | The key works, but this `instance_id` is not one of its devices. Activate again. |
| `INSTANCE_DEACTIVATED` | `false` | The key works, but this device was deactivated, usually to free its seat for another device. Don't activate again on your own: ask the customer, and activate only when they choose to. |

If the usage meter can't be reached, let the app keep working for a while on its last good answer, rather than locking out a paying customer.

### Rate limits [#rate-limits]

Over a limit, the routes answer `429` with a `Retry-After` header (seconds). Wait that long and try again. The limits, checked in this order:

| Limit | Per |
| --- | --- |
| 600 requests, and 20 keys that don't exist | IP address, per minute |
| 10 activations and deactivations, and 60 validations | key, per minute |
| A shared limit for all your customers' requests | account, per second |

Only requests with a real key count against your account, so someone guessing keys can't use up your customers' share.

Every field, answer and status code is in the API reference: [Activate a license key](/api/license-activation/activate-license), [Validate a license key](/api/license-activation/validate-license) and [Deactivate a device](/api/license-activation/deactivate-license). The [License key API](/docs/license-keys/license-key-api) page has the errors and a Mac app example in Swift.

## Next steps [#next-steps]

<Cards>
  <Card title="License key API" href="/docs/license-keys/license-key-api" />
  <Card title="Import existing keys" href="/docs/license-keys/import-licenses" />
  <Card title="License services in the API reference" href="/api/license-services" />
  <Card title="Webhooks" href="/docs/guides/webhooks" />
</Cards>
