javascripttwiliotwilio-apisalesforce-marketing-cloudssjs

POST request from Salesforce Marketing Cloud cloudpage to Twilio using SSJS


I am trying to send a POST request to Twilio's SMS API from Marketing Cloud's cloudpage using SSJS. I am getting a 401 unauthorized access even though I have added ACCOUNT_SID AND AUTH_TOKEN in the URL.

<script type="text/javascript" runat="server">
      Platform.Load("core", "1");
  
  var config = {
        endpoint: "https://XXXX:XXXX@api.twilio.com/2010-04-01/Accounts/XXXX/Messages.json",
        contentType: "application/x-www-form-urlencoded",
       payload : "From=+0000&To=+0000&Body=Test1"
    }
  try {
        var httpResult = HTTP.Post(config.endpoint, config.contentType, config.payload);
            var result = JSON.parse(httpResult.response);
    Write(httpResult.StatusCode);
         Write('result' + result);
  } catch(error) { Write(Stringify(error)); }
  </script>

I get this error: {"message":"An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.","description":"ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.\r\n Error Code: OMM_FUNC_EXEC_ERROR\r\n - from Jint --> \r\n\r\n --- inner exception 1---\r\n\r\nSystem.Net.WebException: The remote server returned an error: (401) Unauthorized. - from System\r\n\r\n\r\n\r\n"}

I have tried using client side JavaScript, but I get the same error.

<script>

const url = 'https://XXXX:XXXX@api.twilio.com/2010-04-01/Accounts/XXXX/Messages.json';
  
  var xhr = new XMLHttpRequest();
xhr.open("POST", url);

xhr.setRequestHeader("Accept", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  
 var data = 'From=+0000&To=+0000&Body=Test1';

xhr.send(data);
 
</script>


Solution

  • Twilio developer evangelist here.

    I'm not familiar with SSJS and I'm finding it hard to connect the documentation(?) with what you've written. If this documentation refers to the same HTTP.Post method you are using then try something like this:

    <script type="text/javascript" runat="server">
      Platform.Load("core", "1");
      
      var accountSid = YOUR_ACCOUNT_SID;
      var authToken  = YOUR_AUTH_TOKEN;
      var auth = btoa(accountSid + ":" + authToken);
    
      var config = {
        endpoint: "https://api.twilio.com/2010-04-01/Accounts/XXXX/Messages.json",
        contentType: "application/x-www-form-urlencoded",
        payload : "From=+0000&To=+0000&Body=Test1"
      };
    
      try {
        var httpResult = HTTP.Post(
          config.endpoint,
          config.contentType,
          config.payload,
          ["Authorization"],
          ["Basic " + auth]
        );
          
        var result = JSON.parse(httpResult.response);
        Write(httpResult.StatusCode);
        Write('result' + result);
      } catch(error) {
        Write(Stringify(error));
      }
    </script>
    

    In this case I have concatenated the accountSid and authToken separated by a ":" then base 64 encoded the result. I then added an Authorization header to the HTTP request and set the value to Basic plus the base64 encoded credentials.