# Create custom coupons

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

A number of use cases may require creating custom coupons using the [Management API][MAPI start]. Custom coupons are coupons with custom attributes that we can leverage to tweak the coupons' behaviour.

## Overview

Let's consider a campaign that rewards individual customers with a _personal_ coupon code.
To do this, our campaign rule needs to contain a _coupon belongs to the current user_
logic.

This logic relies on adding an attribute to each coupon that carry the
information about which profile it belongs to.
In short, we must match a coupon to a profile ID.

:::note Note about the profile ID
The profile ID originally comes from the integration layer's requests
to the
[UpdateCustomerProfile](/integration-api#tag/Customer-profiles/operation/updateCustomerProfileV2) endpoint.

In Talon.One, you can find its value in the customer profile entity's `IntegrationId`
or in the customer session entity's `profileId`, if the session is linked to the customer profile.

For more information, see the [integration tutorial](/docs/dev/tutorials/integrate-talon-one.md).
:::

## Create the custom attribute programmatically

Let's start by creating the custom attribute for our coupons. We can do this using
the Campaign Manager's UI or programmatically using the [Management API][MAPI start].
Let's do it via the API.

We can name the attribute `BelongsToProfileId` and bind it to the `coupons` entity. To
create it, let's use the [Create attribute][Create attribute] endpoint with the following
curl example:

```json {5-6}
curl --location --request POST 'https://[YOUR_DEPLOYMENT].talon.one/v1/attributes' \
--header 'Authorization: Bearer [BEARER_TOKEN]' \
--header 'Content-Type: application/json' \
--data-raw '{
    "entity": "Coupon",
    "name": "BelongsToProfileId",
    "title": "BelongsToProfileId",
    "type": "string",
    "description": "The value of the profile ID that owns this coupon.",
    "suggestions": [],
    "editable": true
}
```

:::note
`BEARER_TOKEN` comes from the
[Create session](/management-api#tag/Sessions/operation/createSession) endpoint.
:::

The custom attribute is created, we can use it in our coupons.

## Create the coupons programmatically

To create the coupons programmatically, we use the
[Create coupons][create coupons] endpoint. We can add the `attributes` object to
the payload to set custom attributes.

In our case, we can dynamically assign the `profile ID` to the `BelongsToProfileId`
attribute.

One request looks like this:

```json {8-10}
curl --location --request POST 'https://[YOUR_DEPLOYMENT].talon.one/v1/applications/[APPLICATION_ID]/campaigns/[CAMPAIGN_ID]/coupons' \
--header 'Authorization: Bearer [BEARER_TOKEN]' \
--header 'Content-Type: application/json' \
--data-raw '{
    "usageLimit": 1,
    "couponPattern": "###-###-###",
    "numberOfCoupons": 1,
    "attributes":{
        "BelongsToProfileId": "<myProfileId>"
    }
}
```

:::note
Notice the `usageLimit` property set to `1` and the `attributes` object. `<myProfileId>`
is owned by the integration layer, so we assume that the code around this call would
set the value.
:::

This is how we can set custom attributes on coupons programmatically. We can also
use one of the available [SDKs][SDK].

## Create the rule

Talon.One now has all the data required for our workflow, we can use this data in a rule.

The identifier of the customer is represented by the `Session.ProfileId`
and the `Profile.IntegrationId`. These are identical.

Our rule is:

```text
- Integration ID (Customer profile) is equal to BelongsToProfileId (Coupon)
- Coupon code is valid
```

- `Integration ID (Customer profile)` is a built-in attribute set by the integration layer.
- `BelongsToProfileId` is the custom attribute we created.

## Related pages

- [Import coupons](/docs/dev/tutorials/import-coupon-codes.md)

[MAPI start]: /docs/dev/management-api/overview.md
[Create attribute]: /management-api#tag/Attributes/operation/createAttribute
[Create coupons]: /management-api#tag/Coupons/operation/createCoupons
[SDK]: /docs/dev/sdks/overview.md
