I am trying to make an API request in SFMC to validate email addresses, which requires first to get the Access Token.

I was able to request the access token, but then the next HTTP.Post request I do doesn't work. I have this far:


var dataRows = Platform.Function.LookupRows('NameofEmailsDE','ColumnName2Check','ValueName');
 if(dataRows && dataRows.length > 0) {
      for(var i=0; i<dataRows.length; i++) {

        var EmailAddress = dataRows[i]["ColumnNameToRetrieve"];

try {
        var authEndpoint = "https://uniqueclienturlapi.auth.marketingcloudapis.com";
        var client_id = "XXXXXXXXXXX";
        var client_secret = "XXXXXXXXXXX";
        var account_id = "XXXXXXXXX";
        var grant_type = "client_credentials";

        // auth        
        var url = authEndpoint + '/v2/token';
        var contentType = 'application/json';
        var payload = '{"grant_type":"' + grant_type + '",';
        payload += '"client_id":"' + client_id + '",';
        payload += '"client_secret":"' + client_secret + '"}';

        // create token
        var accessTokenResult = HTTP.Post(url, contentType, payload);
        var statusCode = result["StatusCode"];
        var response = accessTokenResult["Response"][0];
        var access_token = Platform.Function.ParseJSON(response).access_token; 

 /* Until here, the request works, and I get the access_token. The issue comes in this next request when I have to add to the HTTP.Post the headerNames + headerValues */

        var restEndpoint = "https://uniqueclienturlapi.rest.marketingcloudapis.com";
        restEndpoint += "/address/v1/validateEmail";  
        var headerNames = ["Authorization"];
        var headerValues = ["Bearer " + access_token]; 

        // payload
          var restpayload = '{';
          restpayload += ' "email": "' + EmailAddress + '",'; 
          restpayload += ' "validators": [ "SyntaxValidator", "MXValidator", "ListDetectiveValidator" ]';
          restpayload += '}';                            

        try {
            var restresult = HTTP.Post(restEndpoint, contentType, restpayload, headerNames, headerValues);
            var reststatusCode = result["StatusCode"];
            var response = restresult["Response"][0];
            var emailresponse = Platform.Function.ParseJSON(response).email;
            var validresponse = Platform.Function.ParseJSON(response).valid;
            var failedValidationresponse = Platform.Function.ParseJSON(response).failedValidation;
        catch (err)  {
            // error

  catch (err)  {
    // error

I would appreciate any help! Thanks a lot

  • Can you give more info on how it does not work? Does it error? Not produce expected results? etc? – Gortonington Oct 1 at 13:36
  • 1
    Agreed that you should share the output of your error so we can better help. Also, you should move your request for the access token outside of the for loop. Right now you are requesting a new access token for each iteration of the loop when a single request would be sufficient. – Jason Hanshaw Oct 1 at 18:49
  • Unfortunately, I don't have any more information regarding the error message since I am running this script first in a Cloud Page. In the Automation Studio, there's also no info when the automation fails. That's why I decided to try in the CloudPages first. . Can you help me to put the accestoken request outside the loop? – Bruda Oct 7 at 8:16
  • If you uncomment your //Write("error") and change it to Write(Stringify(err)) you should be able to get an output on the CloudPage of what the issue is. To move the auth token call outside the loop, just move that above the for loop and remove the try/catch that is around it inside the loop. – Gortonington Oct 7 at 12:57
  • Hi Gortonington, thanks for your explanation. I moved the auth token call above the "for". I got following error-message: "{"message":"Unable to retrieve security descriptor for this frame.","description":"System.InvalidOperationException: Unable to retrieve security descriptor for this frame. - from mscorlib\r\n\r\n"}{"message":"Unable to retrieve security descriptor for this frame.","description":"System.InvalidOperationException: Unable to retrieve security descriptor for this frame. - from mscorlib\r\n\r\n"}" But I didn't remove the first try-catch that nests the second call – Bruda Oct 9 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.