How to send consent to Klaviyo via Zapier

read
Last updated at:

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

  1. Log into your Zapier account.
  2. From the main page in Zapier, select Create Zap from the top of the navigation menu.
  3. Enter a name for your new Zap (e.g., Klaviyo Webhook).
  4. Select Webhook from the Trigger menu.
    Zapier Trigger menu, showing trigger options of App Event, Schedule,vRSS, and Webhook (Premium)
  5. 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
  6. Click Continue.

Create webhook trigger

  1. Set up your webhook trigger.
  2. Copy the custom webhook URL that Zapier provides for you to send your requests to.
  3. Click Continue to move onto testing the trigger.
  4. Zapier Trigger setup page, with option to copy the custom webhook URL from the first text box
  5. 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
  6. Once you’ve sent the test data to the webhook, click Test trigger.
  7. 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
  8. Once your webhook is working, click Continue.

Set up Zapier code action

  1. 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
  2. Click Choose an event.
    Selecting Run Python from Action Event dropdown in the Zapier code setup menu
  3. Select Run Python from the dropdown.
    The Run Python Action Event is selected, and the blue Continue button is displayed in the Zapier webhook setup
  4. Click Continue.

Set up field mapping and create script logic

  1. 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>

  2. 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}
    
  3. 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
  4. Click Test & Continue.
  5. 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
  6. 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 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 #).

Additional resources

x
Was this article helpful?
8 out of 16 found this helpful