I have a few emails in Email Studio (I don't have any classic emails). I would like to use the API (either SOAP or REST whichever works) to get the email template. Then after that, I will make another call to SFMC to get the data of a subscriber which is saved in a data extension and use this data to fill up the info in the email template. I know how to retrieve data from a data extension using the API but I don't know how to get the email template using the API. Thanks!
1 Answer
Here's an example of something I've done before to retrieve the HTML of an email. I then combined the email code with data from a DE to create my own "preview" in a CloudPage. Adapt for your own use.
<script runat="server">
Platform.Load("core","1");
// Example of HTTP GET/POST to SFMC REST API to retrieve HTML of an email
try {
var authBaseUrl = 'https://YOURENDPOINT.auth.marketingcloudapis.com';
var authEndPoint = '/v1/requestToken';
var restBaseUrl = 'https://YOURENDPOINT.rest.marketingcloudapis.com';
var emailID = '12345'
var assetEndPoint = '/asset/v1/content/assets/' + emailID + '/channelviews/html';
var authObj = {};
authObj.clientId = 'YOURCLIENTID';
authObj.clientSecret = 'YOURCLIENTSECRET';
var contentType = 'application/json';
var authReq = HTTP.Post(authBaseUrl + authEndPoint, contentType, Stringify(authObj));
if (authReq.StatusCode == 200) {
var tokenResponse = Platform.Function.ParseJSON(authReq.Response[0]);
var accessToken = tokenResponse.accessToken;
if (accessToken) {
var assetReq = HTTP.Get(restBaseUrl+assetEndPoint, ['Authorization'],['Bearer '+ accessToken]);
// zero means success
if (assetReq.Status == 0) {
var assetContent = Platform.Function.ParseJSON(assetReq.Content);
// html of email
var emailHtml = assetContent.compiled;
}
}
} else {
// error handling
}
} catch (ex) {
Write("An error has occurred: " + Stringify(ex));
}
</script>
-
Hey Josh de Blank, thanks for your response. It was the answer I was looking for. Just one small comment. We need to use assetID instead of emailID.– tkhuynhAug 7, 2019 at 22:29