Subscribe Mobile Numbers to a Keyword with MobileConnect API in Salesforce Marketing Cloud

Author: Florin Valean

Use case

This solution can be useful when we need to implement a double opt-in process. A common scenario would be when customers provide their consent through different channels to be contacted via SMS but we are required to implement a double opt-in process. In this case MobileConnect QueueMO REST API can be used to simulate the first step in this process (the first text sent by customers in a regular double opt-in process) with a mobile originated message with a specific keyword.
MobileConnect QueueMO REST API:

/* replace YOUR_SUBDOMAIN with your subdomain generated when you create the Installed Package in Marketing Cloud */


For this use case we are going to use an Installed Package created with enhanced functionality with a component of type API Integration and integration of type Server-to-Server. Make sure you assign all the scopes you need to the Installed Package.

We also need to create a Mobile Opt-In message in MobileConnect. See this page for more details.

OAuth 2.0 access token is required. For more details on how to obtain and how to use the access token go to this page.

The payload for /sms/v1/queueMO is in JSON format and it will look like this:

/* replace the mobile number, SHORT_CODE and KEYWORD with your own values */ { "mobileNumbers": ["44123456789"], "shortCode": "SHORT_CODE", "messageText": "KEYWORD" }

Server-side javascript sample code

You can create a landing page in Web Studio and include this code. Publish the page and load it in a web browser to test the code.
For this example our mobile numbers are stored in a data extension with external key MobileNumbersToSubscribe and we'll create the mobileNumbers attribute dynamically (a maximum of 250 normalized phone numbers can be included per API call).

/* replace the highlighted values with yours */ <script runat="server"> Platform.Load("Core", "1"); try { /* get OAuth 2.0 access token */ var payload = '{"grant_type":"client_credentials",'; payload += '"client_id":"d20aaul96htb2hgo9b4i47gx",'; payload += '"client_secret":"tUriuDS0zdV82vFrLIbJek77",'; payload += '"scope":null,'; payload += '"account_id":"012345678"}'; var OAuth2URL = ""; var contentType = 'application/json'; var accessTokenResult = HTTP.Post(OAuth2URL, contentType, payload); var tokenObj = Platform.Function.ParseJSON(accessTokenResult["Response"][0]); var accessToken = tokenObj.access_token; var headerNames = ["Authorization"]; var headerValues = ["Bearer " + accessToken]; /* build an array of mobile numbers from the data extension data extension external key is MobileNumbersToSubscribe the field that stores mobile numbers is named Mobile */ var sc = DataExtension.Init("MobileNumbersToSubscribe"); var data = sc.Rows.Retrieve(); var mobileNumbers = []; if(data.length) { for(i=0; i<data.length; i++) { mobileNumbers[i] = data[i].Mobile; } } /* create the payload */ var mobilePayload = '{"mobileNumbers": ' + Stringify(mobileNumbers) + ','; mobilePayload += '"shortCode": "67890",'; mobilePayload += '"messageText": "KEYWORD"}'; /* make the API call and get the response */ var content = [0]; var urlPost = ""; var response = HTTP.Post(urlPost, contentType, mobilePayload, headerNames, headerValues, content); Write(Stringify(response)); } catch(e) { Write(Stringify(e)); } </script>

Notice the use of try-catch block - see this page for more details on how/when to use try-catch.

View the new status in MobileConnect

As a result the Status attribute will be set to Opted In in MobileConnect and the Opt-In Method will be set to API.
This is how the contact membership will appear in MobileConnect:

API mobile subscription
Share this page
Stay in touch

Subscribe to the newsletter

p1 p2 p3