Skip to main content

    Get data out of Klaviyo with APIs

    Course overview
    Lesson
    5 min read

    Try it! Use flow webhooks

    In this set of exercises, you will write and test a webhook in a flow.

    Write flow webhooks

    To complete your setup, you’ll want to use a webhook to automatically add customers to the proper list after they book a tour.

    Note that you do not need to use Napkin when writing your flow webhook, as those are authored directly in the flow builder within Klaviyo. However, Napkin or Postman can be useful tools for testing your webhook format.

    Automate list membership using webhooks

    Use a flow webhook to create a flow that automatically adds customers to the Hiking tours list after they book a hiking tour.

    Follow these steps to set up this flow webhook:

    1. Create a flow triggered by the custom Booked tour event you previously created.
    2. Apply the trigger filter "type of tour equals hiking."
    3. Add a webhook action into the flow.
    4. Set the webhook's destination URL to be that of the Add Profile to List endpoint. Note that this endpoint does not subscribe profiles to marketing; it just adds them to the list.
    5. Complete the webhook headers and body according to the documentation.

    If you need help getting started with creating this webhook, check out the steps listed under Hints.

    To test that you have correctly completed this exercise, click the Preview button in the webhook settings, then click Send Test Request. If the webhook was successful, you should see your test profile in the Hiking tours list:

    Profile in Hiking tours list in Klaviyo
    Hints

    Use these hints to help if you get stuck:

    • You will need to find and use the 6-digit ID of the Hiking tours list.
    • You can access a profile ID in a webhook using {{ person.KlaviyoID }}.
    • Add 2 headers to your webhook: Authorization and revision.
    • If the webhook is not firing, make an API call with the same headers, request URL, and body in Postman, Napkin, or any other software to troubleshoot any errors.

    Once the API call is working in Postman, copy the revision and authorization headers into the Headers section in the webhook builder. Copy the destination URL and request body from Postman to their appropriate spaces in the webhook builder.

    Add tour dates to customer profiles

    In the same flow, add a webhook that updates customer profiles with a custom profile property, hiking tour start date, which contains the start date of the hiking tour they just booked.

    Follow these steps to build this webhook:

    1. In the same flow triggered by Booked tour, add another webhook after the one you just created.
    2. Set your new webhook's destination URL to the Create or Update Profile endpoint.
    3. Complete the webhook headers and body according to the documentation.

    To test that you have correctly completed this exercise, click the Preview button in the webhook settings, then click Send Test Request. If your webhook fires correctly, you should see the hiking tour start date property added to customer profiles with the date of the tour they booked:

    Hints

    • Add the custom profile property within properties in your webhook body.
    • You can access a profile ID in a webhook using {{ person.KlaviyoID }}.
    • You can access the Start date event property using {{ event|lookup:"Start date" }}. Make sure this exactly matches the case of your event data.
    • Add 2 headers to your webhook: Authorization and revision.
    • If the webhook is not firing, make an API call with the same headers, request URL, and body in Postman, Napkin, or any other software and use this to troubleshoot any errors.

    Once the API call is working in Postman, copy the revision and authorization headers into the Headers section in the webhook builder. Copy the destination URL and request body from Postman to their appropriate spaces in the webhook builder.

    Stuck? Check out these resources

    Check out our guide on how to add a webhook action to a flow for detailed instructions on formatting a flow webhook.

    If you need additional assistance, post your question in our Developer Group on Klaviyo Community.

    Solutions
    Automate list membership using webhooks

    The destination URL of your webhook should be https://a.klaviyo.com/api/lists/LISTID/relationships/profiles/ (LISTID is the ID of the Hiking tours list). You can find this ID either using APIs, like you did in the first set of exercises, or by navigating to the list settings:

    List settings showing list ID

    There should be 2 headers on the webhook: Authorization and revision. The value for Authorization should be Klaviyo-API-Key pk_xxx (pk_xxx is your private API key). The value for revision should be the most recent revision, which can be found in the API documentation.

    The body of the webhook should be:

    text
    {
    	"data": [
    		{
    			"type": "profile",
    			"id": "{{ person.KlaviyoID }}"
    		}
    	]
    }

    Putting this all together, the webhook will look like:

    Flow webhook adding profiles to Hiking tours list
    Add tour dates to customer profiles

    The destination URL of your webhook should be https://a.klaviyo.com/api/profile-import/.

    The webhook should have 2 headers: Authorization and revision. The value for Authorization should be Klaviyo-API-Key pk_xxx (pk_xxx is your private API key). The value for revision should be the most recent revision, which can be found in the API documentation.

    The body of the webhook should be:

    text
    {
      "data": {
        "type": "profile",
        "attributes": {
          "properties": {
            "hiking tour start date": "{{ event|lookup:'Start date' }}"
          }
        },
        "id": "{{ person.KlaviyoID }}"
      }
    }

    The entire flow should look like:

    Flow containing both webhooks
    Try it! Use flow webhooks