How to query Stripe API with pagination

Here is an example of how to query Stripe's API with pagination.

:exclamation: The example uses the "Repeat" action, which is an advanced action. If you're new to EasyMorph, understanding the action might require a bit of learning. You can start with these topics:

To use the example, you need to know:

  • Your Stripe account ID. It starts with acct_.
  • Your Stripe API key. It can start with sk_live or rk_live.

To use the example, create a Web Location connector that is called "Stripe" configured as follows:

Tab "Connection"

Base URL: https://api.stripe.com/v1 (if you need to access a v2 endpoint, use https://api.stripe.com/v2).
Authentication: Basic
Login: your Stripe API key

Tab "Headers"

Header name: Stripe-Account
Value: your Stripe account ID

Here is a sample workflow that obtains payment intents.

:exclamation: Change the endpoint in the "Web Request" actions if you need to retrieve something else, for instance, customers.

stripe-api.morph (9.6 KB)

The workflow is organized as follows:

It performs the initial request. If the response contains has_more = true, then the workflow starts a loop to retrieve all the remaining pages.

After that, the remaining pages are appended to the result of the initial request.

Remarks

  • The workflow has two "Web request" actions that query Stripe API. If you need to change the endpoint URL or the page size, make changes in both actions.
  • The workflow extracts only one property, "amount", from API responses. When you add more properties, make sure to add them in both "Web request" actions.
  • You can specify the page size (in rows) using the page_size parameter in the "Repeat" action.
  • In the "Request" module, notice that the "Skip" action must return empty table if it skips actions. This is important as it will signal to the "Repeat" action to stop the loop. Otherwise, it can loop indefinitely.

Bonus point

Stripe's API allows starting_after to be empty. Therefore, we can simplify our workflow and keep only one "Web request" action in the iterated module. It is more convenient because we can configure the endpoint URL and JSON properties to extract from response in one place.

The simplified workflow can be harder for beginners to understand, so feel free to use the version published above - it's slightly simpler.

In the simplified workflow, we don't perform the initial request. Instead, we artificially create an input dataset that would start the pagination loop right from the 1st page and not from the 2nd page as in the example above. Therefore, we can use only one "Web request" action for all web requests. There is no need to make a separate initial request.

Here is the workflow:
stripe-api2.morph (7.1 KB)

1 Like