comecerodocs
Entitlements

Quickstart

Create features, publish a feature set, sell it with a product, then check access from your backend over HTTP and hear about changes by webhook.

This quickstart gives a "Pro" plan three features: single sign-on, one project a seat, and 10,000 API calls a month. It takes about ten minutes in test mode.

You need:

  • Entitlements turned on for your shop by Comecero staff. Ask support before setup. For the metered feature, usage billing must be on too.
  • A test secret token, in COMECERO_SECRET_TOKEN. See authentication.
  • A sum metric for API calls, met_api_calls, with the event key api_calls. See create a metric.
  • A test subscription product, in PRODUCT_ID.
  • A test usage key, in COMECERO_USAGE_KEY. See create a usage key.
  • Node.js 18 or later (it has fetch).

1. Create the features and the feature set

// setup.mjs: run with COMECERO_SECRET_TOKEN and PRODUCT_ID set.
const API = "https://api.comecero.com/api/v1";
const headers = {
  Authorization: `Bearer ${process.env.COMECERO_SECRET_TOKEN}`,
  "Content-Type": "application/json",
};

async function call(method, path, body) {
  const response = await fetch(API + path, { method, headers, body: body && JSON.stringify(body) });
  const answer = await response.json().catch(() => ({}));
  if (!response.ok) {
    throw new Error(`${method} ${path}: ${response.status} ${answer.error?.message ?? ""}`);
  }
  return answer;
}

// The keys are what your app checks. They never change.
await call("POST", "/features", { key: "sso", name: "Single sign-on", type: "boolean" });
await call("POST", "/features", { key: "projects", name: "Projects", type: "number", unit_label: "project" });
await call("POST", "/features", {
  key: "api_calls",
  name: "API calls",
  type: "metered",
  metric_id: "met_api_calls",
  unit_label: "call",
});

// The plan. It grants nothing until a revision is published.
await call("POST", "/feature_sets", { feature_set_id: "fs_pro", name: "Pro plan" });

// Revision 1. Published revisions never change: to change the plan, publish revision 2.
const revision = await call("POST", "/feature_sets/fs_pro/revisions", {
  rollout: "all",
  entries: [
    { feature_key: "sso", value: true },
    { feature_key: "projects", limit_per_unit: "1" },
    { feature_key: "api_calls", limit: "10000", reset: "billing_period", hard_limit: true },
  ],
});
console.log("Published revision", revision.revision);

// Everyone who buys the product gets the plan.
await call("POST", `/products/${process.env.PRODUCT_ID}`, { feature_set_id: "fs_pro" });

Run setup once with unused feature keys and a new feature set id. A duplicate create returns 409; read the existing object and check its settings before reusing it. To change the plan later, publish another revision with the complete entry list.

With limit_per_unit: "1", a customer who buys 12 seats gets 12 projects. With hard_limit: true, API calls stop at 10,000 until the next billing period; leave it out to let usage go on past the limit. See soft and hard limits.

2. Buy the product

Before the purchase, create or choose a test customer and set CUSTOMER_ID to their Comecero id. Give them the id your app uses, so usage and access use the same id:

curl -X POST https://api.comecero.com/api/v1/customers/$CUSTOMER_ID \
  -H "Authorization: Bearer $COMECERO_SECRET_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "external_id": "user_8412" }'

external_id can be set once. In the first 90 seconds after your shop first uses entitlements, setting it returns 409 with code in_use; try again after two minutes. Set it before the first priced purchase and before sending usage. Without it, your app uses the Comecero customer_id. See your id for the customer.

Buy the product in test mode, for example on your test shop, as that same customer. A quantity of 12 gives them 12 projects.

3. Check access from your backend

@comecero/entitlements is coming soon and is not on npm yet. Today, call GET https://usage.comecero.com/v1/entitlements from your server with a usage key:

// check.mjs: run with COMECERO_USAGE_KEY set. Node.js 18 or later.
const url = new URL("https://usage.comecero.com/v1/entitlements");
url.searchParams.set("customer_id", "user_8412");
url.searchParams.set("feature", "api_calls");

const response = await fetch(url, {
  headers: { Authorization: `Bearer ${process.env.COMECERO_USAGE_KEY}` },
});
if (!response.ok) throw new Error(`Entitlement check failed: ${response.status}`);
const answer = await response.json();
const calls = answer.entitlements.find((row) => row.feature === "api_calls");
console.log(calls?.has_access === true, calls?.used, calls?.limit, calls?.reset_at);

Check api_calls before serving work, and send a usage event for each call served. Your app enforces the answer; the meter keeps accepting valid usage events even at a hard limit. Omit feature to read all features, or ask for sso or projects alone.

Cache successful answers for up to 60 seconds or until valid_until, whichever comes first. The API does not cache them for you. Keep the usage key on your server. See the check API for limits and errors, and SDK status and HTTP examples for integration guidance.

4. Hear about changes

When an upgrade, cancellation or override changes the customer's access, Comecero sends entitlement:changed. Subscribe to it. Set active to true: a new event subscription is off until you do.

curl -X POST https://api.comecero.com/api/v1/event_subscriptions \
  -H "Authorization: Bearer $COMECERO_SECRET_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "event_type": "entitlement:changed",
    "method": "http",
    "format": "json",
    "destination": "https://example.com/webhooks/comecero",
    "active": true
  }'

On your webhook route, check the signature against the raw request body before parsing JSON. Use the subscription's signature_key, or your account's key. Drop an event_id you have already handled, and keep the highest data.version for each customer because deliveries can arrive out of order.

For a new entitlement:changed event, remove the cached answers for data.external_id. The next check then reads the new access. A cache shared by several server processes needs invalidation in each process, or in the shared cache.

Next

On this page