Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send TextLocal SMS using Node.js
//This code was posted for an article at https://codingislove.com/send-sms-developers/
const axios = require("axios");
const tlClient = axios.create({
baseURL: "https://api.textlocal.in/",
params: {
apiKey: "YOUR API KEY", //Text local api key
sender: "6 CHARACTER SENDER ID"
}
});
const smsClient = {
sendPartnerWelcomeMessage: user => {
if (user && user.phone && user.name) {
const params = new URLSearchParams();
params.append("numbers", [parseInt("91" + user.phone)]);
params.append(
"message",
`Hi ${user.name},
Welcome to iWheels, Download our app to get bookings from our customers with better pricing.
https://iwheels.co`
);
tlClient.post("/send", params);
}
},
sendVerificationMessage: user => {
if (user && user.phone) {
const params = new URLSearchParams();
params.append("numbers", [parseInt("91" + user.phone)]);
params.append(
"message",
`Your iWheels verification code is ${user.verifyCode}`
);
tlClient.post("/send", params);
}
}
};
module.exports = smsClient;
// Now import the client in any other file or wherever required and run these functions
// const smsClient = require("./smsClient");
// smsClient.sendVerificationMessage(user)
@ranjithkumar8352

This comment has been minimized.

Copy link
Owner Author

@ranjithkumar8352 ranjithkumar8352 commented Jul 26, 2019

Guys, This is just the SMS client code. You need to import it in another file and use it. Don't just blindly copy-paste the code. Read the code and modify it as per your needs.

const smsClient = require("./smsClient"); //Modify the path based on your app
const user = {name: "Ranjith", phone: "1234567891"};
smsClient.sendPartnerWelcomeMessage(user);

Also, Change the SMS message content in client code as per your approved templates. Read the full tutorial here - https://codingislove.com/send-sms-developers/

@gbharani5

This comment has been minimized.

Copy link

@gbharani5 gbharani5 commented Apr 14, 2020

As per the Text Local documentation, if POST method is used parameters should be sent in POST headers.
Using this source code the response is always "Invalid login credentials". Normal Axios GET method works fine.

@ranjithkumar8352

This comment has been minimized.

Copy link
Owner Author

@ranjithkumar8352 ranjithkumar8352 commented Apr 14, 2020

@gbharani5 I'm still using the same code from the last 2 years and it works perfectly fine. Check if your API key and sender id are correct or not?

@gbharani5

This comment has been minimized.

Copy link

@gbharani5 gbharani5 commented Apr 14, 2020

For the same APIKey and sender when used GET method it worked.

@ninjasujan

This comment has been minimized.

Copy link

@ninjasujan ninjasujan commented Apr 28, 2020

I have a plan to use this in my real time applicayion, but in Text local docs no support for Nodejs and then how you implemented.

@ranjithkumar8352

This comment has been minimized.

Copy link
Owner Author

@ranjithkumar8352 ranjithkumar8352 commented Apr 28, 2020

@ninjasujan It is implemented using Text local's Rest API. You can make an API request from any language.

@AjinkyaTaranekar

This comment has been minimized.

Copy link

@AjinkyaTaranekar AjinkyaTaranekar commented Jul 2, 2020

I have added your smsClient.js in my react-native app, but it is not working, I have checked API key and sender but it didn't work. Please help.

@rajeshwarpatlolla

This comment has been minimized.

Copy link

@rajeshwarpatlolla rajeshwarpatlolla commented Jul 31, 2020

As per the Text Local documentation, if POST method is used parameters should be sent in POST headers.
Using this source code the response is always "Invalid login credentials". Normal Axios GET method works fine.

@gbharani5 Can you share the sample code which is working for u. I am getting the below error.
Screenshot 2020-08-01 at 2 00 08 AM

@gbharani5

This comment has been minimized.

Copy link

@gbharani5 gbharani5 commented Aug 1, 2020

`const axios = require("axios");
const {URLSearchParams} = require('url')

const apiKey = "" // YOUR API KEY HERE
const baseURL = "http://api.textlocal.in"

const smsClient = {
sendPartnerWelcomeMessage: user => {
if (user && user.phone && user.name) {
const params = new URLSearchParams();
params.append("apikey",apiKey);
params.append("sender","TXTLCL");
params.append("numbers", user.phone);
params.append(
"message",
Hi ${user.name},
);
axios.get(baseURL+"/send/?"+params.toString()).then(res=>{console.log(res.data)})
}
},
sendVerificationMessage: contactNumber => {
if (contactNumber) {
const params = new URLSearchParams();
params.append("apikey",apiKey);
params.append("sender","YOUR PARTNER KEY HERE"); // Please change you value here
params.append("numbers", contactNumber);
params.append(
"message",
Your One Time Verification code is
);
return axios.get(baseURL+"/otp_send/?"+params.toString())

}

},
checkBalance : user => {
axios.get(baseURL+"/balance").then(res=>{console.log(res.data)})
},

validateVerificationMessage: user => {
if (user && user.contactNumber) {
const params = new URLSearchParams();
params.append("apikey",apiKey);
params.append("numbers", user.contactNumber);
params.append("code",user.token);

  return axios.get(baseURL+"/otp_challenge/?"+params.toString())

}

},
};

module.exports = smsClient;`

@gbharani5

This comment has been minimized.

Copy link

@gbharani5 gbharani5 commented Aug 1, 2020

Hope this works for you

@rajeshwarpatlolla

This comment has been minimized.

Copy link

@rajeshwarpatlolla rajeshwarpatlolla commented Aug 1, 2020

Thanks for sending the code @gbharani5
I came up with a simple solution for sending sms.

var url = 'https://api.textlocal.in/send/?apikey=<API_KEY>&numbers=<NUMBER>&sender=TXTLCL&message=' + encodeURIComponent('OTP to login to app is 123456');
  axios
    .get(url)
    .then(function (response) {
      console.log(response.data);
    })
    .catch(function (error) {
      console.log(error);
    });
@dsh819

This comment has been minimized.

Copy link

@dsh819 dsh819 commented Apr 22, 2021

{
errors: [ { code: 4, message: 'No recipients specified' } ],
status: 'failure'
}

const tlClient = axios.create({
baseURL: "https://api.textlocal.in/",
params: {
apiKey: "M2U3NTE2MjRkMzRkZWQ4NDZjZmRmYzE5ZGZmODkxNmE=", //Text local api key
sender: "TXTLCL"
}
});

var phone = 919742039640
const params = new URLSearchParams();
params.append("numbers", phone);
params.append(
"message",
Your iWheels verification code is Harshith
);
tlClient.get("/send", params)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});

Also, can let me know what is the partner id
Can anyone please help me,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment