# Entitlements (/docs/entitlements)

Entitlements tell your app what a customer may do: turn on single sign-on, allow 10 seats, allow 50,000 API calls a month. You describe the features once, group them in a feature set, and point products at the set. When a customer buys, renews, changes or cancels, Comecero works out what they have, and your app asks with one call.

<Callout title="Not the same as usage billing entitlements">
[Usage billing entitlements](/docs/usage-billing/entitlements) price a metric on a billing agreement. The entitlements on these pages decide access in your app. A metered feature can use a usage billing rate plan: Comecero then makes the usage billing agreement and entitlement for you (see [priced entries](#priced-entries-and-usage-billing)).
</Callout>

Comecero staff turn entitlements on per shop. Ask support to enable the switch before setup; it is not on for every shop by default. Until it is on, the feature, feature set and customer entitlement routes answer `403` with code `entitlements_not_enabled`. The switch covers live and test mode. Turning it off later blocks setup and these routes, but keeps existing grants working through the usage meter.

<Cards>
  <Card title="Quickstart" href="/docs/entitlements/quickstart" description="Create features, publish a set, sell it and check access from your backend." />
  <Card title="Check API" href="/docs/entitlements/check-api" description="GET /v1/entitlements on the usage meter, with a usage key." />
  <Card title="Customer entitlements" href="/docs/entitlements/customer-entitlements" description="The same answer from the Comecero API, for support tools." />
  <Card title="SDK and HTTP integration" href="/docs/entitlements/sdk" description="The SDK is coming soon. Use HTTP from your server today." />
</Cards>

## Features [#features]

A **feature** is one thing you sell access to. Its `key` is what your app checks, for example `sso` or `api_calls`. The key never changes. Each account has at most 200 features in each mode, including archived features (live and test are separate). Archiving does not free a catalog slot.

| `type` | What it is | What the check answers |
| --- | --- | --- |
| `boolean` | On or off, such as single sign-on. | `value` true or false. |
| `number` | A setting, such as seats or projects. | `value`, a decimal string, or null for unlimited. |
| `metered` | A limit on a usage metric, such as API calls a month. | `limit`, `used`, `remaining` and `reset_at`. |

A metered feature counts a **sum** or a **latest** metric of [usage billing](/docs/usage-billing), so usage billing must be on. Your app sends usage events for the metric as usual, and the check reads those totals, or for a `latest` metric the last value sent. A feature on a `latest` metric uses `reset: never` (the default for it), and a feature set can't price it: give it a limit, or bill it with a usage agreement.

A feature grants nothing on its own. Once a published revision or an override grants it, its `type` and `metric_id` are fixed. Archive a feature you no longer sell: customers who have it keep it, and new revisions can't list it. See [Create a feature](/api/features/create-feature).

## Feature sets and revisions [#feature-sets-and-revisions]

A **feature set** is what a plan includes, for example "Pro plan". Its content lives in **revisions**. A published revision never changes; to change a set, publish the next revision. See [Publish a feature set revision](/api/feature-sets/publish-feature-set-revision).

You can choose the set's own `feature_set_id` when you [create it](/api/feature-sets/create-feature-set). The id `none`, in any case, is reserved (`400`, `EnV37`).

Each revision lists entries, one per feature:

| Field | For | Meaning |
| --- | --- | --- |
| `value` | boolean, number | The value. For a boolean, true or false. |
| `limit_per_unit` | number, metered | The value or limit is this times the quantity bought: 1 a seat on 12 seats is 12. |
| `limit` | metered | The limit in each window. |
| `unlimited` | number, metered | No limit. |
| `reset` | metered | `billing_period` (the default) starts usage at zero each period. `never` never resets: usage counts from when the customer got the feature, and a renewal doesn't start it again. `day`, `week`, `month`, `quarter` and `year` are fixed periods counted from the purchase (a month from the 31st ends on the last day of a shorter month), for entries without a rate plan. They need a separate platform setting: until it is enabled, publishing one is refused (`400`, `EnV35`). |
| `hard_limit` | metered | True stops access at the limit. False (the default) lets usage go on. |
| `rate_plan_id`, `rate_plan_version` | metered | A priced entry. The rate plan's included allowance is the limit, and usage past it is billed. |

A revision's `rollout` decides who gets it:

- `all`: every customer who holds the set moves to the new revision.
- `new_only`: customers keep the revision they bought; only purchases from now on get the new one.

A revision's priced entries must all bill in one billing region and currency, and a revision with `rollout: all` can't move holders to another region or currency (`400`, `EnV36`): publish it with `new_only` instead, or price it in the region and currency the holders are billed in.

Publishing checks the revision against every product that points at the set. A one-time product needs `reset: never` or a fixed period, and no rate plan. A priced entry needs a subscription billed monthly or more often, with a price in the rate plan's currency.

### Priced entries and usage billing [#priced-entries-and-usage-billing]

A subscription that holds a priced entry is billed through [usage billing](/docs/usage-billing): Comecero makes its billing agreement and metered entitlements for its priced metrics, from the first paid hour. If several items on the same subscription price the same metric, Comecero bills only the oldest membership for that metric and logs the others. Their ids start with `agr_ent_` and `ent_ent_`, and the agreement has `created_by: "entitlements"`. You can't create ids with those prefixes yourself (`400`, `invalid_input`).

Comecero keeps these agreements in line, so change the feature set or the subscription, not the agreement:

- **Plan change on the same subscription.** The old entitlement ends at an hour and the new one starts at that hour on the same agreement. The allowance already used is carried over, and no hour is billed twice or missed.
- **Downgrade.** When the subscription has nothing priced left (a product without a rate plan, or a plan change to another subscription), the agreement ends at that hour and bounds its entitlements, even when their own fields still show active. Nothing is billed at the old rate after it.
- **`external_id`.** Priced entries bill under the id usage knows the customer by, so [set `external_id`](#your-id-for-the-customer) before the customer's first priced purchase. Once an agreement for priced entries exists under their `customer_id`, even one that ended, setting it is refused (`409`, `in_use`, `EnCus10`).
- **Rollout.** A revision with `rollout: all` that adds, changes or removes a priced entry brings every holder's billing in line, in the background through the hourly reconciliation job.
- **A new agreement inside a period.** When a downgrade ended the agreement and an upgrade starts a new one in the same subscription period at the hour the old one ended, the period still has one count: for a `sum` metric the new agreement starts with the allowance and tiers the period already used. Late usage for such a period is rated on top of everything the period billed, and each agreement pays only for its own late units, at its own price and under its own limits. Nothing already billed moves between them. A gap between agreements starts fresh allowance and tier counts.

A product with priced entries can only be bought in the currency they bill in. Anything else is refused before any money moves (`400`, `invalid_input`): a cart payment in another currency (`EnPay01`), a subscription made through the API in another currency (`EnPay02`), and the product added to a subscription in another currency (`EnPay03`). The message names the product and the currency it can be bought in.

A customer who already has an agreement you made by hand is not given one, and the failure is logged. Agreements made by hand are not billed alongside: end that agreement to bill the priced entries.

## Products [#products]

A product grants a feature set through its `feature_set_id`. Set it with [Update a product](/api/products/update-product). Every subscription or order for that product then grants the set's features. Send `null` to take the product out of the set.

## Memberships [#memberships]

A customer holds a set through a **membership**: one for each subscription or order line of a product in a set. You don't create memberships; Comecero keeps them in step with purchases.

- **Subscriptions** grant while the subscription is in its trial or active, with a short grace while a renewal payment is pending. A cancel at the end of the period keeps access to the end of the period paid for; a cancel now, or one after failed payments, ends it at once. The usage window is the billing period (the trial, during a trial).
- **Orders** (one-time products) grant once the order is paid. A full refund, a refund of the whole line or a chargeback ends the access; refunding part of the line lowers the quantity.
- **Which revision.** With `rollout: new_only`, the membership keeps the revision that was latest when it was granted. With `all`, it follows the latest revision.
- **Quantity.** `limit_per_unit` multiplies by the quantity bought.

## No pooling [#no-pooling]

A customer can hold the same feature from more than one purchase, for example two subscriptions. Comecero never adds them together:

- A boolean feature is on when any purchase turns it on.
- A number feature takes the highest value.
- A metered feature keeps one **item** per purchase, each with its own limit and window. The customer has access while any item allows it. When every item shares one window, the row shows that usage against the largest item limit. Otherwise the row's `limit`, `used`, `remaining` and `reset_at` are null, and each item has its own.

## Soft and hard limits [#soft-and-hard-limits]

A metered limit is soft unless the entry sets `hard_limit: true`.

- **Soft limit.** Passing it keeps `has_access` true and sets `limit_reached` true. If the entry is priced, the usage past the included allowance is billed. Use `limit_reached` to show a warning or an upgrade offer.
- **Hard limit.** At the limit, `has_access` is false with `reason: "limit_reached"` until the window resets. A priced entry counts as hard while its subscription is in a free trial, so a trial never runs up a bill. A trial with a cancel scheduled is still a trial until it ends.

The check is seconds behind the live count, so a burst can pass a hard limit by a little. If a customer must never pass it by even one unit, count in your app too.

## Overrides [#overrides]

An **override** changes one feature for one customer, for example a larger limit agreed by sales, or access taken away during a review. See [Create an entitlement override](/api/entitlement-overrides/create-entitlement-override).

- A customer has at most one active override per feature. Remove it before you create another: a second one is refused (`409`, `EnOv09`).
- `value: false` denies the feature on any type, whatever the customer bought (`reason: "denied"`), except a feature the customer gets through a priced entry: the priced grant wins, since it is billed.
- `value: true` grants a boolean feature. For a metered feature, send `limit` or `unlimited: true` (`value: true` on a metered feature is refused, `400`, `EnOv06`).
- `limit` sets a metered limit. `value` sets a number feature.
- `expires_at` ends it. Leave it out to keep it until you delete it.
- `reason` is a note for your team.
- Overrides can't change a priced metered feature, because its limit is what the customer pays for.

Removed and expired overrides are kept, so [List entitlement overrides](/api/entitlement-overrides/list-entitlement-overrides) shows the history.

## Your id for the customer [#your-id-for-the-customer]

Your app checks access with its own id for the customer, the same one it sends on usage events as `external_customer_id`. In Comecero that id is the customer's `external_id`. Until you set one, it is the Comecero `customer_id`.

Set `external_id` with [Update a customer](/api/customers/update-customer):

- It can be set once and never changed or removed.
- Only a secret or trusted token can set it, and only while entitlements are on.
- It must not be another customer's `external_id` or `customer_id` (`409`, code `external_id_taken`).
- In the first 90 seconds after your account first uses entitlements it is refused (`409`, code `in_use`). Try again after two minutes.
- Once the customer's usage is billed by a priced entry under their `customer_id` (an agreement for priced entries exists, even one that ended), it is refused (`409`, code `in_use`, `EnCus10`). Set it before their first priced purchase.

Set it before you send usage events for the customer, so usage and access use the same id.

## When access changes [#when-access-changes]

Comecero sends the webhook event [`entitlement:changed`](/api/webhooks/entitlement-changed) when what a customer can do changes: a feature granted or taken away, or a new value or limit. Its `data` names the customer (`customer_id` and `external_id`), the `cause` (`subscription`, `order` or `override`), a `version`, and the `changes`, each with the feature and its `before` and `after`.

A change nobody would notice, such as a renewal that only starts a new window, sends nothing. Events can arrive twice or out of order: drop an `event_id` you have seen, and keep the highest `version` for each customer. Check the [signature](/docs/guides/webhooks#check-the-signature) before you trust a delivery.
