Skip to main content

Creating coupons via Klaviyo

In this tutorial, let's set up Klaviyo to automatically generate coupons inside Talon.One and add them to customer profiles in Klaviyo.

Let's imagine your website has a newsletter. When a customer subscribes to the newsletter, you want to send them a personal coupon.

To achieve this, we rely on webhooks and API endpoints:

Talon.One requirements

You can edit the default format of the coupon codes in the campaign's Settings section.

Klaviyo requirements

Creating the API keys

To send data back and forth, we need access keys for Talon.One's Integration API and Klaviyo's Profiles API.

Integration API key

To create a generic Integration API key in Talon.One:

  1. In your Application, click Settings > Integration API Keys.

  2. Click Create API Key.

  3. In the Create API Key drawer, select Production as the key type.

  4. In Key name, type a name to identify the key.

  5. In Key expiration date, select a date.

  6. In Third-party integration, select No.

  7. Click Create API Key.

  8. Click to copy the key for use.

    note

    You cannot view or copy the API key after closing the drawer. If you lose it, create another API key.

Profiles API key

To create a Profiles API key in Klaviyo, refer to the Klaviyo docs.

The key must have Read/Write access for the Profiles scope.

Creating and storing the coupon code

In the sections below, we create a workflow that includes the following steps:

  • Adding a customer to a newsletter list in Klaviyo triggers a flow with a webhook action.
  • The webhook action calls Talon.One's Track event endpoint, providing the customer's profile ID.
  • Registering the event in Talon.One triggers a rule that creates a coupon code for the customer.
  • Creating the coupon triggers a custom webhook in Talon.One.
  • The custom webhook calls Klaviyo's Create or Update Profile endpoint, providing the profile ID and the coupon code.
  • The customer's profile is updated with the coupon code.

Klaviyo workflow

Creating the flow

To create a Klaviyo flow with a webhook action that calls Talon.One's Track event endpoint:

  1. In Klaviyo, open Flows and click Create flow.
  2. Click Build your own, type a name for the flow, and click Create flow.
  3. Select the Added to list trigger, choose the list you created, and save the trigger.
  4. On the left-side menu of the flow builder, click the Webhook action and drag it into your flow.
  5. Name the webhook and make the following settings:
    • Destination URL: https://YOUR_DEPLOYMENT_URL/v2/events, where YOUR_DEPLOYMENT_URL is the base URL of your Talon.One deployment, for example, mycompany.europe-west1.talon.one.

    • Headers:

      KeyValue
      AuthorizationApiKey-v1 YOUR_API_KEY, where YOUR_API_KEY is the Integration API key you created.
      Content-Typeapplication/json
      Acceptapplication/json
    • JSON body:

      {
      "type": "klaviyoEvent",
      "profileId": "{{ person.KlaviyoID|default:'' }}",
      "attributes": {
      "flowName": "newsletter"
      }
      }

Now, whenever a customer is added to your list, a request is sent to Talon.One's API.

The request includes the following:

  • The type of the event (klaviyoEvent).
  • A unique ID of the current customer's profile in Klaviyo (person.KlaviyoID). This allows us to personalize the coupon code.
  • A custom attribute named flowName with the static value newsletter. This allows us to uniquely identify the requests from this flow.

To register the event, both the event type and the custom attribute must exist in Talon.One, so let's create them.

Creating the custom attributes

To create the custom attributes required for your flow:

  1. In Talon.One, click Account > Tools > Attributes > Create Attribute.
  2. In Entity, select Event.
  3. In Event type, type klaviyoEvent.
  4. In Type, select String.
  5. In API name, type flowName.
  6. In Name, type Flow name.
  7. Fill the other fields as needed and click Create Attribute.

This creates both the klaviyoEvent event type and the flowName attribute.

Creating the webhook

To create a webhook that calls Klaviyo's Create or Update Profile endpoint:

  1. In the Campaign Manager, click Account > Tools > Webhooks and click Create Webhook.

  2. In Properties, enter a name and select your Application.

  3. Add the parameters to include in the request. In this tutorial, we use the following parameters:

    Param typeParam name
    stringCouponCode
    stringCustomerProfileID
  4. In Request, set Verb to POST.

  5. In URL, enter https://a.klaviyo.com/api/profile-import/.

  6. In Headers, click Add and define the following HTTP headers:

    • Authorization: Klaviyo-API-Key YOUR_KLAVIYO_API_KEY, where YOUR_KLAVIYO_API_KEY is the API key you created.
    • revision: 2024-10-15. For more information about API revisions, see the Klaviyo docs.
    • accept: application/json
    • content-type: application/json
  7. In Payload, add the following request payload:

    {
    "data": {
    "type": "profile",
    "attributes": {
    "properties": {
    "t1_coupon": "${$CouponCode}"
    }
    },
    "id": "${$CustomerProfileID}"
    }
    }
    tip

    To learn more about accessing Talon.One attributes and including them in the webhook, see Using parameters in a webhook.

This webhook calls Klaviyo's API to store the coupon code in a custom profile property named t1_coupon. The property is created automatically when the endpoint is called.

Creating the rule

Now, let's bring it all together by creating a rule that generates the coupon code and stores it in Klaviyo:

WhatNameProperties
ConditionCheck for event types
and custom event values
  • Event type: klaviyoEvent
  • Flow Name (Event) is equal to newsletter.
Effect 1Create coupon code
  • Set your desired coupon settings.
  • Ensure Recipient is set to Current Customer (Friend).
Effect 2Webhooks
  • Choose the name of the webhook you created.
  • Set CouponCode to Generated Coupon in Session.
  • Set CustomerProfileID to Integration ID (Customer Profile).

When the matching event is registered, the rule is triggered, a coupon code is generated, and the custom webhook is triggered, which stores the coupon code in Klaviyo.

Next steps

Now that all of your newsletter subscribers have a personal coupon code, you can send them the code, for example, by creating an email campaign in Klaviyo.

To reference the coupon code in the email, use the following personalization tag: {{ person|lookup:"t1_coupon" }}.