Learn how to check feature access with the check route
When you create a product in Autumn, you define what features your customers on that product get access to.The check method returns the allowed property to check if a customer should have access to a feature, so you can block access or prompt them to upgrade.
Before your customer uses a feature, you can check if the customer is allowed to use it and their current usage.
ExampleLet’s imagine you have a free product that allows 100 AI messages per month. Before your customer sends an AI message, you can check if they have any remaining messages.If they still have messages remaining, they’ll be allowed to send an AI message. You can then record the usage event so Autumn can keep track of the balance.
Copy
import { useCustomer } from "autumn-js/react";const { allowed, check } = useCustomer();// check the customer state object locally (no API call)const canSendMessage = allowed({ featureId: "messages" });// alternatively, call Autumn's API to get the latest stateconst { data } = await check({ featureId: "messages" });
Even if your product doesn’t have usage limits (ie your product is purely usage-based), you can still use the above method to prevent excessive usage if a customer’s payment fails.If the product status is past_due, by default, Autumn will block access to the feature.
If you know the quantity a user will consume in advance, you can specify it with the required_quantity parameter. This means you can prevent a user from starting a process that would consume more than their allowance.By default, required_quantity is 1, so not passing this parameter will return allowed: true as long as the customer has a feature balance of 1 or more.
Checking Feature Access and Recording Usage together
In a typical workflow, you’ll want to check if a customer has access to a feature and then record the usage as separate steps.However, you can check entitlement and record usage in a single API call using the send_event field. The usage value will be the required_quantity parameter.
We recommend doing these as separate steps in your application logic, so usage is only counted if the event is successful.Alternatively, you can “refund” usage with a negative usage value if the event fails.
For simple on/off features, use the endpoint to determine if a customer has access. Alternatively you can just check if they have access to a certain product.
React
Copy
const { allowed } = useCustomer();// check if the customer has access to a productconst isPremium = allowed({ productId: "premium" });// check if the customer has access to a featureconst dashboardAccess = allowed({ featureId: "premium-dashboard" });