Skip to content

Instantly share code, notes, and snippets.

@zuzannamj
Last active December 22, 2021 08:10
Embed
What would you like to do?
<script runat="server">
Platform.Load("Core","1.1.1");
try {
//API authentication
var authEndpoint = ""; //provide authentication endpoint
var payload = {
clientId: "", //provide Client Id
clientSecret: "" //provide Client Secret
};
var url = authEndpoint + "/v1/requestToken";
var contentType = "application/json";
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
if(accessTokenRequest.StatusCode == 200) {
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
var accessToken = tokenResponse.accessToken;
}
//make a call using the messageContact route
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];
var smsEndpoint = ""; //provide rest endpoint
var smsPayload = {
"mobileNumbers": [
"" //pass phone number
],
"Subscribe": true,
"Resubscribe": true,
"keyword": "" //provide KEYWORD
};
var smsUrl = smsEndpoint + "/sms/v1/messageContact/xxxxxxxxx/send"; //provide message API key
var sendSMS = HTTP.Post(smsUrl, contentType, Stringify(smsPayload), headerNames, headerValues);
} catch (error) {
Write(Stringify(error));
}
</script>
@rain89146
Copy link

this code is broken in every way, the HTTP.Post doesn't work at all

@zuzannamj
Copy link
Author

@rain89146 I just checked and the code works just fine, I even set up a CloudPage with the above code (unfortunately it only works with Polish phone numbers) - https://mcg7y3r488-d6mcg1dym88btc7g4.pub.sfmc-content.com/x1p4cdfw5ud
I suggest you take your code to https://salesforce.stackexchange.com/ and have someone help you fix it there instead of making vicious comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment