Used AMPScript and SSJS to generate an encrypted JSON message and pass this encryped JSON (payload) to the external system using a URL. However, the last line in the below code is not picking the URL. Cannot understand what's going wrong here -

%%[ const crypto, var @data, @contentType, @payload, @encryptkeybase64, @authkeybase64,  @domain,  @response, @req, @resp, @encryptionKey, @authenticationKey, @cipher, @cipherText, @hmac, @base64Payload, @url, @iv, @ssconsole]%%

<script runat="server">


var contentType = 'application/x-www-form-urlencoded';
var encryptkeybase64  = '3kXLV1rnimLRdsdfsdfsdf2Wdd+/hYVTB/Dt6nnIk=';
var authkeybase64 = 'BKulGe3CTrweSHkrwerwerwerdNpFAeuuWhfevc4A=';
var domain = 'hughes.com';
const crypto = require('crypto');

var order   = {
                name : "Nishanth M",
                email : "[email protected]",
                ref : "12345"

 var jsonSerializedOrder  = JSON.stringify(order);

// When you get the keys from Trustpilot, they are base64 encoded, so first we need to decode them
var encryptionKey = Buffer.from(encryptionKeyBase64, 'base64');
var authenticationKey = Buffer.from(authenticationKeyBase64, 'base64');

  // Generate a random initialization vector
var iv = crypto.randomBytes(16);

  // Encrypt our order
var cipher = crypto.createCipheriv('aes-256-cbc', encryptionKey, iv);
var cipherText = Buffer.concat([cipher.update(jsonSerializedOrder, 'utf8'), cipher.final()]);

// Compute the HMAC
var hmac = crypto.createHmac('sha256', authenticationKey).update(Buffer.concat([iv, cipherText])).digest();

// Base64 encode the IV + cipherText + HMAC
var base64Payload = Buffer.concat([iv, cipherText, hmac]).toString("base64");

// URL encode to get the final payload
var payload = encodeURIComponent(base64Payload);
var url = 'https://www.trustpilot.com/evaluate-bgl/' + domain + '?p=' + payload;  
  console.log("Something happened");

  try {
  var req = HTTP.Post(url, contentType, payload);


 if(req.StatusCode == 200) {
 var resp = Platform.Function.ParseJSON(String(req.Response[0]));
  var accessToken = tokenResponse.accessToken;
 } catch (ex) {
          Write("Exception Error: " + Stringify(ex));


<a href="%%=RedirectTo(@url)=%%">Test</a>

2 Answers 2


I hope the issue is NOT with SSJS or AMPscript! Please find the minified version of your code, where in I tested and it worked.

<script runat="server">

var url = 'https://www.trustpilot.com/evaluate-bgl/' + 'testDOMAIN' + '?p=' + 'testPayload';  
  Variable.SetValue("@OutputToConsole",url); //you cannot directly output to console, as this will output to server console NOT browser console

<script runat="client">
console.log(`%%=v(@OutputToConsole)=%%`); //Here this outputs to Browser console

<a href="%%=RedirectTo(@url)=%%">Test</a>

Note: I added the technique to log to browser console from SSJS, use this technique to debug your code.

  • Appreciate for taking time to look into this and thank you so much Vishal .. this worked !! As you rightly pointed out, this was always hitting server console before.
    – Nishanth
    Commented Dec 18, 2019 at 23:06

Try it without the @:


Here’s the help article with an example: https://developer.salesforce.com/docs/atlas.en-us.mc-programmatic-content.meta/mc-programmatic-content/ssjs_utilitiesVariableSetValue.htm

  • I tried it but the URL never receives the value.
    – Nishanth
    Commented Dec 17, 2019 at 23:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .