This is why Unprice is really adaptive. Instead of thinking about plans inside your code, with Unprice you think about features.
You map your application to validate specific features, this gives you the flexibility to change the packaging without affecting other customers.Think about it as a feature flag with super powers.
Entitlements are the features a customer has access to. They can be billed or not, for instance if a customer
is subscribe to a plan PRO with fetures “tokens” with a limit of 10K tokens per month, that is an entitlement that is billed.
Copy
// without Unpriceif (customer.plan === "PRO") { // <-- mapping to PRO plan means when the plan changes you have to change this // let custor use tokens} else { return "don't have access"}// ----------------------------------------------------// with Unprice// Verify if the customer has access to the featureconst { result, error } = await unprice.customers.can({ customerId: customer.unpriceCustomerId, featureSlug: "tokens", // map your feature code metadata: { action: "create", country: "US" }});if (error) { // handle potential network or bad request error // a link to our docs will be in the `error.docs` field console.error(error.message); return;}if (result.access) { // let custor use tokens // because now the feature is tied to the customer itself and not the plan // you can change the features of your customer without touching your code continue} else { return { message: "don't have access", deniedReason: result.deniedReason }}console.log(result);
A plan represent the package of features that you are offering. Every plan can have multiple versions, which comes handy when you want iterate on it. More info here
Copy
npm install @unprice/api
Verify features
Can method verify if the customer has access to the feature, whether is a flat feature or a usage based one.
See more here
Copy
const { result, error } = await unprice.customers.can({ customerId: "cus_1234567890", featureSlug: "tokens", metadata: { action: "create", country: "US" }});if (error) { // handle potential network or bad request error // a link to our docs will be in the `error.docs` field console.error(error.message); return;}console.log(result);
Report Usage
Depending on the feature, you can report usage.
Copy
const { result, error } = await unprice.customers.reportUsage({ customerId: "cus_1234567890", featureSlug: "tokens", usage: 30, idempotenceKey: "123e4567-e89b-12d3-a456-426614174000", metadata: { action: "create", country: "US" }});if (error) { // handle potential network or bad request error // a link to our docs will be in the `error.docs` field console.error(error.message); return;}console.log(result);
You should get to know our API reference or the Typescript SDK to start integrate with your product.You can also check out the Features section for more information on how to use Unkey.