The SMSGlobal Node library provides convenient access to the SMSGlobal REST API from node applications.
Sign up for a free SMSGlobal account today and get your API Key from our advanced SMS platform, MXT. Plus, enjoy unlimited free developer sandbox testing to try out your API in full!
Check out the code examples
Rest API credentials can be provided in the SMSGlobal client or node environment variables. The credential variables are SMSGLOBAL_API_KEY
and SMSGLOBAL_API_SECRET
npm install --save smsglobal
- Require
smsglobal
in your file
const apiKey = 'YOUR_API_KEY';
const apiSecret = 'YOUR_API_SECRET';
var smsglobal = require('smsglobal')(apiKey, secret);
All method return promise if no callback is given
var payload = {
origin: 'from number',
destination: 'destination',
message: 'This is a test message'
}
smsglobal.sms.send(payload, function (error, response) {
console.log(response);
});
var promise = smsglobal.sms.getAll();
promise
.then(function(response) {
console.log(response)
})
.catch(function(error){
console.log(error)
});
var id = 'outgoing-sms-id';
var promise = smsglobal.sms.get(id);
promise
.then(function(response) {
console.log(response)
})
.catch(function(error){
console.log(error)
});
var promise = smsglobal.sms.incoming.getAll();
promise
.then(function(response) {
console.log(response)
})
.catch(function(error){
console.log(error)
});
var id = 'incoming-sms-id';
var promise = smsglobal.sms.incoming.get(id);
promise
.then(function(response) {
console.log(response)
})
.catch(function(error){
console.log(error)
});
var payload = {
origin: 'from number',
message: '{*code*} is your SMSGlobal verification code.',
destination: 'destination'
};
// {*code*} placeholder is mandatory and will be replaced by an auto generated numeric code.
smsglobal.otp.send(payload, function(error, response) {
if (response) {
console.log(response);
}
if (error) {
console.log(error);
}
});
Success response object
{
statusCode: 200,
status: 'OK',
data: {
requestId: '404372541683676561917558',
destination: '61400000000',
validUnitlTimestamp: '2020-11-18 17:08:14',
createdTimestamp: '2020-11-18 16:58:14',
lastEventTimestamp: '2020-11-18 16:58:14',
status: 'Sent'
}
}
Error response object in the case of validation error
{
statusCode: 400,
status: 'Bad Request',
data: {
errors: {
message: {
errors: [
'Message template should contain a placeholder for code i.e. {*code*}.'
]
}
}
}
}
The OTP request can be cancelled if it's not expired and verified yet. It can be done by either using requestId
or destination number
. The followings are examples of each method:
var id = 'otp-request-id'; // requestId received upon sending an OTP
var promise = smsglobal.otp.cancelByRequestId(id)
promise.then((response) => {
console.log(response)
}).catch((err) => {
console.log(error)
});
var destination = 'destination-number';
var promise = smsglobal.otp.cancelByDestination(id)
promise.then((response) => {
console.log(response)
}).catch((err) => {
console.log(error)
});
Success response object
{
statusCode: 200,
status: 'OK',
data: {
requestId: '404372541683676561917558',
destination: '61400000000',
validUnitlTimestamp: '2020-11-18 17:08:14',
createdTimestamp: '2020-11-18 16:58:14',
lastEventTimestamp: '2020-11-18 16:58:14',
status: 'Cancelled'
}
}
The OTP code entered by your user can be verified by either using requestId
or destination number
. The followings are examples of each method:
var id = 'otp-request-id'; // requestId received upon sending an OTP
var code = 'otp-code'; // input code entered by your user
smsglobal.otp.verifyByRequestId(id, code, function(error, response) {
if (response) {
console.log(response);
}
if (error) {
console.log(error);
}
});
var destination = 'destination-number';
var code = 'otp-code'; // input code entered by your user
smsglobal.otp.verifyByDestination(id, code, function(error, response) {
if (response) {
console.log(response);
}
if (error) {
console.log(error);
}
});
Success response object
{
statusCode: 200,
status: 'OK',
data: {
requestId: '404372541683676561917558',
destination: '61400000000',
validUnitlTimestamp: '2020-11-18 17:08:14',
createdTimestamp: '2020-11-18 16:58:14',
lastEventTimestamp: '2020-11-18 16:58:14',
status: 'Verified'
}
}
Run the tests:
npm test
To run test with code coverage report
npm run mocha-only
- Sms
- Sms Incoming
- OTP (beta)
For any query contact us