Send Transactional Messages Through On-Demand Flow
API-triggered transactional messaging lets you send prioritized, personalized messages—such as one-time passwords (OTPs) or order confirmations from Marketing Cloud Next in real time. By invoking an On-Demand Flow through the REST API, you can securely pass dynamic data at send time and deliver time-sensitive service communications exactly when they’re needed.
Before you begin, make sure you have:
- Permissions to create Apex Classes, CMS Email content, Flows, and Connected Apps
- Ability to authenticate and invoke REST APIs
- A data source for personalization, such as an Apex class or Data Cloud data graphs
Note: Using Data Cloud data graphs may introduce additional latency.
To pass real-time personalization values into the email at send time, define a data source that the Flow and email can reference.
In this example, an Apex class defines the input structure for personalization fields such as first name and OTP code.
- In Setup, enter Apex Classes in Quick Find.
- Click New.
- Create an Apex Class that defines the required input variables required for the transactional message.
- Save the class. The Apex Class exposes variables that are later mapped in the Flow and populated through the REST API request.
Create transactional email content as CMS content and configure it with the correct data source.
- Create the Email:
- Go to Marketing App > Content.
- Click Add CMS Content and select Email.
- Open the email in Email Builder.
- Connect the data source:
- In the Email Builder, add a new data source and select Apex Data Provider.
- Choose the Apex Class you created.
- Design the email body:
- Insert merge fields (for example, FirstName or otpCode) from the Apex Data Provider.
- Set Message Purpose to Transactional.
- Save and publish the email.
CMS channels represent publishing endpoints such as email, and you must publish transactional emails before using them in a Flow.
The REST API calls the On-Demand Flow, which defines the send logic.
- Create the Flow:
- Go to the Flow tab and click New.
- Select On-Demand Flow (launched by REST API).
- Add the Apex resource:
- In the Toolbox, click New Resource.
- Set Resource Type to Variable.
- Set Data Type to Apex-Defined.
- Select your personalization class as the Apex Class.
- Enable Available for Input. Save the API Name of this resource. You’ll need it when constructing the REST API request.
- Add Send Email as the first Flow element.
- Select the published transactional email.
- Configure the sender and activate the Flow.
- Save and activate the Flow.
To invoke the Flow, you must authenticate using OAuth and then call the Flow action endpoint.
- In Setup, search for External Client App.
- Open App Manager and click New App.
- Enter basic information (Name, API Name, Contact Email).
- Enable OAuth Settings.
- Enter a Callback URL.
- Add these OAuth scopes:
- Full access (full)
- Manage user data via APIs (api)
- Save the app.
After saving, open Manage Consumer Details and record the Consumer Key and Consumer Secret.