How to Send Consent to Klaviyo via Zapier

read
Last updated at:

Overview

Learn how to create a custom Zapier webhook to send email and SMS consent to Klaviyo.

While traditional Zapier Zaps can be used to trigger API calls to Klaviyo, some Klaviyo endpoints require data points to be sent in a list or boolean format, which the traditional Zaps don’t support.

In these cases, Zapier offers a code action (either Python or Javascript) that runs a script when the trigger is detected. With this solution, you can map fields to the data types that Klaviyo endpoints require; specifically, the Subscribe to List endpoint.

The steps below will help you create a Zap using a webhook (also called a catch hook) that triggers a Python script action. The script sends an API call to Klaviyo to subscribe email and phone numbers to a list with the $consent and sms_consent fields mapped to the correct data type.

This feature is only available to users with paid Zapier plans.

Set Up Zapier Webhook

First, log into your Zapier account. From the main page in Zapier, select Create Zap from the top of the navigation menu.

Enter a name for your new Zap (e.g., Klaviyo Webhook). Then, select Webhook from the Trigger menu.

Zapier Trigger menu, showing trigger options of App Event, Schedule,vRSS, and Webhook (Premium)

In the next menu, select Catch Hook from the Trigger Event options.

Catch hook selected from Trigger event dropdown, with the Continuevbutton just below the dropdown in Zapier

Click Continue.

Create Webhook Trigger

Next, set up your webhook trigger. Zapier provides a custom webhook URL for you to send your requests to. Copy this URL, and click Continue to move onto testing the trigger.

Zapier Trigger setup page, with option to copy the custom webhook URL from the first text box

Before you click Test trigger, you’ll need to send test data to your webhook. See our guide on How to Send Test Data to Zapier for more information.

Test trigger page, with Test trigger button in center

Once you’ve sent the test data to the webhook, click Test trigger. If the request is successful, you’ll see the success message We found a request!, and the test data will populate below the success message, similar to the example below:

Zapier test trigger setup, a successful POST request and the Continue button in blue at the bottom

Once your webhook is working, click Continue.

Set Up Zapier Code Action

In the Action menu, enter Code by Zapier into the search box and select the option from the list.

Zapier Action menu with search bar for code by zapier to select the correct App Event

Click Choose an event and select Run Python from the dropdown. Then, click Continue.

Selecting Run Python from Action Event dropdown in the Zapier code setup menu

The Run Python Action Event is selected, and the blue Continue button is displayed in the Zapier webhook setup

Set Up Field Mapping and Create Script Logic

Now, you’ll need to set up field mapping to correlate the input data in the table below to the correct test data you previously sent:

Input Data Test Data Mapped
email 1. Email: <Test email>
phone_number 1. Phone Number: <Test phone number>
$consent 1. $consent: sms, email
sms_consent 1. Sms_consent: true
name 1. Name: <Test name>

Zapier webhook setup with the Set up action form open to display the difference between input data and test data

Then, copy and paste the following python script into the code box:

import requests

url = "https://a.klaviyo.com/api/v2/list/LIST_ID/subscribe"
querystring = {"api_key":"PRIVATE_API_KEY_HERE"}

// configuring sms_consent mapping to boolean value
if (input_data["sms_consent"] == "true" or input_data["sms_consent"] == "True" or input_data["sms_consent"] == "TRUE"):
  sms_consent = True
elif (input_data["sms_consent"] == "false" or input_data["sms_consent"] == "False" or input_data["sms_consent"] == "FALSE"):
  sms_consent = False

// configuring $consent mapping to list value
consent = input_data["$consent"].split(",")    

payload = {"profiles": [
  {"email": input_data["email"]},
  {
    "phone_number": input_data["phone_number"],
    "sms_consent": sms_consent,
    "$consent": consent
  }
]}
headers = {
  "Accept": "application/json",
  "Content-Type": "application/json"
}

response = requests.request("POST", url, json=payload, headers=headers, params=querystring)

// Zapier requires an 'output' object
output = {"response text": response.text}

Replace the LIST_ID with the Klaviyo list ID you want to send your Zapier data to, and replace PRIVATE_API_KEY_HERE with your Klaviyo private API key.

Treat private API keys like passwords - keep them in a safe place and never expose them to the public.

Python script from above into Zapier Python editor, with List ID and Private API Key shown

Then, click Test & Continue.

Simplified preview of Python code in Zapier, with buttons to Test & Review in white and Test & Continue in blue

If your test is successful, you’ll get a success message of Test was successful! If this is the case, click Turn on Zap to finish setting up your webhook.

Successful Zapier Zap alert, with Retest action button in white and Turn on Zap button in blue

If the test isn’t successful, check out the troubleshooting tips for common issues you may have encountered.

You can also check Klaviyo to see if the test email and phone number were successfully added to the list you specified.

Zapier test data in Klaviyo, with profiles added for the email and phone number passed

Troubleshooting Tips

The following are common issues you may run into while setting up the Zapier webhook. If you encounter any other issues, you can reach out to the Klaviyo Community for further guidance.

EOL While Scanning String Literal

If you see this error, then you’re missing a quotation mark around one of your variables. Check the list ID and private API key to make sure they’re surrounded on both sides by quotation marks.

SyntaxError: Invalid Syntax

If you see this error, remove the comments in the Python code (any line that starts with a #).

Additional Resources

x
Was this article helpful?
0 out of 1 found this helpful