Customers and Subscriptions
Now that you have defined your Product
and how to price it using Plans
and Prices
, the next step is to connect your offering with the end-users who will consume it. This is managed through Customers
and Subscriptions
.
Customers
A Customer
resource represents the relationship between your Merchant
Organization
and a Consumer
Organization
that uses your Product
.
- Linkage: It connects a specific
Consumer
(who holds aWallet
and pays) to yourMerchant
account. - Context: When you report
Usage Events
, you report them against aCustomer
ID, allowing Sulu to identify the correctConsumer
Wallet
to bill and the correctMerchant
Wallet
to credit. - Creation:
Customer
resources are typically created automatically as part of the Subscription Flow.
Subscriptions
A Subscription
formalizes the agreement between a Customer
and a specific Plan
(and by inheritance, a Product
).
- Agreement: It signifies that the
Customer
(representing theConsumer
Organization
) has agreed to pay for theProduct
according to the rates defined in thePlan
. - Scope: A
Subscription
links oneCustomer
to onePlan
. - Billing Basis: Active
Subscriptions
are necessary for Sulu to correctly processUsage Events
and apply the rightPrices
. - Creation: Like
Customers
,Subscriptions
are usually created automatically during the Subscription Flow.
Essentially:
- A
Consumer
wants to use yourProduct
. - You establish a
Customer
relationship between yourMerchant
and theirConsumer
. - The
Customer
agrees to a specificPlan
by creating aSubscription
. - The
Subscription
allows theMerchant
to reportUsage Events
for thatCustomer
, which are then billed according to thePlan
’sPrices
against theConsumer
’sWallet
.
The Subscription Flow
Sulu provides a managed checkout flow to simplify the process of creating Customers
and Subscriptions
.
1. Generate Authorization Link
To initiate the flow, you need a unique URL associated with a specific Plan
.
- Method: Use the Create Authorization Session API endpoint (
POST v0/customers/authorization
) or the “Generate Authorization Link” button on thePlan
details page in the Sulu Merchant Dashboard. - Input: You’ll need the
planId
and yourmerchantId
. - Output: Sulu generates a unique URL (e.g.,
https://platform.sulu.sh/authorize/...
) representing a pending authorization session.
2. Redirect Consumer to Link
Provide the generated authorization URL to the end-user who wants to subscribe.
3. Sulu Checkout
The user clicks the link and is taken to Sulu’s secure, hosted checkout page.
- Existing Sulu User: If the user is already logged into a Sulu
Consumer
account, they will be prompted to confirm the subscription to the specifiedPlan
. - New Sulu User: If the user is not logged in or doesn’t have a Sulu account, they will be guided through creating a new Sulu
Consumer
Organization
andUser
account, and adding a payment method to top up theirWallet
before confirming the subscription. - Merchant Branding: The checkout page displays the
Product
andPlan
details (name
,description
) you defined.
4. Resource Creation
Upon successful completion of the checkout flow by the user:
- Sulu automatically creates a
Customer
resource linking yourMerchant
Organization
to theConsumer
Organization
(if one didn’t already exist for this Consumer). - Sulu automatically creates a
Subscription
resource linking theCustomer
to the chosenPlan
. - The subscription status becomes
active
.
5. Ready for Usage
The Customer
is now successfully subscribed to your Plan
, and you can begin reporting Usage Events against their customerId
.
Entitlements
An Entitlement
represents a Customer
’s right to consume Billable Metrics
associated with their active Subscription
, provided they have sufficient funds in their Wallet
.
Sulu allows Merchants
to check these entitlements in real-time, which is crucial for Pay-As-You-Go (PAYG) models where access should be granted before consumption and billing.
Checking Active Entitlements
To verify if a Customer
is currently entitled to use resources associated with certain Billable Metrics
:
- Use the Get Active Entitlements API endpoint (
GET v0/entitlements/activeEntitlements
). - Provide the
customerId
. - Response: The endpoint returns a list of
ActiveEntitlement
objects. Each object indicates an activeSubscription
and lists thebillableMetrics
the customer is currently permitted to consume under thatSubscription
(based on the subscription being active and the associatedConsumer
Wallet
having a positive balance).
If the response list is empty, or if the specific billableMetricId
you are checking is not present in any of the returned ActiveEntitlement
objects, the Customer
is not entitled to consume that resource at this moment.
Example Flow: API Gateway Entitlement Check
Imagine you’re building an API gateway for your monetized API:
- Request Received: A user sends a request to your gateway, including their API key.
- Authentication & Customer Lookup: Your gateway authenticates the key and looks up the associated Sulu
customerId
. - Check Sulu Entitlements: Your gateway calls Sulu’s
GET v0/entitlements/activeEntitlements
endpoint with thecustomerId
. - Verification: The gateway checks the response:
- Does the response contain an
ActiveEntitlement
object? - Does that object’s
billableMetrics
list include the ID for the metric being consumed by this API call (e.g.,bm_api_call
)?
- Does the response contain an
- Decision:
- Entitled: If yes to both, the gateway forwards the request to your backend service. Upon successful processing by the backend, the gateway reports a Usage Event to Sulu for the consumed
billableMetricId
andquantity
(usually 1 for a per-call metric). - Not Entitled: If no, the gateway rejects the request immediately (e.g., with an HTTP 402 Payment Required or 403 Forbidden status code), preventing usage and potential billing issues.
- Entitled: If yes to both, the gateway forwards the request to your backend service. Upon successful processing by the backend, the gateway reports a Usage Event to Sulu for the consumed
This real-time check ensures users only consume resources they are actively subscribed to and have the balance to pay for.
With Customers
subscribed and Entitlements
understood, the final piece is reporting consumption via Usage Events.