The following examples shows how you can send and receive messages using our REST API and JavaScript. 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
function sendMessage() { function serialize(obj) { var str = []; for(var p in obj) if (obj.hasOwnProperty(p)) { str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); } return str.join("&"); } function getXMLHttpRequest() { if (window.XMLHttpRequest) { return new window.XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP"); } catch (ex) { return null; } } } function handler() { if (oReq.readyState == 4 /* complete */ ) { if (oReq.status == 200) { console.log(oReq); //handle response } } } var oReq = getXMLHttpRequest(); if (oReq != null) { 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 += "?" + serialize(sendRequest); oReq.open("GET", url); oReq.onreadystatechange = handler; oReq.send();} }
Send SMS Message Advanced
function sendMessageAdv() { function getXMLHttpRequest() { if (window.XMLHttpRequest) { return new window.XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP"); } catch (ex) { return null; } } } function handler() { if (oReq.readyState == 4 /* complete */ ) { if (oReq.status == 200) { console.log(oReq); //handle response } } } var oReq = getXMLHttpRequest(); if (oReq != null) { var sendRequestAdv = { 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 numbers 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"; oReq.open("POST", url); oReq.onreadystatechange = handler; oReq.setRequestHeader("Content-type","application/json"); oReq.send(JSON.stringify(sendRequestAdv)); } }
Receive Messages
function getMessages() { function serialize(obj) { var str = []; for(var p in obj) if (obj.hasOwnProperty(p)) { str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); } return str.join("&"); } function getXMLHttpRequest() { if (window.XMLHttpRequest) { return new window.XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP"); } catch (ex) { return null; } } } function handler() { if (oReq.readyState == 4 /* complete */ ) { if (oReq.status == 200) { console.log(oReq); //handle response } } } var oReq = getXMLHttpRequest(); if (oReq != null) { 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 += "?" + serialize(getMessagesRequest); oReq.open("GET", url); oReq.onreadystatechange = handler; oReq.send(); } }
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
function getStatus() { function getXMLHttpRequest() { if (window.XMLHttpRequest) { return new window.XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP"); } catch (ex) { return null; } } } function handler() { if (oReq.readyState == 4 /* complete */ ) { if (oReq.status == 200) { console.log(oReq); //handle response } } } var oReq = getXMLHttpRequest(); if (oReq != null) { 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 number(s) array "1b07ea71-0376-444a-a7f0-1a7ea479faaf", "791e291a-8436-49b3-8a39-5f6333cb1322" ] }; var url = "http://api.123-txt.com/Api123WCF.svc/rest/SentSmsStatus"; oReq.open("POST", url); oReq.onreadystatechange = handler; oReq.setRequestHeader("Content-type","application/json"); oReq.send(JSON.stringify(statusRequest)); } }
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.