# Send coupons via CleverTap

> In this tutorial, let's set up Talon.One to automatically generate coupons and send them via CleverTap.

> For the complete documentation index, see [llms.txt](https://docs.talon.one/llms.txt).

Let's imagine you want to email a coupon code to every customer who spends $200 or more on their order.

To do so, we rely on Talon.One's [webhook][webhook] functionality and CleverTap's
[Upload Events](https://developer.clevertap.com/docs/upload-events-api) endpoint.

## Talon.One requirements

- You are an [admin](/docs/product/account/account-settings/manage-roles.md#admin-role) user.
- You have set up [an Application][App].
- Your Application contains [a running campaign][campaigns] with the **Coupons** feature enabled.

## CleverTap requirements

- You are an [admin](https://docs.clevertap.com/docs/role-based-access-control) user.
- You have configured an [email provider](https://docs.clevertap.com/docs/setup-email) and tested it.

## Workflow overview

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

- Closing a customer session with a cart item value of $200 or more creates a coupon code
  and triggers a [custom webhook](#create-the-webhook-in-talonone) in Talon.One.
- The webhook calls CleverTap's [Upload Events](https://developer.clevertap.com/docs/upload-events-api)
  endpoint, providing the coupon code and a customer profile ID.
- Registering the event triggers a [campaign](#create-the-campaign-in-clevertap) in CleverTap.
- The campaign sends an email with the provided coupon code to the customer with the provided profile ID.

<ClevertapWorkflow className="figma" width="100%" alt="CleverTap workflow" />

## Set up the integration

<StepsContainer>

### Create the webhook in Talon.One

To create a webhook that calls CleverTap's [Upload Events](https://developer.clevertap.com/docs/upload-events-api):

1. In the Campaign Manager, click <Account className="icon"/> **Account** > <Tools className="icon"/>
   **Tools** > **Webhooks** and click **Create Webhook**.
1. In **Properties**, enter a name and select your Application.
1. Add the parameters to include in the request. In this tutorial, we'll use the following parameters:

   | **Param type** | **Param name**      |
   | -------------- | ------------------- |
   | `string`       | `CouponCode`        |
   | `string`       | `CustomerProfileID` |

1. In **Request**, set **Verb** to **POST**.
1. In **URL**, enter `https://<YOUR_REGION>.api.clevertap.com/1/upload`, where `YOUR_REGION` is your [region key](https://developer.clevertap.com/docs/common-api-components#region), for example, `us1`.
1. In **Headers**, click **Add** and define the following headers:
   - `X-CleverTap-Account-Id: <YOUR_PROJECT_ID>`, where `YOUR_PROJECT_ID` is your [Project ID](https://developer.clevertap.com/docs/api-quickstart-guide#get-clevertap-account-credentials-to-authenticate-api-requests) in CleverTap.
   - `X-CleverTap-Passcode: <YOUR_PROJECT_PASSCODE>`, where `YOUR_PROJECT_PASSCODE` is your [Passcode](https://developer.clevertap.com/docs/api-quickstart-guide#get-clevertap-account-credentials-to-authenticate-api-requests) in CleverTap.
   - `Content-Type: application/json; charset=utf-8`
1. In **Payload**, add the following request payload:

   ```json
   {
     "d": [
       {
         "type": "event",
         "evtName": "Coupon Generated",
         "$source": "Talon.One",
         "identity": "${$CustomerProfileID}",
         "evtData": {
           "CouponCode": "${$CouponCode}"
         }
       }
     ]
   }
   ```

   :::note
   To learn more about accessing Talon.One attributes and including them in the webhook,
   see [Use parameters in the webhook payload](/docs/product/account/dev-tools/manage-webhooks.md#use-parameters-in-the-webhook-payload).
   :::
1. In **Preview**, click **Copy**.
1. Click **Create Webhook**.
1. Paste and run the copied `curl` command in your terminal.
   This calls the endpoint with placeholder data, which creates a [custom event](https://docs.clevertap.com/docs/events#custom-events)
   named `Coupon Generated` in CleverTap. The event is required to [create the campaign](#create-the-campaign-in-clevertap).

### Create the campaign in CleverTap

To create an email campaign that can be triggered by our webhook:

1. In CleverTap, in the sidebar, open **Campaigns**.
1. Click **+ Campaign > Email**.
1. In **Name**, type a name for your campaign.
1. In **Start here** > **Qualification criteria**, select **Live behavior** and click **Done**.
1. In **Email Service Provider**, select the email provider you [configured](#clevertap-requirements)
   and click **Done**.
1. In **Set a goal**, click **Done**.
1. In **Who > Target Segment > As soon as user does**, select the name of the event you
   created (**Coupon Generated**, in our case) and click **Done**.
1. In **Control group and target segment cap**, click **Done**.
1. In **What**, select **Single Message** and click **Go To Editor**.
1. Select a template and personalize your message in the message editor.
1. In the email body, type **@** and select the desired custom event property
   (**@Coupon Generated - CouponCode**, in our case). Replace `[default value]` with a default
   value for the coupon code, for example, `[DISCOUNT10]`.
1. In **Sender Details**, fill in the sender information as required and click **Done**.
1. In **When** > **Date and time**, click **Done**.
1. In **Delivery preferences**, click **Done**.
1. Click **Publish Campaign**.

### Create the rule

Now, let's bring it all together by creating a [rule](/docs/product/rules/create-and-manage-rules.md)
in Talon.One that generates the coupon code and triggers the webhook:

| What            | Name                                               | Properties                                                                                                                                                                                                                                                                                          |
| --------------- | -------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Condition 1** | Check attribute value                              | <Attribute name="Session Total (Current Session)" type="builtin" /> **is greater than or equal to** `200`.                                                                                                                                                                                          |
| **Condition 2** | Check for event typesand custom event values | **Event type** is `Customer Session Closing`.                                                                                                                                                                                                                                                       |
| **Effect 1**    | Create coupon code                                 | Set your desired coupon settings.Ensure **Recipient** is set to **Current Customer (Friend)**.                                                                                                                                                                           |
| **Effect 2**    | Webhooks                                           | Choose the name of the webhook you [created](#create-the-webhook-in-talonone).Set **CouponCode** to <Attribute name="Generated Coupon in Session" type="builtin" />.Set **ProfileID** to <Attribute name="Integration ID (Customer Profile)" type="builtin" />. |

</StepsContainer>

## Test the setup

Let's test our setup by simulating a purchase with a high cart item value.

1. Send the following payload to the [Update customer session](/integration-api#tag/Customer-sessions/operation/updateCustomerSessionV2)
   endpoint, where `YOUR_PROFILE_ID` refers to a customer identity in CleverTap. For testing,
   provide an email address to send the email to, for example, `john@doe.com`.

   ```json {4}
   {
     "customerSession": {
       "profileId": "YOUR_PROFILE_ID",
       "state": "open",
       "cartItems": [
         {
           "name": "Fine woven scarf",
           "sku": "SCARF123",
           "quantity": 1,
           "price": 249.99
         }
       ]
     }
   }
   ```

1. Send the payload again with the `state` property set to `closed`.

The session is now closed, with a cart item value greater than $200. This triggers our [rule](#create-the-rule).

The rule calls CleverTap's API endpoint using the webhook, which in turn activates the campaign
and sends an email to the provided profile ID.

[App]: /docs/product/applications/overview.md
[campaigns]: /docs/product/campaigns/overview.md
[webhook]: /docs/product/account/dev-tools/manage-webhooks.md
