Skip to main content

    Make API calls to retrieve and manipulate data in your Klaviyo account

    Course overview
    Lesson
    1 min read

    Try it: Create a webhook

    Put your skills to the test by using Postman to make actual API calls into your Klaviyo account.

    Use Postman to make POST requests

    Before you proceed, make sure you have set up your development environment and forked Klaviyo's Postman collection. If you have not completed these steps, return to the previous lessons before continuing.

    For these exercises, you are working for Beantown Coffee. Beantown just launched a series of educational coffee seminars, and your goal is to track how many customers sign up for each seminar and how much revenue you are generating from the seminars.

    You want to automate the process of adding profiles to a list to maintain a group of all profiles who signed up for a seminar. Then, you want to set up a custom "Booked seminar" event that fires when a customer books the educational coffee seminar.

    Based on Beantown's setup, this will require 3 steps:

    1. Create a list to contain people who sign up for seminars
    2. Create a flow triggered by this list.
    3. Configure a webhook in this flow that creates an event.

    Ready to get started? Open up Postman and your Klaviyo test account, then click the arrow on each dropdown to work through the exercises below. You will also need your private API key handy in order to set up a webhook.

    Create a list

    In Postman, use the Create List endpoint to create a new list titled Seminar attendees.

    The payload for this API request should be:

    text
    {
    	"data": {
    		"type": "list",
    		"attributes": {
    			"name": "Seminar attendees"
    		}
    	}
    }
    Using the create list endpoint in Postman

    After making your API request, you can check the Lists & segments tab to make sure a list titled Seminar attendees was successfully created:

    Seminar attendees list in Klaviyo

    Get the list ID

    The list you just created prior to starting this exercise will contain all customers who signed up for a coffee seminar.

    Your goal is to set up a way to quickly check how many customers have signed up for a seminar.

    To do so, first use the Get Lists endpoint to return all lists in your account.

    __wf_reserved_inherit

    Use these results to determine the list ID of the Seminar attendees list. This is the 6-digit id that is associated with the list name:

    The JSON format of the list ID

    Create a flow

    Send a POST request to the Create Flow endpoint.

    Use this as the body of the API request:

    text
    {
        "data": {
            "type": "flow",
            "attributes": {
                "name": "Create Booked seminar events",
                "definition": {
                    "triggers": [
                        {
                            "type": "list",
                            "id": "RcFaQ5"
                        }
                    ],
                    "profile_filter": {
                        "condition_groups": [
                            {
                                "conditions": []
                            }
                        ]
                    },
                    "actions": [
                        {
                            "type": "send-webhook",
                            "temporary_id": "test"
                        }
                    ],
                    "entry_action_id": "test"
                }
            }
        }
    }

    The only change you will need to make is replacing RcFaQ5 with the ID of the list you found in the previous step.

    If the request went through successfully, you should see a 201 Created status in Postman:

    201 status code in Postman

    In addition, if you navigate to Klaviyo, you should see your new flow titled Create Booked seminar events in the Flows tab. This flow should look like:

    New Create Booked seminar events flow in Klaviyo
    Configure your webhook destination URL

    Beantown's ultimate goal is to send a custom "Booked seminar" event into their Klaviyo account after customers book.

    The webhook automatically fires when the flow is triggered, aka when customers book. Now, your job is to set up the webhook to make a POST request to the Create Event endpoint.

    Let's start by setting up the destination URL. In Klaviyo, click on the webhook in your new Create Booked seminar event flow so that Webhook details pops up on the right. Then, set the Destination URL to https://a.klaviyo.com/api/events.

    Configuring webhook destination URL

    This is the URL that is listed in the Create Event endpoint documentation.

    Configure webhook headers

    Next, add 2 headers to the webhook: Authorization and revision. The value of the revision header is listed at the bottom of the endpoint's documentation:

    Revision header shown in Create Event documentation

    At the time of this writing, the most recent revision is 2025-01-15, so this is what I'll include in the revision header.

    The value of the Authorization header should be Klaviyo-API-Key followed by your private API key. If you did not save your private API key, you can create a new one.

    Configuring webhook headers
    Configure webhook payload

    The last step is to set up the body or payload of the webhook. Copy the payload below into your webhook:

    text
    {
        "data": {
            "type": "event",
            "attributes": {
                "properties": {},
                "metric": {
                    "data": {
                        "type": "metric",
                        "attributes": {
                            "name": "Booked seminar"
                        }
                    }
                },
                "profile": {
                    "data": {
                        "type": "profile",
                        "attributes": {
                            "email": "{{ person.email }}"
                        }
                    }
                }
            }
        }
    }

    This will create an event named Booked seminar. You may notice that the email in the above body is "{{ person.email }}". This will pull the email address of the profile that triggered the flow.

    The entire webhook should look like:

    Webhook configured to create a new event
    Test your webhook

    To test your webhook, add your profile to the Seminar attendees list.

    Then, you can test your webhook by clicking Preview then Send Test Request.

    Sending a test webhook

    After sending a test request, you should see a Booked seminar event on the profile you added to the list.

    Congratulations! You just used Klaviyo's APIs to automatically create a custom event every time a customer is added to a list.

    Webhooks allow you to make an API call to any endpoint within a flow, so that when a flow is triggered, an API call is made.

    Try it: Create a webhook