Rendering Email in Browser with REST API in Salesforce Marketing Cloud

Author: Florin Valean

Use case

A common scenario where rendering an email in the browser can prove useful is when you want people in your business to access the email after the email was sent to a specific subscriber. A good example would be the customer service agent who may need to view emails sent to contacts.
This is the API route that can be used to render the content of an email based on data stored in a data extension:

/* replace YOUR_SUBDOMAIN with your subdomain generated when you create the Installed Package in Marketing Cloud */ /* replace YOUR_EMAIL_ID with your email ID - available with _emailid personalization string */ /* replace YOUR_DE_CUSTOMER_KEY with your data extension customer key */ /* replace YOUR_SUBSCRIBER_KEY with your subscriber key */


For this use case we are going to use an Installed Package created with enhanced functionality with a component of type API Integration and integration of type Server-to-Server. Make sure you assign all the scopes you need to the Installed Package.

We also need to create a sendable data extension. Let's call it Render Email with customer key RenderEmail. This test data extension will have three attributes:

Attribute nameAttribute data type
SubscriberKeyText (50)
FirstNameText (30)

SubscriberKey in this data extension relates to Subscribers on Subscriber Key.

We are going to import a record in this data extension:


And obviously en email is necessary. Let's assume we have an email created in Content Builder that displays Hello %%FirstName%%! For this exercise the email id is 123. When this email is sent to the sendable data extension that was created earlier, Marketing Cloud will send an email to and the email will display Hello Anna!

OAuth 2.0 access token is required. For more details on how to obtain and how to use the access token go to this page.

Server-side javascript sample code

You can create a landing page in Web Studio and include this code. Publish the page and load it in a web browser to test the code.

/* replace the highlighted values with yours */ <script runat="server"> Platform.Load("Core", "1"); try { /* get OAuth 2.0 access token */ var payload = '{"grant_type":"client_credentials",'; payload += '"client_id":"d20aaul96htb2hgo9b4i47gx",'; payload += '"client_secret":"tUriuDS0zdV82vFrLIbJek77",'; payload += '"scope":null,'; payload += '"account_id":"012345678"}'; var OAuth2URL = ""; var contentType = 'application/json'; var accessTokenResult = HTTP.Post(OAuth2URL, contentType, payload); var tokenObj = Platform.Function.ParseJSON(accessTokenResult["Response"][0]); var accessToken = tokenObj.access_token; var headerNames = ["Authorization"]; var headerValues = ["Bearer " + accessToken]; var deCustomerKey = "RenderEmail"; var subKey = "Sub-1"; var emailID = "123"; /* make the API call and get the HTML code of the rendered email */ var content = [0]; var urlPost = "" + emailID + "/dataExtension/key:" + deCustomerKey + "/contacts/key:" + subKey + "/preview?kind=html"; var response = HTTP.Post(urlPost, contentType, '', headerNames, headerValues, content); if(response) { var content = eval("[" + response.Response + "]")[0]; htmlContent = content.message.views[0].content; htmlContent = htmlContent.replace(/[\n\r\t]/g, ''); Write(htmlContent); } } catch(e) { Write(Stringify(e)); } </script>

Notice the use of try-catch block - see this page for more details on how/when to use try-catch.

As a result the cloud page will display the rendered email with the message Hello Anna!

Share this page
Stay in touch

Subscribe to the newsletter

p1 p2 p3