Skip to content
Create account or Sign in
The Stripe Docs logo
/
Ask AI
Create accountSign in
Get started
Payments
Revenue
Platforms and marketplaces
Money management
Developer resources
APIs & SDKsHelp
Overview
Billing
OverviewAbout the Billing APIs
Subscriptions
Invoicing
Usage-based billing
Advanced usage-based billing
Quotes
Customer management
Billing with other products
Revenue recovery
Automations
Scripts
Test your integration
    Test your Billing integration
    Test clocks
      Simulate subscriptions
      API and advanced usage
    Test your invoicing integration
Tax
Overview
Use Stripe Tax
Manage compliance
Reporting
Overview
Select a report
Configure reports
Reports for multiple accounts
Reports API
Revenue recognition
Data
Overview
Query business data
Sigma
Data Pipeline
Import external data
United States
English (United States)
HomeRevenueTest your integrationTest clocks

API and advanced usage

Learn advanced strategies for using test clocks in the Dashboard and API.

You can run a simulation separately from a subscription for running advanced simulations. In this scenario, you create the simulation first and then add different test cases to it.

Not ready for a full integration? See our guide for running simulations on subscriptions.

Follow these steps to start using test clocks:

  1. Create a simulation
  2. Set up your simulation
  3. Advance time
  4. Monitor and handle the changes
  5. Update the simulation
  6. Delete the simulation

You can advance the time, monitor changes, and update the simulation as often as you need to test different cases.

Create a simulation and set its time

A simulation uses a clock as its object, letting you reference the clock’s time. To start a simulation, create a clock and set its frozen time—the starting point for your tests. Set the frozen time to a point in the past or future, but after you set it, you can only move it forward in time.

Create a sandbox to start simulating.

  1. Go to the Subscriptions section under the Billing tab.
  2. Click Simulations link in the banner.
  3. Click New simulation.
  4. 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 renewal or Free trial.
  5. Set the frozen time for the simulation.

Set up your simulation

Next, set up the test case for your simulation. You need to create a customer first, then a subscription for them.

To create a customer for your simulation through the Dashboard:

  1. Go to the Simulations page and find your simulation.
  2. Click Add > Add customer.

You can’t choose existing customers during simulations. You can add up to three new customers to each simulation.

You can optionally enter other available properties for the customer, like their name, email, and billing information, but none are required. For some simulations, like testing free trials, you might not want to collect any billing information up front.

Next, you can create up to three subscriptions or subscription schedules for your customer. To create a subscription for the customer through the Dashboard:

  1. Go to the Simulations page and find the simulation.

  2. Click Add > Add subscription. Select or search for your customer from the drop-down menu. You can also add the customer to a subscription through the customer page, by clicking Actions > Create subscription.

  3. Select a recurring product and price in the Pricing section.

  4. For the Subscription schedule, define the start and end date for the subscription and when to start the billing period.

  5. Choose a payment collection method:

    • Select Automatically charge a payment method on file if you want to charge your customer when the billing period starts.
    • Select Email invoice to the customer to pay manually if you want to invoice your customer in arrears.
  6. Click Start test subscription to start the subscription and billing period.

Both the customer and subscription are associated with the simulation you created in the first step.

Advance the simulated time

After you’ve created a simulation and set up your test case, advance the simulated time. The first time you do this, you’ll advance the time from the initial frozen time you set at the start. As you advance time, you can see how your integration works when subscriptions end, renew, or undergo other changes (like upgrading from a free trial to a paid subscription).

Advance time in intervals of up to two. The length of the interval is based on the shortest service period associated with the subscription, which is determined by the recurring price. For example, if you have a monthly subscription, you can only advance up to two months at a time. If you haven’t set any subscriptions or subscription schedules, you can advance up to two years from the initial frozen time.

To advance time through the Dashboard:

  1. Go to the Simulations page and find your simulation.

  2. Click Advance time.

  3. Use the calendar modal to select the date you want to advance the clock to.

  4. Click Advance.

Monitor and handle changes

After a successful API request or Dashboard operation, it takes a few seconds to advance to the specified time. To know when the simulation has changed status, you can use webhooks to listen for event notifications or you can poll the clock object. The Dashboard also reflects the changes.

For example, you can go to the Invoices page to check whether an invoice was created or paid for your subscription.

If you use webhooks, listen to the following event notifications. Before production, make sure your integration correctly handles the other billing-specific event notifications in addition to the ones listed below.

EventDescription
test_helpers.test_clock.advancingThe clock has started to advance but hasn’t reached the specified time.
test_helpers.test_clock.readyThe clock has completed advancing to the specified time.

To poll the status of the clock, retrieve it by ID to examine its status.

Command Line
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
curl https://api.stripe.com/v1/test_helpers/test_clocks/{{CLOCK_ID}} \ -u "
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:"

Update the simulation

You can continue to make changes to your simulation and advance the clock for simulations like:

  • Adding a customer balance.
  • Making a mid-cycle upgrade.
  • Adding one-off invoice items.

After each update, monitor the changes again. Repeat as many times as you need to satisfy your test case.

Delete a simulation

Simulations are automatically deleted 30 days after you create them, but you can delete them when you’re done testing to ensure a clean test environment.

To delete a simulation and all of its associated test objects through the Dashboard:

  1. Go to the simulations page and find your simulation.
  2. Click Finish simulation.
  3. In the confirmation modal, click Finish.

Deleting the simulation also deletes the test customers associated with the clock and cancels their subscriptions. Simulations are only available in sandboxes, so you can’t delete any production objects when you delete a clock.

Use cases

In each case, begin by creating a new simulation:

  1. Create a simulation
  2. Set up your simulation
  3. Advance time
  4. Monitor and handle the changes
  5. Update the simulation

Test subscription renewals

Let’s say that you’d like to test that a 50 USD/month subscription renews correctly:

  • Create a simulation and set its frozen_time to January 1.
  • Add a customer and add a payment method for the customer:

To add a payment method for a customer in the Dashboard:

  1. From the customer account page, click Add > Add card from the Payment methods section.
  2. Enter payment information. In this case, use the test card.
  3. Click Add card in the modal.
  • After adding a payment method for the customer, create a subscription for the new customer set at 50 USD/month. In doing so, the invoice of 50 USD is paid automatically and the subscription is active.

  • Advance the date to February 1 to see that an invoice of 50 USD is created. By default, the invoice appears in a draft status for one hour.

  • Advance the time by one hour to see that the invoice is finalized and paid automatically.

Test mid-cycle upgrades with prorations

Let’s say that you have two products. One product is 50 USD month (basic plan), and the other is 100 USD month ('premium plan). In this case, you might want to test prorations for a customer who upgrades their basic plan to the premium plan in the middle of a billing period. To simulate this:

  • Create a simulation and set the frozen_time to January 1.
  • Create a customer and add their payment method. In this case, use the test card.
  • Create a subscription for the ‘basic plan’ at 50 USD/month. After this is done, you’ll see that the 50 USD/month invoice is created, finalized, and automatically paid.
  • Advance the date by two weeks. In this case, we’ll set the date to January 16.
  • Upgrade the subscription to a ‘premium plan’ at 100 USD/month:

To upgrade a subscription using the Dashboard:

  1. From the customer account page or the subscription details page, click the overflow menu () associated with a subscription, then select Update subscription.
  2. Make your desired modifications.
  3. Click Update subscription in the top right corner to apply the changes.
  • After upgrading the subscription, the customer.subscription.updated webhook event is created.

  • Pending invoice items are also created for the prorations. You’ll see a negative proration of -25 USD for the unused time with the ‘basic plan’ and a positive proration of 50 USD for using the ‘premium plan’ for half of the remaining month. At this point, no invoice has been generated.

  • Advance the date by two weeks. In this case, set the date to February 1. You’ll see that the subscription has cycled. An invoice has been generated in a draft status and has incorporated the pending invoice items, including a negative proration, a positive proration, and the total payment for the month of February, resulting in 125 USD. By default, the invoice appears in a draft status for approximately one hour.

  • To finalize the invoice, advance the time by one hour.

Test trials

Let’s say that you want customers to try your product for free with a seven-day trial before they start paying and want to collect payment information up front. To simulate this situation using test clocks, follow these steps:

  • Create a new simulation and set the frozen_time to January 1.
  • Add a customer and include their payment method. In this case, use a test card.
  • Create a subscription and add a seven-day free trial period:

To add a trial period to an existing subscription using the Dashboard:

Find the subscription you want to change.

  1. Click Actions.
  2. Click Update subscription.
  3. Click Add free trial and enter seven in Free trial days field.
  4. Click Update subscription.
  • After creating a subscription with a seven-day free trial period, a subscription is created in a trialing status. An invoice of 0.00 USD is generated due to the free trial.
  • Advance the date to January 5 to see the customer.subscription.trial_will_end event notification. Stripe sends the notification three days before the trial ends. You can use this webhook event to inform your customers that the trial ends soon.
  • Advance the date to January 8 to see that the subscription is now paid and an invoice for 50 USD is created.
  • Advance the date by one cycle (for example, to February 8 for a monthly subscription) to see the subscription renew successfully.

Limitations

You can simulate up to:

  • Three customers
  • Three subscriptions, including scheduled subscriptions, per customer
  • Ten quotes that aren’t attached to customers

Test clock objects omitted in list all results

Stripe list APIs (such as List invoices) omit results generated by test clocks for list all requests. To see results generated by test clocks in these cases, you must request results within a specific parent, such as test_clock, customer, or subscription.

For example, GET /v1/invoices won’t return test clock generated invoices, but GET /v1/invoices/{customer_id} returns all invoices for that customer, including those that are test clock generated.

Similarly, you can specify a test clock ID in this example to get all invoices related to that test clock, or you can specify a subscription ID to return all invoices billed for that subscription, including test clock generated invoices.

Rate limit errors

If you make multiple updates to a subscription that has a test clock, Stripe might return a rate limit error. Since the subscription is frozen to the time of the test clock, all API requests count toward that time, which can trigger the rate limit.

To avoid this, advance the simulated time of the clock by a few minutes before making additional API requests on the subscription.

The Subscriptions API restricts the maximum number of requests as follows:

  • 10 new invoices per subscription, per minute
  • 20 new invoices per subscription, per day
  • 200 quantity updates per subscription, per hour

Caveats with payment processing

Test clock advancement currently doesn’t support collecting payments through bank debits (for example, us_bank_account payment method types). Stripe collects payments after the test clock advances. To test payment failures:

  1. Choose the Cancel subscription after all payment retries fail setting.

  2. Attach a us_bank_account payment method type to a customer that fails payments.

  3. Create a subscription under the customer.

  4. Advance the test clock to cycle and collect payment on a subscription.

After the Test Clock advances, the subscription remains in the active status. This indicates that the payment collection wasn’t attempted during test clock advancement, and the subscription hasn’t entered the canceled status due to payment_failed.

Listen to the invoice.payment_failed event to monitor the delayed subscription status and invoice payment. The customer.subscription.deleted event indicates that the subscription status is set to canceled.

Was this page helpful?
YesNo
  • Need help? Contact Support.
  • Chat with Stripe developers on Discord.
  • Check out our changelog.
  • Questions? Contact Sales.
  • LLM? Read llms.txt.
  • Powered by Markdoc
On this page