Check what a customer holds
What you will build
You read the features a customer is entitled to, first by your own reference for the customer and then by Black's ID, before and after the customer's first payment. You also read what your environment can sell, and you learn when a benefit starts and stops and which event tells you that the answer changed.
Before you start
You need a test environment and an access token. See Authentication. You need a feature attached to a product, a recurring price of that product, and a customer with a subscription, as Create a subscription and report its payments explains. The sample below creates each of them.
Give the decision to the lookup, not to the subscription's status alone. A subscription in grace keeps its features by default, and a subscription set to cancel keeps them until its period ends, so the lookup applies those rules for you.
Steps
-
Create the customer with your own reference. Send a
POSTrequest to/v1/customerswith anexternal_reference, the ID that your own system holds for the customer. The response holds the customer. Black treats the reference as exact: case matters, and it trims spaces at the start and the end. -
Read what the customer holds by your reference. Send a
GETrequest to/v1/active-entitlements?customer_reference={reference}. The response is a list:dataholds one entry for each feature the customer holds, and it is empty while the customer holds nothing. -
Read the same by Black's ID with
GET /v1/active-entitlements?customer={id}. Send exactly one ofcustomerandcustomer_reference. The two answers differ only in the field you sent:customer_referenceisnullin an answer by ID. -
Read each entry. An entry is an
active_entitlement, and the list is ordered bylookup_key:idstarts withent_. It is the same at every read, because Black works it out from the environment, the customer and the feature, and stores nothing.lookup_keyis the feature's lookup key.featureis the whole feature as it is now: itsid,name,active,createdandmetadata. Read your own values, such as a discount, frommetadata. Black does not check them.subscriptionis the subscription that gives the feature: itsid,status,status_label,subscription_item,price,product,current_period_endandrenews.
A feature that two items of the subscription give appears once. The list is never paged, so
has_moreis alwaysfalse. -
Read
renews. It istruewhile the subscription will renew atcurrent_period_end. It isfalsewhen the subscription is set to cancel at the end of its period, and while the vendor of your account has stopped renewals. The customer still holds the feature untilcurrent_period_endin both cases, so show the customer when the benefit ends, not that it has ended. -
Keep the answer for up to 60 seconds. Every successful answer carries
Cache-Control: max-age=60, private. Your server or your app may reuse an answer for 60 seconds without asking again, in a cache that belongs to one customer, never in a cache shared by several. A change that Black makes reaches you at most 60 seconds late if you keep the answer for the full time. Black works each answer out from what it stores at the moment of the request, and asks no other service.
Handle an empty answer
An empty data is an answer, not an error. Each of these answers 200 with data empty:
- The customer has no subscription that gives features: it is
incomplete,incomplete_expiredorcanceled, or it ispast_duewhile the settingkeeps_entitlements_in_graceisfalse. See "Read the status of a subscription" in Create a subscription and report its payments. - The customer is deleted, or your account has blocked it, and you ask by ID. The same holds by reference, with
customernullfor a deleted customer. - The vendor of your account has switched
service_offon. The lookup answers as for a customer who holds nothing, and nothing stored changes, so switching it off again gives back what the customer held. - You ask by a reference that Black has never seen in this environment, or that only a deleted customer held, or that another environment holds. The answer is
200withcustomernull, your reference and an emptydata. Create the customer if the reference should name one.
Handle a refusal
Each refusal gives an error with a code. See Errors for every code.
authentication_required(401). The request has no access token, or the token is not valid. Send a valid token.parameter_invalid(422) namingcustomer. You sent neithercustomernorcustomer_reference, or both. Send one.parameter_invalid(422) namingcustomer_reference. The reference is empty, longer than 255 characters or not a string. Send a reference of 1 to 255 characters.resource_not_found(404) namingcustomer. The ID names no customer of your environment. Take the ID from the response that created the customer.
Know when a benefit starts and stops
The lookup follows the subscription, so a benefit starts and stops when the subscription's features change:
- A feature attached to a product reaches a subscription at the start of its next period. A subscription created after the attachment gains the feature from its first period, once its first invoice is paid.
- A feature detached from a product stops reaching a subscription at the start of its next period.
- A change to a feature's
metadatashows at once, in the next lookup. - A feature made inactive, and a product or a price archived, change nothing for subscriptions that already exist.
- A subscription set to cancel gives its features until the period ends, with
renewsfalse. - A subscription in grace gives its features while
keeps_entitlements_in_graceistrue, which is the default. Black reads the setting at each request, so a change shows at once. - A customer that your account has blocked, and a customer you delete, hold nothing from that moment.
Know a customer by your own reference
An external_reference names at most one customer among the customers of an environment that are not deleted. Two requests have the answer below, so that you can retry a create safely:
- A create with a reference that a customer already holds answers
200with that customer, unchanged. Black does not apply the other fields of the repeated request to that customer, and creates and records nothing. - An update that sets a reference another customer holds is refused with
409and the codecustomer_reference_in_use, namingexternal_reference. Black changes nothing. Send another reference, or first remove the reference from the customer that holds it. - A deleted customer's reference is free again, and a customer with no reference is a new customer at every create.
List the customer of a reference with GET /v1/customers?external_reference={reference}. The list holds the one customer, or none.
Read what you can sell
Send a GET request to /v1/plans. The response holds the plans the environment sells now. A plan is an active recurring price of an active product. Each entry holds its id, which is the ID of the price that you send when you create a subscription, the price and the product, and the features attached to the product, oldest attachment first. The list is never paged.
The fields sales_open and sales_closed_reasons say whether a new subscription can be created. sales_open is true when none of these reasons applies. sales_closed_reasons lists each reason that applies, in this order, and is empty exactly when sales are open:
service_off. The vendor of your account has switched the service off.renewals_stopped. The vendor of your account has stopped renewals.no_plans. The environment has no plan. Create a recurring price for an active product.
The plans stay in the list while sales are closed, and data is empty only when there is no plan.
While sales are closed, a request to create a subscription is refused with 409 and the code sales_closed, whatever the reason, and Black stores nothing. Read GET /v1/plans for the reasons. Create a plan for no_plans. For the other two, wait: only the vendor of the account turns them off, and the next create succeeds once no reason applies.
Learn when the lookup changes
Black records the event entitlements.active_entitlement_summary.updated when the set of features a customer holds changes. Its data.object is an active_entitlement_summary: the customer, livemode and entitlements, a list of the first 10 entries as the lookup gives them, with has_more and the url where you read them all. The list before the change is in data.previous_attributes.entitlements. The event is about the customer, so it carries customer and customer_sequence, and its subscription fields are null.
Black records it when a subscription starts to give features, or stops, for example:
- The first payment makes a subscription
active. - A subscription enters grace with
keeps_entitlements_in_gracefalse, and leaves it. - A subscription ends, by a cancel now, at the end of its period or at the end of its grace.
- A late payment brings a subscription back.
- A customer is deleted or blocked.
- You change
keeps_entitlements_in_gracewhile a customer holds a subscription in grace.
Black records no event when the set of features stays the same, and for these changes:
- A renewal of a subscription that holds its features, and a cancel at the end of the period or its undo.
- A feature's
metadatachanges.feature.updatedtells you. - A feature is attached or detached. It reaches each subscription at its next period, and the decision of that period records the event when the set changes.
- A feature is made inactive, a product or a price is archived, or a product is deleted.
- The vendor switches
service_offorrenewals_stopped.
Do not rely on the event alone. Read the lookup when you need the answer, because a change you are not told about, such as a feature's metadata, still shows in the lookup. See Events for every event.
A runnable sample
The sample creates a feature with metadata, a product with the feature attached, a monthly price of 60 SAR and a customer with a reference. It reads the lookup by reference before the subscription exists, creates the subscription, reports its first invoice paid, and reads the lookup by reference and by ID. It then reads a reference Black has never seen, repeats the create of the customer, lists the customer by its reference, and reads the plans. It writes PLACEHOLDER_ACCESS_TOKEN where your access token goes, reads each ID from the response before it, and needs sed. A test environment takes five writes a second, so the sample waits a second after each write.
TOKEN=PLACEHOLDER_ACCESS_TOKEN
FEATURE=$(curl --silent --request POST https://api.black.example/v1/features \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"name":"Ten percent off","lookup_key":"lookup-ten-percent","metadata":{"kind":"percentage","percent":"10"}}' \
| sed -n 's/.*"id":"\(feat_[0-9a-z]*\)".*/\1/p')
PRODUCT=$(curl --silent --request POST https://api.black.example/v1/products \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"name":"Prime Lookup Monthly"}' \
| sed -n 's/.*"id":"\(prod_[0-9a-z]*\)".*/\1/p')
curl --silent --request POST https://api.black.example/v1/products/$PRODUCT/features \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"feature\":\"$FEATURE\"}"
PRICE=$(curl --silent --request POST https://api.black.example/v1/prices \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"product\":\"$PRODUCT\",\"currency\":\"SAR\",\"unit_amount\":6000,\"recurring\":{\"interval\":\"month\",\"interval_count\":1}}" \
| sed -n 's/.*"id":"\(price_[0-9a-z]*\)".*/\1/p')
CUSTOMER=$(curl --silent --request POST https://api.black.example/v1/customers \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"name":"Lina","email":"lina@example.com","external_reference":"lookup-guest-2204"}' \
| sed -n 's/.*"id":"\(cus_[0-9a-z]*\)".*/\1/p')
curl --silent "https://api.black.example/v1/active-entitlements?customer_reference=lookup-guest-2204" \
--header "Authorization: Bearer $TOKEN"
SUBSCRIPTION_RESPONSE=$(curl --silent --request POST https://api.black.example/v1/subscriptions \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: lookup-subscription-2204" \
--data "{\"customer\":\"$CUSTOMER\",\"items\":[{\"price\":\"$PRICE\"}]}")
INVOICE=$(echo "$SUBSCRIPTION_RESPONSE" | sed -n 's/.*"latest_invoice":"\(in_[0-9a-z]*\)".*/\1/p')
curl --silent --request POST https://api.black.example/v1/invoices/$INVOICE/mark-paid \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"amount":6000,"currency":"SAR","payment_reference":"lookup-payment-2204"}'
curl --silent "https://api.black.example/v1/active-entitlements?customer_reference=lookup-guest-2204" \
--header "Authorization: Bearer $TOKEN"
curl --silent --include "https://api.black.example/v1/active-entitlements?customer=$CUSTOMER" \
--header "Authorization: Bearer $TOKEN"
curl --silent "https://api.black.example/v1/active-entitlements?customer_reference=lookup-never-seen" \
--header "Authorization: Bearer $TOKEN"
curl --silent --request POST https://api.black.example/v1/customers \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"name":"Lina again","external_reference":"lookup-guest-2204"}'
curl --silent "https://api.black.example/v1/customers?external_reference=lookup-guest-2204" \
--header "Authorization: Bearer $TOKEN"
curl --silent https://api.black.example/v1/plans \
--header "Authorization: Bearer $TOKEN"
Each request answers 200, except the creates of the feature, the product, the attachment, the price, the customer and the subscription, which answer 201. The repeated create of the customer answers 200. The first lookup by reference answers an empty data, because the customer has no paid subscription, and the second holds one entry with the feature lookup-ten-percent, its metadata and a subscription with status active and renews true. The lookup by ID answers the same entry, with customer_reference null, and the header Cache-Control: max-age=60, private:
HTTP/1.1 200 OK
Cache-Control: max-age=60, private
{"object":"list","url":"/v1/active-entitlements","customer":"cus_01m7decd2f0b28sdpxp7r6gmmp","customer_reference":null,"livemode":false,"has_more":false,"data":[{"object":"active_entitlement","id":"ent_6mpp6qs5pk5r8a0sa3p3hfyrcb","livemode":false,"lookup_key":"lookup-ten-percent","feature":{"object":"feature","id":"feat_01m7dec8v2r27x8efaanebk5z5","livemode":false,"created":1794673877,"name":"Ten percent off","lookup_key":"lookup-ten-percent","active":true,"metadata":{"kind":"percentage","percent":"10"}},"subscription":{"id":"sub_01m7dece4y99wa4x6t06ddw831","status":"active","status_label":"active","subscription_item":"si_01m7dece4y99wa4x6t06ddw82z","price":"price_01m7decc0nx280p5zh96mb991t","product":"prod_01m7dec9x4p6dchfa1y78xd2de","current_period_end":1797265883,"renews":true}}]}
The reference that Black has never seen answers customer null and an empty data. The repeated create answers the customer that the first create made, with the name Lina, and the list by reference holds that one customer. The plans read holds the price of the sample with sales_open true while the environment's sales are open.
To run the sample again in the same environment, change the names that start lookup: Black refuses a lookup key that another feature holds, and a reference that another customer holds answers the first customer.
What to do next
- Read how a subscription is created, paid and renewed in Create a subscription and report its payments.
- Read what a cancel changes in the lookup in Cancel a subscription.
- Read every event, including the entitlements event, in Events.
- Read every error code in Errors.