I would like to know if we can do an API call Using SSJS. My need is to extract data from a data extension and target an end point using API call. The SSJS is unfortunlly not documented very well.
I do a sample just to target end point using one data but the automation fail.
<script type="javascript" runat="server">
Platform.Load("core","1.1.5");
var url = 'https://mcmgxxxx.auth.marketingcloudapis.com/v2/token';
var contentType = 'application/json';
var payload = "";
payload += '{"grant_type": "client_credentials", "client_id":"xx","client_secret":"vxx"}';
var accessTokenResult = HTTP.Post(url, contentType, payload);
var statusCode = result["StatusCode"];
var response = accessTokenResult["Response"][0];
var accessToken = Platform.Function.ParseJSON(response).access_token;
url= "";
url = "https://mcmgxx.rest.marketingcloudapis.com";
url += "/interaction/v1/events";
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];
payload = "";
payload += '{';
payload += ' "ContactKey": "01010102",';
payload += ' "EventDefinitionKey":"APIEvent-xxx",';
payload += ' "Data": {';
payload += ' "C_CustomerID": "01010102"';
payload += ' },';
payload += ' }';
try {
result = HTTP.Post(url, contentType, payload, headerNames, headerValues);
result = Stringify(result).replace(/[\n\r]/g, '');
log.Rows.Add({"Message": "result: " + result});
} catch (e) {
e = Stringify(e).replace(/[\n\r]/g, '')
log.Rows.Add({"Message": "error: " + e});
}
</script>