Build a subscriptions solution for an AI startup with a usage-based pricing model
Create a customized payments integration to handle billing for usage-based pricing models.
This guide describes how SaaS startups can build a usage-based pricing model with a customized UI and payment flow with Stripe. As an example, the guide uses a fictional AI company called Alpaca AI, that’s integrating with Stripe to provide a fixed fee and overages pricing model for their Llama Chat model. Alpaca AI charges customers a flat rate per month for a base package, and any usage beyond that limit is billed separately.
Create a Stripe account
Before integrating with Stripe, you must create a Stripe account.
- Create an account by entering your email address, full name, country, and creating a password.
- Fill out your business profile.
- In the Dashboard, click Verify your email. A verification email is sent to your email address.
- Verify your email address.
Set up testing environment
Before processing live transactions, test your integration with Stripe’s testing tools, including sandboxes and test clocks. Set these up early so you can iterate and tune your integration before going live.
Create a sandbox
First, create a sandbox. Configure the sandbox to match your live mode configuration, so you can test and stage changes on an ongoing basis.
Create a test clock
Next, create a test clock to simulate the forward movement of time in sandbox. As you increment the test clock, resources such as Subscriptions change state and trigger webhook events.
- In the Stripe Dashboard, go to the test clocks page in the Dashboard.
- Click + New simulation.
- In the Create new simulation modal, enter a name for the simulation. You can use this to describe the simulation you’re testing, like
Annual renewalorFree trial. - Set the frozen time of the clock. This is the starting point for your simulation. You can set this to a time in the future or in the past, but you can only move it forward in time after you set it.
To simulate subscription scenarios, click the Add dropown and select Customer. You only need to enter a name for the customer but for some scenarios, like tax collection and calculation, you need to add other information, like billing and shipping addresses.
Next, click the Add dropown and select Subscription. Configure the subscription to suit your scenario.
You can add additional customers and subscriptions to follow the rest of this guide.
Set up a pricing model
In this example, Alpaca AI charges customers for access to their LLM services by using a fixed fee and overages pricing model, with the following rates:
| License | Fee |
|---|---|
| Per user | 100 USD |
| Usage | Fee |
|---|---|
| 0-1000 | 0 USD |
| 1000+ | 0.04 USD |
To implement this model, you create a meter to record the usage, products and prices to represent your service, a customer, and a customer subscription.
Create a meter
Meters specify how to aggregate meter events over a billing period. Meter events represent all actions that customers take in your system (for example, API requests). Meters attach to prices and form the basis of what’s billed.
For the Alpaca AI example, meter events are the number of tokens a customer uses in a query. The meter is the sum of tokens over a month.
You can use the Stripe Dashboard or API to configure a meter. To use the API with the Stripe CLI to create a meter, get started with the Stripe CLI.
Create the pricing model
Use the Stripe Dashboard or API to create a pricing model that includes your Products and their pricing options. Prices define the unit cost, currency, and billing period.
For the Alpaca AI example, you create a product with a metered price of 0.04 USD per hundred units, billed at a monthly interval. Use the meter that you created in the previous step.
Next, create the 100 USD monthly fee.
Set up promotion codes
To set up a promotion code, you create a coupon and promotion code, then apply the promotion code to a subscription.
Create a coupon
Create coupons in the Dashboard or with the API:
Set eligible products
When you make changes to a subscription, Stripe calculates the proration and applies any existing discounts. You can’t discount proration line items further on the invoice that’s generated.
Apply coupons to subscriptions
After you’ve created coupons, create a discount by applying them to a subscription. You can apply the coupon when you create the subscription or by updating a customer’s existing subscription.
You can still create a subscription when a customer doesn’t have a stored payment method if no immediate payment is required after you apply coupons to it.
Apply coupons to Checkout
Apply coupons to subscriptions in a Checkout Session by setting the discounts parameter in the API. To create a session with an applied discount, pass the coupon ID in the coupon parameter of the discounts array.
Build a checkout page and subscribe your customer
Use Stripe Elements and the Checkout Sessions API to build a checkout page as part of a fully customized subscriptions integration.
To set up a subscription you need to create a Customer. You can collect address and payment details with the Express Checkout Element. Then, you can use that information to create the subscription with the Checkout Sessions API.
Remarque
To test this in a sandbox, you can attach the customer you created to the test clock you created while setting up the testing environment.
Set up Stripe
Install the Stripe client of your choice:
And then install the Stripe CLI. The CLI provides webhook testing and you can run it to make API calls to Stripe. This guide shows how to use the CLI to set up a pricing model.
For additional install options, see Get started with the Stripe CLI.
Create a Customer
Stripe needs a Customer for each subscription. In your application front end, collect any necessary information from your users and pass it to the back end.
If you need to collect address details, the Address Element enables you to collect a shipping or billing address for your customers. For more information on the Address Element, see the Address Element page.
<form id="signup-form"> <label> Email <input id="email" type="email" placeholder="Email address" value="test@example.com" required /> </label> <button type="submit"> Register </button> </form>
const emailInput = document.querySelector('#email'); fetch('/create-customer', { method: 'post', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: emailInput.value, }), }).then(r => r.json());
On the server, create the Stripe Customer object.
Remarque
Make sure you store the Customer ID to use in the Checkout Session
Enable payment methods
By default, Stripe uses your payment methods settings to determine which payment methods are enabled in the Express Checkout Element. You can also configure specific payment methods on your Checkout Session using the payment_method_types attribute.
Supported payment methods
Apple Pay and Google Pay are automatically enabled when using the card payment method type. When using Link, you must also pass the card payment method type.
| Payment method | Payment method type |
|---|---|
| Amazon Pay | amazon_ |
| Apple Pay | card |
| Google Pay | card |
| Klarna | klarna |
| Link | link, card |
| PayPal | paypal |
Create the Checkout Session
On the back end of your application, define an endpoint that creates the session for your front end to call. You need the price ID of the subscription the customer is signing up for—your front end passes this value.
Initialize Checkout
Set up a trial period
To offer a free 7-day trial period of your service, pass in trial_period_days when you create the Checkout Session.
Set up Stripe Elements
Create and mount the Express Checkout Element
The Express Checkout Element contains an iframe that securely sends the payment information to Stripe over an HTTPS connection. The checkout page address must also start with https://, rather than http://, for your integration to work.
Collect customer details and display line items
The Checkout Session you created on the server automatically determines the line items, total amount, and available payment methods. The Express Checkout Element uses this information to display the appropriate interface.
Submit the payment
Listen for webhook events
To set up your back-end logic, you need to set up an endpoint that can listen for webhook events sent by Stripe. These events are triggered whenever the status in Stripe changes, such as subscriptions creating new invoices. In your application, set up an HTTP handler to accept a POST request containing the webhook event, and verify the signature of the event.
During development, use the Events tab in Workbench to monitor and filter events.
For production, set up a webhook endpoint URL in the Dashboard, or use the Webhook Endpoints API.
See Subscription events for more details about subscription-specific webhooks.
Provision access
Grant customers access to your service upon successful payment. In most cases, you can provision access to you service when:
- You receive the invoice.paid event.
- The subscription status is
active. You can check the status in the Subscriptions page in the Dashboard or by using the API to retrieve the subscription and checking the status field.
For more details about subscription-specific webhooks, Subscription events.
Test your integration
Test your integration before going live.
| Scenario | Tests |
|---|---|
| Successful subscription flows |
|
| Failure scenarios |
|
| Time-based events |
|
| Promotion codes |
|
| Proration |
|
| Cancellations |
|
Go live
- In the Dashboard, open your Account settings.
- Enter your business type, tax details, business details, personal verification information, and customer-facing information (for example, a statement descriptor).
- Add bank details to confirm where your money will be paid out.
- Set up two-step authentication to secure your account.
- You can optionally add automatic tax collection or revenue-based climate donations.
- Review the information you entered, and click Agree and submit.
- After you activate your profile, Stripe updates you from sandbox mode to live mode.
Learn more about activating your Stripe account.
Next steps
After setting up your integration, we recommend you implement the following features:
- Set up the customer portal to let your customers manage their subscriptions and payment information.
- Set up Smart Retries to let Stripe’s AI choose the best times to retry failed payment attempts to increase the chance of successfully paying an invoice.
- Set up billing automations to build custom, automated workflows to streamline your business processes, enhance customer communication, and improve revenue recovery efforts.