You will learn
Learn how to create a custom Zapier webhook to send email and SMS consent to Klaviyo.
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.
About the integration
This integration is not fully supported by Klaviyo, but can be used as a work around if needed. We recommend only using this integration if you have developers on your team who can support it.
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.
Set up Zapier webhook
- 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).
- Select Webhook from the Trigger menu.
- In the next menu, select Catch Hook from the Trigger Event options.
- Click Continue.
Create webhook trigger
- Set up your webhook trigger.
- Copy the custom webhook URL that Zapier provides for you to send your requests to.
- Click Continue to move onto testing the trigger.
- 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.
- 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:
- 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.
- Click Choose an event.
- Select Run Python from the dropdown.
- Click Continue.
Set up field mapping and create script logic
- 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> sms_consent
1. Sms_consent: true name
1. Name: <Test name>
- Add a script like the one below to 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 payload = {"profiles": [ {"email": input_data["email"]}, { "phone_number": input_data["phone_number"], "sms_consent": sms_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 replacePRIVATE_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.
- Click Test & Continue.
- 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.
- 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.
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 iteral
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 #
).