create a new token

Sending Transactional Emails with the Marketo REST API: Part 2, Custom Content

Kenny_Elkington
Marketo Employee
Marketo Employee

This post originally appears here.

Last time, we took a look at triggering transactional emails from outside of Marketo.  This week we'll be looking at how to pass dynamic content to our emails via the Request Campaign API call.  Request Campaign not only allows the triggering of emails externally, but you can  also replace the content of My Tokens within an email.  My tokens are reusable content that can be customized at the program or marketing folder level.  These can also just exist as placeholders to be replaced through your request campaign call.  For instructions on configuring the smart campaign, see part one.

Building your Email

In order to customize our content, first we will need to configure a program and an email in Marketo.  To generate our custom content, we will need to create tokens inside of the program, and then place them into the email that we're going to be sending.  For simplicity's sake, we'll be using just one token in this example, but you can replace any number of tokens in an email, in the From Email, From Name, Reply-to or any piece of content in the email.  So let's create one token Rich Text for replacement and call it "bodyReplacement".  Rich Text allows us to replace any content in the token with arbitrary HTML that we want to input. create a new token

Tokens can't be saved while empty, so go ahead and insert some placeholder text here.  Now we need to insert our token into the email:

add token to email

This token will now be accessible for replacement through a Request Campaign call.  This token can be as simple as a single line of text which needs to be replaced on a per-email basis, or can include almost the entire layout of the email.

The Code

In previous posts, we've looked at authenticating and retrieving lead records, and then sending an email to those leads.  We'll be expanding on the code from last week to pass in customized tokens to our request campaign call.

package dev.marketo.blog_request_campaign;  import com.eclipsesource.json.JsonArray;  public class App { public static void main( String[] args ) { //Create an instance of Auth so that we can authenticate with our Marketo instance Auth auth = new Auth("Client ID - CHANGE ME", "Client Secret - CHANGE ME", "Host - CHANGE ME");  //Create and parameterize an instance of Leads Leads leadsRequest = new Leads(auth).setFilterType("email").addFilterValue("requestCampaign.test@marketo.com");  //get the inner results array of the response JsonArray leadsResult = leadsRequest.getData().get("result").asArray();  //get the id of the record indexed at 0 int lead = leadsResult.get(0).asObject().get("id").asInt();  //Set the ID of our campaign from Marketo int campaignId = 1578; RequestCampaign rc = new RequestCampaign(auth, campaignId).addLead(lead);  //Create the content of the token here, and add it to the request String bodyReplacement = "<div class=\"replacedContent\"><p>This content has been replaced</p></div>"; rc.addToken("{{my.bodyReplacement}}", bodyReplacement); rc.postData(); } } 

If the code looks familiar, that's because it only has two additional lines from the main method in the previous post.  This time we're creating the content of our token in the bodyReplacement variable and then using the addToken method to add it to the request.  addToken takes a key and a value and then creates a JsonObject representation and adds it to the internal tokens array.  This is then serialized during the postData method and creates a body that looks like this:

{"input":{"leads":[{"id":1}],"tokens":[{"name":"{{my.bodyReplacement}}","value":"<div class=\"replacedContent\"><p>This content has been replaced</p></div>"}]}}

Combined, our console output looks like this:

Token is empty or expired. Trying new authentication Trying to authenticate with ... Got Authentication Response: {"access_token":"19d51b9a-ff60-4222-bbd5-be8b206f1d40:st","token_type":"bearer","expires_in":3565,"scope":"apiuser@mktosupport.com"} Executing RequestCampaign call Endpoint: .../rest/v1/campaigns/1578/trigger.json?access_token=19d51b9a-ff60-4222-bbd5-be8b206f1d40:st Request Body: {"input":{"leads":[{"id":1}],"tokens":[{"name":"{{my.bodyReplacement}}","value":"&lt;div class=\"replacedContent\"&gt;&lt;p&gt;This content has been replaced&lt;/p&gt;&lt;/div&gt;"}]}} Result: {"requestId":"1e8d#14eadc5143d","result":[{"id":1578}],"success":true} 

Wrapping Up

This method is extensible in a multitude of ways, changing content in emails within individual layout sections, or outside emails, allowing custom values to be passed into tasks or interesting moments.  Anywhere a my token can be used from within a program can be customized using this method.  Similar functionality is also available with the Schedule Campaign call which will allow you to process tokens across an entire batch campaign.  These can't be customized on a per lead basis, but are very useful for customizing content across a wide set of leads.

480
0