Skip to content

Instantly share code, notes, and snippets.

@ranjithkumar8352
Last active July 26, 2023 19:23
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranjithkumar8352/56681a9bd5f2603bec7444ef71604ed2 to your computer and use it in GitHub Desktop.
Save ranjithkumar8352/56681a9bd5f2603bec7444ef71604ed2 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@AjinkyaTaranekar
Copy link

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
Copy link

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
Copy link

`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
Copy link

Hope this works for you

@rajeshwarpatlolla
Copy link

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
Copy link

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,

@sufiyan1234
Copy link

I am getting a huge delay in receiving the OTP!!!. How to solve the issue?

@sawrubgupta
Copy link

I am getting this error
{
errors: [ { code: 204, message: 'Invalid message content' } ],
status: 'failure'
}
anyone solved it ?

@sufiyan1234
Copy link

I am getting this error { errors: [ { code: 204, message: 'Invalid message content' } ], status: 'failure' } anyone solved it ?

I guess it is because your message format might not be matching the textlocal template created to send messages. You can check the template on going to textlocal dashboard and selecting the "Send" drop down -> "Send text messages". You would see "Message Details" on the left hand side where you would also be able to see "Templates".

@ramusarithak
Copy link

I'm getting this error
errors: [ { code: 3, message: 'Invalid login details' } ],)

what should i do in there place can you please let me know

@sufiyan1234
Copy link

@ramusarithak My code is as follows:

params.append("numbers", countryCode + phone); params.append( "message", Your OTP for {COMPANY-NAME} login is ${SmsService.otp}. Do not share your OTP with anyone. - {COMPANY-NAME}`,
);
params.append("apiKey", process.env.apiKey);
params.append("sender", process.env.sender);

fetch(`${process.env.textLocalBaseURL}/send`, { method: "POST", body: params });`

You get the api key in textlocal in Settings->API keys and sender from Send-> Send Text messages -> (On the left you would see "Messaage Details" and the sender name)
Text local baseurl is https://api.textlocal.in/

@ramusarithak
Copy link

@sufiyan1234 Thanks for share this info!

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