Solution Recipe 5: Using Klaviyo’s APIs to Automate Daily Emails
Solution Recipes are tutorials to achieve specific objectives in Klaviyo. They can also help you master Klaviyo, learn new third-party technologies, and come up with creative ideas. They are written mainly for developers & technically-advanced users.
Note: We do our best to make sure any code and API references are accurate and current when this is published, but you might need to update code and it’s always a best practice to leverage our latest API versions. If you have questions, feel free to hop over to our Developer Community.
How to use Replit with Klaviyo’s APIs to automate daily emails.
Klaviyo makes it easy to create and send campaigns. But, if you want to automate a daily campaign that sends out fresh content every day, it can be tedious to create that content and schedule the send every day within the platform. Klaviyo’s APIs make it possible to automate this process entirely with straightforward set up work and little overhead to maintain this in the long run with a virtual development environment like Replit.
Moderate
Introduction
If you’ve created and launched campaigns in Klaviyo, you’ll know that the process is straightforward and easy to do. However, if you have a use-case for sending a daily campaign to your customers, the effort it takes to create each campaign with unique content can add up to a lot of precious time spent configuring, testing, and scheduling. This is where Klaviyo’s APIs come in handy.
In order to take advantage of Klaviyo’s APIs, development work is required. Traditionally, when running a recurring job, the building blocks involve creating the script or program and hosting it on a server, along with any maintenance needed for said server.
On the other hand, tools like Replit make this process much easier to implement. Replit is a browser-based, serverless and collaborative integrated development environment, which means less setup and maintenance for you, and more control and transparency for your team. Replit offers some features for free, but for this use-case, you must upgrade to their lowest paid tier, the “Hacker” plan, which costs $7 a month.
In this recipe we’ll explore sending a daily email that highlights daily deals on your site. This means, not only are you sending out a campaign each day to your customers, the content in each daily email will be different from the day before, in order to show new product deals.
Ingredients
- 1 data source housing the HTML you plan to use for these campaigns
- 1 Replit account (“Hacker” plan required)
- 1 campaign generating and sending script
Instructions
Step 1: Set up an external data source to host the daily content
The first step is to set up an external data source to host the content you want to display in the daily email. Some examples of what this can be: an HTML page hosted on your site, an endpoint that returns HTML stored in a JSON object, or an RSS Feed that updates with different content every day.
As we work through this recipe, we’ll be referring to an example daily campaign that is pulling from this HTML page: https://jingrid-klaviyo.github.io/templateHTML/index.html
When you click into that page, it will serve up the (rendered) HTML to be put into the daily email.
As you start to think about creating this campaign, understand that your data source must be updated every day, or at least at the cadence you’d like to send these campaign, in order to show new deal content to recipients each send.
Step 2: Create a “Hacker” plan Replit account
Once you have your external data source configured, it’s time to create the program that will create and send these daily campaigns.
Before getting into the details of creating the program itself, create an account on Replit and make sure you sign up for the Hacker plan, which currently costs $7 a month. We will be using the ‘Always On’ feature that comes with the Hacker plan in the final step.
At the end of the following Replit setup steps, you should be able to hit the ‘Run’ button in your Repl and have a campaign sent to your email.
Step 3: Fork example Repl
In your Replit account, fork (make a copy of) this example Repl we’ll be referencing: https://replit.com/@IngridJin/Daily-Deals-Email-Campaign. This Repl contains all the code that we will run in order to send data to Klaviyo’s APIs, which will trigger the daily sends.
At the top right of the page, click the blue ‘Fork’ button.
Step 4: Create environment variables to store Klaviyo API keys
Once you’ve forked the Repl, the first thing we must do is create some environment variables, which are hidden values we’ll be using to store our Klaviyo API keys. This will keep your API keys private and avoid exposing them. We’ll need these keys to create and send the campaign from your Klaviyo account.
Click on the lock icon on the left-hand navigation panel to access the “Secrets” tab, and add your public API key using the key name “KLAVIYO_PUBLIC_KEY” and your private API key using the key name “KLAVIYO_PRIVATE_KEY”. These are the environment variables that will hold your Klaviyo public and private API keys, respectively.
Log into your Klaviyo account and grab your public and private API keys (Account > Settings > API Keys). If you don’t have a private key, you can generate a new one to use. Paste each API key under the corresponding key name in the Secrets tab.
Your Secrets tab should now look like this:
Step 5: Create test list in Klaviyo
Next, we will create a List in Klaviyo to hold the recipients of this daily email. But before we use a List that contains the actual customers to whom you want to send, create a List for testing purposes and add only your email address. This way, we can safely test our work before going live.
See here for steps on creating a List in Klaviyo.
Once you’ve created that list, grab the List’s ID, which can be found in the URL bar of the list, see the highlighted text:
Copy the List ID from the url or the List’s Settings page and paste it into your Repl under the greyed out comment ‘# list_id is the ID for the daily deals subscriber list’ (replace ‘def456’ with your List ID).
Step 6: Add the external data source URL to Repl
Back in Step 1, we created our external data source which contains the content for the daily email. Now we need our Repl to reference that data and serve it up in the emails.
Grab the URL of the data source and replace the example URL under the greyed out comment ‘# url of external data source’.
Step 7: Test your Repl
Now that you’ve configured your Repl to hold your Klaviyo API keys, Klaviyo List ID, and URL of the external data source, we are ready to do a test send!
At the top of the page, click the button with green text ‘Run’. After clicking it, you should start to see messages logged to the console on the right side of the page. If your script is running successfully, you should see the following messages logged in the console:
To break down these messages line by line:
- The Epoch timestamp of when the script started to run
- The ID of the template created in Klaviyo based on your external content
- The ID of the campaign created in Klaviyo using the created template
- The response code of the campaign send (200 indicates the campaign sent successfully)
- If you receive a different response code, refer to our Campaign API documentation
Check your inbox for the campaign that was just sent. If you’re using Gmail, be sure to check the Promotions tab if you don’t see it in your main inbox view.
Step 8: Configure sender details and subject line
You’ll notice that the email in your inbox came from ‘marketing@example.com’ with the subject line containing a string of numbers. Those are the sender details that have been configured in the Repl by default.
Now that you know the email has been sent successfully, you can configure the sender profile details and subject line. Here’s where you can configure those details:
Step 9: Configure recipient List in Klaviyo, replacing test List
In step 5 we created a test List in Klaviyo to send this campaign to for testing purposes. Once you’ve finalized the campaign details, switch the List ID in the Repl to the List ID of the actual List of customers to whom you want to send.
Step 10: Configure Repl to run automatically on a daily cadence
At the bottom of the Repl, you’ll see some commented out code, which references the Python Schedule library.
This code was commented out to allow for testing our campaign as an immediate send. In order for this campaign to be automated as a daily send, you must uncomment lines 104–108, and remove (or comment out) line 102.
By default, the scheduling code will trigger the script to run and send the campaign every day at 10:30 AM UTC. You can change the time or cadence of the send by modifying line 104.
Step 11: Toggling on the ‘Always On’ feature for the Repl
With the paid Hacker plan in Replit, you can use a feature called ‘Always On’. This allows your Repl to stay active instead of going to “sleep” after a period of inactivity. With your Repl always on, the scheduling logic will be able to run every day to trigger the daily campaigns.
Once your Repl is Always On, click the ‘Run’ button again to initiate the script, which will start sending out on a daily basis.
Impact
With your Repl set up and running, you’ll begin to see a new template and campaign generated in Klaviyo every day. As a creator, this set-and-forget solution cuts down on time spent on campaign configuration, giving you more time to engage with your customers, who can now look forward to new deals on your site every day.
Learn more
If you’re interested in learning more about Klaviyo’s developer experience & capabilities, please visit developers.klaviyo.com!