Querying API Related Resources
Learn how to load multiple data objects in a single API request.
The Lemon Squeezy API lets you easily access and manage your store programatically but sometimes multiple API requests are needed to load related data.
In this quick tutorial, we’ll go through some examples of using includes
to load multiple data objects into a single API request.
Products and variants
This first example loads “Variant” data when querying “Products”. This is really useful if you are saving product data locally in your application because you need variant IDs to change a subscriber’s plan.
Typically you may query the products list and then use the URLs returned in the data.relationships.variants.links
object to get data for each product’s variants. If you do this, you’ll end up making multiple API requests:
GET https://api.lemonsqueezy.com/v1/products
GET https://api.lemonsqueezy.com/v1/products/1/variants
GET https://api.lemonsqueezy.com/v1/products/2/variants
GET https://api.lemonsqueezy.com/v1/products/3/variants
- etc.
Here is an example response from the /products
endpoint. Note the variant relationship URLs for each product, which could be used to query each product’s variant
This can be slow and inefficient, especially if you have a lot of products. You also risk hitting rate limits by requesting the API so frequently.
A better way to do this is to use the include
query parameter to load the variant data with the products in a single request:
Notice that data
contains a list of products and included
now contains the objects requested via the include
query parameter.
Multiple related resources can be loaded in a single request by separating them with a comma. For example, to load both variants and store data in a single request:
By doing this, included
would contain a mixed list of “Variant” and “Store” objects.
A product and its store
This next example retrieves store data when querying a single product. For instance, we are querying a product with the ID of 1.
In the response below you’ll find a single Product object in the data
key, and a single Store object in included
.
Subscriptions and customers
This next example loads Customer data when querying Subscriptions.
In this response you’ll get a list of Subscription objects in the data
key, and a list of Customer objects in included
.
A subscription and subscription invoices
This last example loads invoice data when querying a subscription.
In this response you’ll get a single Subscription object in the data
key, and a list of Subscription invoice objects in included
.