The following examples show how you can send and receive messages using our REST API and JQuery. JQuery provides a built-in method that facilitates communication with the remote HTTP servers via the browser’s XMLHttpRequest object or via JSONP.
Send SMS Message
$(document).ready(function (e) { var sendRequest = { user: "user@email.goes.here", // Replace with your user pass: "user_password_goes_here", // Replace with your password source: "virtual_number", // Replace with your sender ID destination: "recipient_mobile", // Replace with the recipient number sms: "Test Message 1", // Replace with your message }; var url = "http://api.123-txt.com/Api123WCF.svc/rest/SendSms"; url += "?" + $.param(sendRequest); $.ajax({ type: 'GET', url: url, dataType: 'json', success: function (res) { console.log(res); //Handle Response } }); });
Send SMS Message Advanced
$(document).ready(function (e) { var sendRequest = { user: "user@email.goes.here", // Replace with your user pass: "user_password_goes_here", // Replace with your password source: "virtual_number", // Replace with your sender ID destinations: [ // Replace with the recipient number(s) array "dest_number_1", "dest_number_2", ], sms: "Test Message 1" // Replace with your message }; var url = "http://api.123-txt.com/Api123WCF.svc/rest/SendSmsAdvanced"; $.ajax({ type: 'POST', url: url, data: JSON.stringify(sendRequest), contentType: 'application/json', success: function (res) { console.log(res); //Handle Response } }); });
Receive Messages
$(document).ready(function (e) { var getMessagesRequest = { user: "user@email.goes.here", // Replace with your user pass: "user_password_goes_here", // Replace with your password maxRecords: "20", // Limit number of records returned lastDate: "2015-01-28 10:30:46" // Limit to this date and time }; var url = "http://api.123-txt.com/Api123WCF.svc/rest/ReceiveSms"; url += "?" + $.param(getMessagesRequest); $.ajax({ type: 'GET', url: url, contentType: 'application/json', success: function (res) { console.log(res); //Handle Response } }); });
Note: The response from this method will contain a date which you can then use to set to ‘lastDate’ in your next call, this will ensure you do not pull duplicate incoming messages from the service.
Get Message Statuses
$(document).ready(function (e) { var statusRequest = { user: "user@email.goes.here", // Replace with your user pass: "user_password_goes_here", // Replace with your password destIds: [ // Replace with the recipient numbers array "1b07ef75-0376-444a-a7f0-1a7ea479faaf", "791e2091-8436-49b3-8a39-5f6333cb1322", ] }; var url = "http://api.123-txt.com/Api123WCF.svc/rest/SentSmsStatus"; $.ajax({ type: 'POST', url: url, data: JSON.stringify(statusRequest), contentType: 'application/json', success: function (res) { console.log(res); //Handle Response } }); });
Note: SendSmsAdvanced returns the destId’s of each sent message, you must hold onto these IDs and use them within SentSmsStatus to identify their current and final delivery states.