Archive > API Implementations > SSJS SMS Code Fix for APIs

Follow this documentation when setting up APIs forms that trigger an SMS pre-opt-in message from mobile connect.

Create all SMS Pre-Opt-In Message in the UPMC Business Unit

All pre opt-in messages should be created in the UPMC BU.

The first section of code is setting all the SSJS variables and getting the Authentication Bearer
  • Prekeyword is what the phone number will be opted into when the first SMS message goes out. The message will contain the keyword we want them to opt in to. This needs to be created in the UPMC BU
  • MessageAPIKey is an API outbound message that will be used as the first step of the double opt-in message. This needs to be created in the UPMC BU
  • client_id client_secret are the API login credentials to get to get access to execute an API 
  • accountID is the BU where we are executing this code

/******** SMS REPLY TO OPT IN ********/

SET @keyword = ‘WOMEN’

SET @prekeyword = ‘PREWOMEN”
SET @MessageAPIKey = ‘MjAwOjc4OjA’
SET @accountID = ‘7200479’ /* SMS MESSAGE BUILT IN UPMC BU */

]%%

<script runat=”server”>
Platform.Load(“Core”,”1.1.1″);

var phone_number = “1” + Platform.Variable.GetValue(“@phone_number”),
email_address = Platform.Variable.GetValue(“@email_address”);
keyword = Platform.Variable.GetValue(“@keyword”);
prekeyword = Platform.Variable.GetValue(“@prekeyword”);
MessageAPIKey = Platform.Variable.GetValue(“@MessageAPIKey”);
accountID = Platform.Variable.GetValue(“@accountID”);

if(phone_number != ”) {
try {
/*********************** Get Authenication token *********************/
var authEndpoint = “https://mcdw3xqqr5zvbbnr93flxd4v425y.auth.marketingcloudapis.com”; //provide authentication endpoint
var payload = {
grant_type: “client_credentials”,
client_id: “1u7t6asuey4r6xtbzr3lnox0”, //provide Client Id
client_secret: “nCjFQ85HHdD0baEATNVdxyTZ”, //provide Client Secret
scope: “sms_read sms_write sms_send”, //provide Client Secret
account_id: accountID
};


var url = authEndpoint + “/v2/token”;
var contentType = “application/json”;

Write(Stringify(payload));

var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));

if(accessTokenRequest.StatusCode == 200) {
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);

var accessToken = tokenResponse.access_token;
}

/*********************** Get Authenication token *********************/

 

The second section of code is firing the SMS message. At this point we have the Access Token to perform an SMS message.

 

/********** Make a call using the messageContact route ********************/

/* Write(Stringify(accessToken)); */
var headerNames = [“Authorization”];
var headerValues = [“Bearer ” + accessToken];
var smsEndpoint = “https://mcdw3xqqr5zvbbnr93flxd4v425y.rest.marketingcloudapis.com/”; //provide rest endpoint
var smsPayload = {
“mobileNumbers”: [ phone_number ],
“Subscribe”: true,
“Resubscribe”: true,
“keyword”: prekeyword 
};

Write(Stringify(smsPayload));

var smsUrl = smsEndpoint + “sms/v1/messageContact/”+MessageAPIKey+”/send”; //provide message API key

Write(smsUrl);

var sendSMS = HTTP.Post(smsUrl, contentType, Stringify(smsPayload), headerNames, headerValues);

/********** Make a call using the messageContact route ********************/

} catch (error) {
Write(Stringify(error));
}
}
</script>

Note: Be sure to include subscribe and resubscribe = true in the code.

Check to see if a specific phone number has been added to a keyword:

Note: Be sure to include subscribe and resubscribe = true in the code.

Check these previous APIs for reference:

Your email address will not be published. Required fields are marked *