Send SMS messages through the LabsMobile platform and the node library.
- Labsmobile API documentation can be found here.
- Send SMS messages.
- Get account credits
- Get prices by country
- Manage scheduled sendings
- HLR Request (Check mobile)
- A user account with LabsMobile. Click on the link to create an account here.
- This library supports node v16.19.0 and higher versions of node.
To install the labsmobile-sms library, it is recommended to use npm.
npm i labsmobile-sms
"dependencies": {
"labsmobile-sms": "1.0.1"
}
Send SMS
Here is an example of using the library to send a SMS:
const LabsMobileClient = require("labsmobile-sms/src/LabsMobileClient");
const LabsMobileModelTextMessage = require("labsmobile-sms/src/LabsMobileModelTextMessage");
const ParametersException = require("labsmobile-sms/src/Exception/ParametersException");
const RestException = require("labsmobile-sms/src/Exception/RestException");
async function sendSms() {
try {
const username = "myusername";
const token = "mytoken";
const message = "Test SMS";
const phone = ["34XXXXXXXXX"];
const clientLabsMobile = new LabsMobileClient(username, token);
const bodySms = new LabsMobileModelTextMessage(phone, message);
const response = await clientLabsMobile.sendSms(bodySms);
console.log(response);
} catch (error) {
if (error instanceof ParametersException) {
console.log(error.message);
} else if (error instanceof RestException) {
console.log(`Error: ${error.status} - ${error.message}`);
} else {
throw new Error("Error: " + error);
}
}
}
Get account credits
Here is an example to learn credits for an existing account:
const LabsMobileClient = require("labsmobile-sms/src/LabsMobileClient");
const ParametersException = require("labsmobile-sms/src/Exception/ParametersException");
const RestException = require("labsmobile-sms/src/Exception/RestException");
async function getCreditTest() {
try {
const username = "myusername";
const token = "mytoken";
const clientLabsMobile = new LabsMobileClient(username, token);
const response = await clientLabsMobile.getCredit();
console.log(response);
} catch (error) {
if (error instanceof ParametersException) {
console.log(error.message);
} else if (error instanceof RestException) {
console.log(`Error: ${error.status} - ${error.message}`);
} else {
throw new Error("Error: " + error);
}
}
}
Manage scheduled sendings
Here is an example you can cancel or execute the scheduled sendings that are pending for execution:
const LabsMobileClient = require("labsmobile-sms/src/LabsMobileClient");
const LabsMobileModelScheduledSendings = require("labsmobile-sms/src/LabsMobileModelScheduledSendings");
const ParametersException = require("labsmobile-sms/src/Exception/ParametersException");
const RestException = require("labsmobile-sms/src/Exception/RestException");
async function scheduledSendingsTest() {
try {
const username = "myusername";
const token = "mytoken";
const subid = "mysubid";
const cmd = "XXXXXX"; // send or cancel
const clientLabsMobile = new LabsMobileClient(username, token);
const scheduledSendings = new LabsMobileModelScheduledSendings(subid, cmd);
const response = await clientLabsMobile.scheduledSendings(
scheduledSendings
);
console.log(response);
} catch (error) {
if (error instanceof ParametersException) {
console.log(error.message);
} else if (error instanceof RestException) {
console.log(`Error: ${error.status} - ${error.message}`);
} else {
throw new Error("Error: " + error);
}
}
}
Get prices by country
Here is an example to know the credits that a single sending will take depending on the country of delivery:
const LabsMobileClient = require("labsmobile-sms/src/LabsMobileClient");
const LabsMobileModelCountryPrice = require("labsmobile-sms/src/LabsMobileModelCountryPrice");
const ParametersException = require("labsmobile-sms/src/Exception/ParametersException");
const RestException = require("labsmobile-sms/src/Exception/RestException");
async function getpricesCountryTest() {
try {
const username = "myusername";
const token = "mytoken";
const countries = ["CO", "ES"];
const clientLabsMobile = new LabsMobileClient(username, token);
const countriesPrice = new LabsMobileModelCountryPrice(countries);
const response = await clientLabsMobile.getpricesCountry(countriesPrice);
console.log(response);
} catch (error) {
if (error instanceof ParametersException) {
console.log(error.message);
} else if (error instanceof RestException) {
console.log(`Error: ${error.status} - ${error.message}`);
} else {
throw new Error("Error: " + error);
}
}
}
HLR Request
Here is an example queries the mobile phone status with the related information like current operator, format, active, ported information, subscription country, etc:
const LabsMobileClient = require("labsmobile-sms/src/LabsMobileClient");
const LabsMobileModelHlrRequest = require("labsmobile-sms/src/LabsMobileModelHlrRequest");
const ParametersException = require("labsmobile-sms/src/Exception/ParametersException");
const RestException = require("labsmobile-sms/src/Exception/RestException");
async function hlrRequestTest() {
try {
const username = "myusername";
const token = "mytoken";
const numbers = [];
const clientLabsMobile = new LabsMobileClient(username, token);
const hlr = new LabsMobileModelHlrRequest(numbers);
const response = await clientLabsMobile.hlrRequest(hlr);
console.log(response);
} catch (error) {
if (error instanceof ParametersException) {
console.log(error.message);
} else if (error instanceof RestException) {
console.log(`Error: ${error.status} - ${error.message}`);
} else {
throw new Error("Error: " + error);
}
}
}
If you have questions, you can contact us through the support chat or through the support email support@labsmobile.com.