Online
Bento

How to Use JSON and Open APIs to Fetch Data for Your Emails

You can use JSON and open APIs to fetch data and populate your emails dynamically at send time.

This is a more advanced technique, but it unlocks live content like product feeds, quotes, and external data in any email.

Fetching Data from a JSON Placeholder

First, let's look at how to fetch data from a JSON placeholder URL. This URL returns a small blob of data, which includes a user ID, an ID, a title, and a completion status. To use this data in your email, you can:

  1. Assign a variable (e.g., post) to the URL using the fetch_json filter
  2. Render the desired value (e.g., post.title) in your email template

Here's an example code snippet:

Code
{% assign post = "https://jsonplaceholder.typicode.com/todos/1" | fetch_json %}
{{ post.title }}

Every time you change the URL, you'll get a new set of dummy data to work with.

Looping Over an API Response

Next, an example with an array response. This one uses a public JSON API that returns a random Bible verse, but the same pattern works with any API that returns an array.

Here's how it works:

  1. Assign a variable (e.g., bible_verse) to the API URL
  2. Loop over the array of items returned by the API
  3. Render the desired values (e.g., book name, chapter, verse, text) in your email template

Here's an example code snippet:

Code
{% assign bible_verse = "https://labs.bible.org/api/?passage=random&type=json" | fetch_json %}
{% for verse in bible_verse %}
  {{ verse.bookname }} {{ verse.chapter }}:{{ verse.verse }}
  {{ verse.text }}
{% endfor %}

This outputs a different verse from the API each time the email is sent. Swap in your own API to render the data you need.

What You Can Do Now

You can now fetch JSON from any open API with the fetch_json filter, render single values, and loop over array responses in your email templates. The same pattern works for product feeds, content recommendations, or any external data source that returns JSON.

And as always, ping us in Discord if you have any questions!