Webhooks in Klaviyo
Webhooks are designed to send information to another source. In Klaviyo, webhooks are always HTTP POST requests, so a webhook body will be attached to each request.
Like Code functions, webhooks can be triggered by events. However, webhooks can only send requests to an external API endpoint. Code allows for more customization since you are not limited to sending to an external endpoint. With Code, you can communicate with external services, perform complex calculations, and even write data back into Klaviyo.
There are 2 types of webhooks you can set up in Klaviyo:
- Webhooks in flows
- Webhooks in Advanced KDP
Read on to learn about how these types of webhooks differ from Code.
Learn how Code can help your business
Webhooks and Code functions can both perform actions in response to events. Check out this video for an example of when it would be beneficial to use Code rather than a webhook.
Webhook in a flow
Trigger: Webhooks in flows can be triggered by anything that can trigger a flow itself. These triggers can include customers being added to a list or segment, a specific date, or an event. Since webhooks in flows can only be triggered by events that can trigger flows, they cannot be triggered by message-related events like Clicked Email.
Payload: You can customize the payload of your webhook. The payload can include both data about the triggering event (if in a metric-triggered flow), and data about the profile that triggered the flow.
Limitations:
- Only POST requests.
- Each webhook calls one endpoint.
- Unidirectional; cannot ingest data.
- Can only be triggered by anything that can trigger a flow.
- Cannot be triggered by message-related events.
Webhook in Advanced KDP
Trigger: Webhooks in Advanced KDP are triggered by events. Any event in your Klaviyo account can trigger webhooks in Advanced KDP, including message-related events like Opened Email.
Multiple events can trigger the same webhook.
Payload: Each webhook payload is pre-constructed with data about the event that triggers the webhook.
Limitations:
- Only POST requests.
- Each webhook calls one endpoint.
- Unidirectional; cannot ingest data
- Pre-built payload means endpoints need to be set up to accept HTTP requests in that format.
Code
Trigger: Code functions are triggered by events. This includes all message-related events except for Opened Email and Received Email.
Customization: Code functions are not webhooks, and they receive the triggering event as the payload.
Instead, Code functions are written in Python or JavaScript and can do anything that can be written in either of these programming languages.
This includes:
- Sending data to and receiving data from external servers.
- Calling multiple endpoints.
- Sending different kinds of HTTP requests.
- Importing modules.
- Running code directly in the Code editor.
Limitations:
Cannot be triggered by Opened Email or Received Email
When to use webhooks vs. Code
Let’s walk through some examples of when webhooks or Code may be used.
Webhook in a flow
Since flows can be triggered by lists, segments, and dates in addition to events, webhooks in flows can be used to send information in response to any of these triggers.
For example, you can create a flow triggered by a customer’s birthday. Two weeks before a customer’s birthday, the webhook can send the customer’s birthday and address to your physical mail provider. The physical mail provider is then ready to mail your customer a birthday card right on time.
Webhook in Advanced KDP
Webhooks in Advanced KDP are used to send information to external systems in response to a wide range of events. For instance, if you use a data warehouse that is not one of our supported warehouses, you can still send data to that warehouse using a webhook.
To do so, create a webhook triggered by Opened Email, Received Email, and Unsubscribed. This webhook can send the event payload to your data warehouse. The webhook will be triggered any time any one of these 3 events occur, and the entire event payload will be sent to the warehouse so that your data remains up to date across platforms.
Custom Code function
Code allows complete control over what happens in response to an event. Let’s say you use Shopify Markets, which includes a customer_locale variable in each Placed Order event that tells you what country the customer was shopping from.
You can write a Code function that extracts this variable and saves it as a profile property on each customer’s profile. Then, you can use this property to create accurate segments of customers by country, or apply filters to your flow to send more localized and personalized flow messages to each customer.