← WritingCode

Expanding Objects in Stripe API

1 min read

Learn this quick tip to reduce the number of API requests and speed up your payments integration.

Reducing API Requests

Reducing the number of network calls in any web application is a great way to improve not only speed, but reliability of code. Just imagine we have a worker unit that asks Stripe for a Customer, Card, and Invoice via three separate API requests. Not only is this slower than making a single network call, it also triples our chance for errors or failures.

First Attempt

In this first attempt, we simply fetch the refund from stripe and ask for the balance transaction in a separate request.

Single Request

We can definitely improve this by using the expand parameter to fetch the balance transaction at the same time as the refund.

Notice that we pass a Hash to retrieve instead of just an identifier. We spent awhile debugging and digging through the Stripe Ruby gem source before noticing why the expand parameter was being ignored.

We were also sad to see that it is not possible to expand properties for a Stripe::Event.

Stripe API Docs

You can read more about expanding multiple objects on the official Stripe Docs.

Matt

I've been building software since 2008, and I started Lotus in 2015. Now I'm making my own apps, starting with Bobara, alongside a small number of client projects.