Use Klaviyo to Automate Direct Mail
Tags: tutorial, klaviyo, ecommerce, api
Published: 2022-08-23 16:57:30

Klaviyo is a popular marketing automation software tool that empowers its customers to build campaign automation by 
syncing their tech stack with their ecommerce store to scale their communications and business. While it has traditionally served as a great resource for automated email and SMS, direct mail has historically been missing from the equation. 

But marketers now have a solution: integrating Lob’s direct mail APIs. **Klaviyo can connect to Lob to automate direct mail at scale in the same manner that they are automating their digital channels today**: Klaviyo’s webhook functionality within their _Flows_ interface provides an easy connection point to trigger mailpieces. 

Read on for a walkthrough of exactly how to connect Klaviyo with Lob.

## Getting started

We’ll assume you have a [Klaviyo](https://www.klaviyo.com/) account and you have already registered with [Lob](https://dashboard.lob.com/) (to get your API Keys) to follow along. 

In order to send mail from Klaviyo, Lob requires a mailing address to be stored for each profile. Klaviyo does not require mailing addresses by default, so before attempting to send mail, ensure that your recipient profiles contain properties for:

* Address
* City
* State
* Zip Code

![klavioA](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/080nzpko2e5xxw2b1cim.png)

If you are importing new profiles to Klaviyo, simply ensure that you have CSV columns for each of those properties. An example CSV to import Profiles is [linked here](https://s3.us-west-2.amazonaws.com/public.lob.com/solutions/Klaviyo_demo/Klaviyo_demo_Contacts_10.csv).

## Creating a Flow

In the Klaviyo dashboard’s left-hand panel, click on **Flows**.

![KlavioB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5hwngx8h6148l4qm2bf5.png)

Click to **Create Flow,** then select a predefined goal, or click **Create From Scratch** and give your flow a name.

![KlavioC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rw3ln220qke2fstbbr9n.png)

![KlavioD](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbdtgzji97yi3ffaetjf.png)

![KlavioE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2gub9gu783igakkrdr1y.png)

Choose a **Trigger Setup** option in the left panel. For this example, we will be selecting **List**, and choosing a list from the resulting drop-down to trigger the flow. 

![Klavio F](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqxcbfe2winaa23v01l4.png)

## Create Webhook

Once you have configured your trigger, an Actions menu will appear on the left panel. Select **Webhook**, and drag it into your flow beneath the trigger.

![KlavioG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95sr034feynbvu4ulc3b.png)

Select the Webhook in your Flow, and the configuration panel will appear on the left side. You can configure it as follows:

**Destination URL:** This is where you will enter the Lob API URL for the form factor you would like to trigger. In this example we’ll choose postcards: https://api.lob.com/v1/postcards. (Note that Lob has separate endpoints for postcards, letters, checks, and self-mailers.)

**Headers:** This is where you will enter the headers Lob requires in order to correctly process and authenticate the incoming requests. You will be required to enter at least two headers: Content-Type and Authorization. Click **Add Header.**

For **Content-Type Header**, enter the following:

Key: Content-Type
Value: application/json

For **Authorization**, you will need your API Keys. 

## Adding API Key

For the Authorization header, you will need to have your Base64-encoded Lob API key ready to enter, so Lob can associate the request with your account. Retrieve these credentials from your Lob dashboard by clicking on the Settings menu on the sidebar, then clicking on the API Keys tab.

**IMPORTANT: ENSURE YOU ARE USING YOUR TEST API KEY.** 

You can use a [Base64 Encoder tool](https://www.base64encode.org/) to encode your (test) API Key. 

For example, if your API key was test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc, you would enter test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc: into a Base64 Encoder tool, the result of which would be something like dGVzdF8wZGM4ZDUxZTBhY2ZmY2IxODgwZTBmMTljNzliMmY1YjBjYzo=

Note that we added a “:” to the end of the API key before encoding it, please ensure you do the same.

Once you’ve encoded your API key, add the Authorization Header and use the encoded key as the value. For example:

Key: Authorization
Value: Basic dGVzdF8wZGM4ZDUxZTBhY2ZmY2IxODgwZTBmMTljNzliMmY1YjBjYzo=

## JSON body

This is where you will pass Lob the information that informs the mailpiece. The field names (name, address_line1, etc.) represent the information that Lob’s API will require. The values on the right represent what you will pass to Lob to populate the given field. 

The values can be mapped to attributes from Klaviyo profiles by using Klaviyo’s Profile Variables. You can find the Profile Variables for each attribute by taking the following steps:

First, click on the linked word **profile** in ‘View profile properties’, directly beneath the JSON Body input field.

![KlavioH](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9f2s42qddq75hkm6v0wi.png)

This will pull up an example Profile. Clicking on an attribute will automatically copy the corresponding Profile variable, which you can use to populate the appropriate value in the JSON Body. 

![KlavioI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyxzxo7hxism78990j6i.png)

An example of a populated JSON Body could look something like the following. (You can find more information on each of these fields in [Lob’s API documentation](https://docs.lob.com).)

{%gist https://gist.github.com/lobot/4b7709cd25b187634010ebd2ddf4c3b1.js %}

## Update Creative

Note that if you are copying the above example, you will want to replace the values being passed for ‘front’ and ‘back’ with your own Lob template IDs or linked creative.  For example: 

"front": “[https://s3.us-west-2.amazonaws.com/public.lob.com/lobcom/template_gallery/gtmpl_524eb6ff6c6998_Increase_Customer_Loyalty_Postcard_4x6_front.html](https://s3.us-west-2.amazonaws.com/public.lob.com/lobcom/template_gallery/gtmpl_524eb6ff6c6998_Increase_Customer_Loyalty_Postcard_4x6_front.html)"

"back": "[https://s3.us-west-2.amazonaws.com/public.lob.com/lobcom/template_gallery/gtmpl_524eb6ff6c6998_Increase_Customer_Loyalty_Postcard_4x6_back.html](https://s3.us-west-2.amazonaws.com/public.lob.com/lobcom/template_gallery/gtmpl_524eb6ff6c6998_Increase_Customer_Loyalty_Postcard_4x6_back.html)"

(For more about designing/formatting creative for Lob, [go here](https://help.lob.com/mail-piece-design-specs/postcards-self-mailers).) 

## Testing

You can test that your webhook is configured correctly by clicking on **Preview Webhook**. 

![KlavioJ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77w6ozw34i3dgfy4w75w.png)

The resulting panel will show an example of the data being passed to Lob for a specific Profile. You can look at the JSON to verify that the Klaviyo Profile Variables are being correctly replaced. For example, instead of person|lookup:'$address1'|default:'' you will see an address, such as **185 Berry St.**

![KlavioK](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4mql23f7pjssmiwq76a.png)

If this all looks correct, double-check that you used your Test API Key (rather than your live one), then click on **Send Test Request**. If it was configured correctly, you will see the following message:

![KlavioL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72ty5bihfynpb7oq892a.png)

You can then log into your Lob account to verify that your postcard was generated. (The below is the result of the example creative.)

![postcard front](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6xbq56yv7a7vkqt51yw.png)

![postcard back](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pszo27rwcluwo7ep1zds.png)

## Go time!

You will need to add payment information to your Lob account should you actually wish to print and send mailpieces. When you are ready, you can replace your Test API key with your Live API key; **if you have your LIVE API key in the template it will create mail.** If you are using other test resources like address IDs or template IDs, those will also need to be transferred to your live environment.

Make sure when you are testing that your [cancellation windows](https://lob.helpjuice.com/use-case-guides/override-cancellation-window) are set to an ample time (2 hours is recommended) so you can cancel mail if you make a mistake.

Have fun exploring this integration; if you have questions or need support, don’t hesitate to [contact us](https://lob.com/contact).