Skip to main content
Different users should have access to different features and usage limits, depending on how much they’re paying. Autumn tracks what plan people are paying for, and assigns them the features defined in your plan configuration. There are 2 key functions you need to make sure users have the correct permissions:
  • check for feature access, before allowing a user to do something
  • track the usage in Autumn afterwards (if needed)
This example will continue from before: a 2-tier pricing model for a chatbot.
This guide shows an asynchronous approach to checking and tracking. You can also check and reserve balance in a single, atomic API call for concurrent events.

Checking feature access

Check if a user has enough remaining balance of messages, before executing the action.
import { Autumn } from "autumn-js";

const autumn = new Autumn({
  secretKey: process.env.AUTUMN_SECRET_KEY,
});

//check if user can send 1 message
const { data } = await autumn.check({
  customer_id: "user_or_org_id_from_auth",
  feature_id: "messages",
  required_balance: 1,
});

if (!data.allowed) {
  console.log("User has run out of messages");
  return;
}
The feature_id used here is defined by you when you create the feature in Autumn.
When using React hooks, you have access to the customer state, which contains all the billing data for your user (subscription status, feature permissions, usage balances, etc).You can use the client-side check function to gate features and show paywalls. Permissions are determined by reading the local customer state, so no call to Autumn’s API is made. The “true” state should always be fetched server-side.
send-chat-message.tsx
import { useCustomer } from "autumn-js/react";

export default function SendChatMessage() {
  const { customer, check, refetch } = useCustomer();

  return (
      <button
        onClick={async () => {
          const { data } = check({ featureId: "messages" });

          if (!data?.allowed) {
            alert("You're out of messages");
          } else {
            //send chatbot message server-side
            //then refresh customer object and usage
            await refetch();
          }
        }}
      >
        Send AI Message
      </button>
  );
}

Recording usage

After the user has successfully used a chatbot message, you can record the usage in Autumn. This will decrement the user’s message balance.
//Your own function to send the chat message

//then record 1 message used
await autumn.track({
  customer_id: "user_or_org_id_from_auth",
  feature_id: "messages",
  value: 1
});
Once you send usage events, you can verify their receipt in the Autumn dashboard, on the customer detail page.
You should always handle access checks and usage tracking server-side for security. Users can manipulate client-side code using devtools.

Next steps Whenever your customers change their plan, they should automaticially have the correct access and limits. Next, display billing information to your customers.

Displaying Billing Data

Display plan, balance and usage information to your customers using Autumn’s customer state