# Send Transactional Messages Through On-Demand Flow

Use API-triggered transactional messaging to send prioritized, personalized messages, such as one-time passwords (OTPs) or order confirmations, from Marketing Cloud Next. By using REST API to invoke an On-Demand Flow, you can securely pass dynamic data at send time and deliver time-sensitive service communications exactly when they’re needed.

## Prerequisites

Before you begin, make sure that you have:

- Permissions to create Apex Classes, CMS Email content, Flows, and external client apps
- Ability to authenticate and invoke REST APIs
- A data source for personalization, such as an Apex class or a data graph from Data 360

:::note
Using a data graph as the personalization data source can increase the latency of your requests.
:::

## Step 1: Set Up Data Sources for Personalization

To pass real-time personalization values into the email at send time, define a data source that the Flow and email can reference.

### Create an Apex Class

In this example, an Apex class defines the input structure for personalization fields, such as first name and OTP code.

1. In **Setup**, enter **Apex Classes** in Quick Find.
2. Click **New**.
3. Create an Apex Class that defines the required input variables required for the transactional message.
4. Save the class. The Apex Class exposes variables that are later mapped in the Flow and populated through the REST API request.

#### Example Apex Class:

```apex
public with sharing class FlowContactInfoAura {
    @AuraEnabled public String firstName { get; set; }
    @AuraEnabled public String otpCode { get; set; }
    @AuraEnabled public String ContactPointEmail { get; set; }
    @AuraEnabled public String IndividualId { get; set; }
}
```

## Step 2: Create Transactional Email Content

Create transactional email content as CMS content and configure it with the correct data source.

1. Create the Email:
   1. Go to **Marketing App > Content**.
   2. Click **Add CMS Content** and select **Email**.
   3. Open the email in **Email Builder**.
2. Connect the data source:
   1. In the Email Builder, add a new data source and select **Apex Data Provider**.
   2. Choose the Apex Class that you created.
3. Design the email body:
   1. Insert merge fields (for example, FirstName or otpCode) from the Apex Data Provider.
   2. Set **Message Purpose** to **Transactional**.
   3. Save and publish the email.

CMS channels represent publishing endpoints such as email. Publish transactional emails before you use them in a Flow.

## Step 3: Configure the On-Demand Flow

The REST API calls the On-Demand Flow, which defines the send logic.

1. Create the Flow:
   1. Go to the **Flow** tab and click **New**.
   2. Select **On-Demand Flow** (launched by REST API).
2. Add the Apex resource:
   1. In the Toolbox, click New **Resource**.
   2. Set **Resource Type** to **Variable**.
   3. Set **Data Type** to **Apex-Defined**.
   4. Select your personalization class as the Apex Class.
   5. Turn on **Available for Input**. Save the API Name of this resource. You need this value when you construct the REST API request.
3. Add **Send Email** as the first Flow element.
4. Select the published transactional email.
5. Configure the sender and activate the Flow.
6. Save and activate the Flow.

## Step 4: Authenticate and Trigger the Flow with REST API

To invoke the Flow, you must authenticate using OAuth and then call the Flow action endpoint.

### Create an External Client App

1. In Setup, search for `client app`, and then select **External Client App Manager**.
2. Click **New External Client App**.
3. Enter a name for the app and a contact email address.
4. Enable **OAuth Settings**.
5. Enter a Callback URL.
6. Add these OAuth scopes:
   - Full access (full)
   - Manage user data via APIs (api)
7. Save your changes.

After you save the app, open **Manage Consumer Details** and record the **Consumer Key** and **Consumer Secret**.

### Endpoint

```http
POST /services/data/v65.0/actions/custom/flow/FLOW_API_NAME
```

### Request Example

```json
{
  "inputs": [
    {
      "EmailAddress": "jen.doe@example.com",
      "IndividualID": "4001CD4A",
      "OTP": {
        "FirstName": "Jen",
        "otpCode": "4567"
      }
    }
  ]
}
```

### Response Example

```json
[
  {
    "actionName": "FLOW_API_NAME",
    "errors": null,
    "isSuccess": true
  }
]
```
