Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save camrobert/b32f36cf6bfa4ddf7db0df2f0e43e7a0 to your computer and use it in GitHub Desktop.
Save camrobert/b32f36cf6bfa4ddf7db0df2f0e43e7a0 to your computer and use it in GitHub Desktop.
Code Snippet
<script runat="server">
/*
Ref: https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_httpPost.html
Ref: https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/validateEmail.html
*/
Platform.Load("Core","1");
try {
/*=== (unsafe) Plain Text API Credentials ===*/
var authurl = "https://mcxxxxxxxxxxxxxxxx.auth.marketingcloudapis.com/";
var client_id = "xxxxxxxxxxxxxx";
var client_secret = "xxxxxxxxxxxxxxxx";
/*=== Get Token ===*/
var gettoken = HTTP.Post(authurl+"v2/token", 'application/json', Stringify({"grant_type": "client_credentials","client_id": client_id,"client_secret": client_secret}));
if (gettoken.StatusCode == 200) {
var tokenresponseJSON = Platform.Function.ParseJSON(gettoken.Response[0]);
var accessToken = tokenresponseJSON.access_token;
var restUrl = tokenresponseJSON.rest_instance_url;
//Write(accessToken+"<br><br>"); //token
}
else {
throw new Error("Error fetching access token");
}
/*=== SFMC REST API ===*/
var route = "address/v1/validateEmail";
var payload = {"email": "testing@cameronrobert.com.au","validators": [ "SyntaxValidator", "MXValidator", "ListDetectiveValidator" ]};
var headerNames = ["Authorization"];
var headerValues = ["Bearer "+accessToken];
var result = HTTP.Post(restUrl+route, contentType, Stringify(payload), headerNames, headerValues);
if (result.StatusCode == 200) {
var responseJSON = Platform.Function.ParseJSON(result.Response[0]);
Write(Stringify(responseJSON)+"<br><br>"); //response
}
else {
throw new Error("Error calling API");
}
} //end try
catch(error) {
Write('Message: '+ error);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment