How to Use JSON and Open APIs to Fetch Data for Your Emails
Hey there, want to make your emails more engaging and dynamic? I recently discovered a cool trick that can help you do just that. It involves using JSON and Open APIs to fetch data and dynamically populate your emails.
This is a more advanced technique, but I think it's worth learning.
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:
- Assign a variable (e.g.,
post
) to the URL using thefetch_json
filter - Render the desired value (e.g.,
post.title
) in your email template
Here's an example code snippet:
{% 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.
Fetching Random Bible Verses
Now, let's look at a more practical example: fetching random Bible verses. I found a URL that generates random Bible verses and used it to dynamically add quotes to my emails.
Here's how it works:
- Assign a variable (e.g.,
bible_verse
) to the URL that generates random Bible verses - Loop over the array of verses returned by the API
- Render the desired values (e.g., book name, chapter, verse, text) in your email template
Here's an example code snippet:
{% 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 will output a random Bible verse every time the email is sent, making your content more engaging and dynamic.
Wrapping Up
Using JSON and Open APIs to fetch data is a powerful way to make your emails more interesting and relevant to your readers. While it may seem like an advanced technique, it's actually quite simple once you get the hang of it.
So why not give it a try?
Start by finding a relevant API for your niche, and experiment with fetching and rendering different types of data in your emails. You might be surprised by how much it improves your open and engagement rates!
And as always, ping us in Discord if you have any questions!