In this session the team starts building the SSJS coding in the Marketing Cloud page to make the API call back into the Wait Until Event in Journey builder.

How to write SSJS code to make an API call from a cloud page

PART I: Setting all variables in Ampscript
To begin set up the Ampscript variables that include the credentials that are now encrypted thanks to the keys step.

(1) Open your script line using this syntax: <script runat=”server” language=”ampscript”>
(2) Set variables for the Auth and Rest URI
(3) Set your three key variables and include they external key names
(4) Set the Event Definition Key from the Wait Until Event activity in the journey
(5) Set the encrypted Client ID and Secret from the keys exercise in the last segment: https://handsonsfmc.com/2023/04/15/journey-builder-custom-activity-wait-until-event-key-managment/
(6) Set the decrypted variables from the keys exercise in the last segment
(7) close the script section with </script>

PART II: Setting all variables in Ampscript
Next up start coding in SSJS!


(1) Open up the script with the following syntax: <script runat=”server”> and include the platform load as detailed above to bring in the corresponding java script library.
(2) Set variables for the Auth and Rest URIs
(3) Set the variable for the Event Definition Key for the Wait Until Event activity
(4) Set the variables for the Client ID, Secret and subscriberkey which was declared above above on line 9 set to the _subscriberkey personalization string.

Tip #1 Use those amazing Personalization Strings!
Marketing Cloud has this treasure trove of variables that are available to use in emails and cloud pages once the email has been sent. Check out the full list here: https://help.salesforce.com/s/articleView?id=sf.mc_es_available_personalization_strings.htm&type=5

Taking a look at the link above you’ll find on the extensive list _subscriberkey which is that handy unique identifier for the subscriber. Integrations with Sales Cloud this will often be the Contact or Lead ID.

PART III: Auth call to gain access and get a token
Time to make that call to the Marketing Cloud installed package to gain access to the instance and generate a token.


(1) Set the variables to faciliate the Authorization call.
– authURL – customize the auth URL by appending the ‘v2/token’
– restEventURL – customize the rest URL which will be used in the REST call in the next post
– authContentType – standard functionality required to make the call
– token – establishes/resets the token
(2) Set the payload which are the values that need to be passed in order to make the call
– client_id – this is why you need the client id
– client_secret – this is why you need the client secret
– grant_type – standard requirement for the auth payload
(3) This is the call!: var req1=HTTP.Post… is the syntax to make the auth call
(4) var res1 will capture the response back from the API call and var token will capture the token which will be required in the rest call.

Don’t miss part II which will be coming in a couple of days!

Leave a comment