Skip to main content
Best answer

How do I use Javascript to send SMS using Code?


Hello,

 

I am trying to sent an SMS using “code by zapier” and javascript. My SMS provider tells me that I need to use this javascript code.

 

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer <api_key>");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
"message": "This is test and \n this is a new line",
"to": "+35799999999999",
"bypass_optout": true,
"sender_id": "SMSto",
"callback_url": "https://example.com/callback/handler"
});

var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};

fetch("https://api.sms.to/sms/send", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

 

But when I run this on zapier I get the following error.

 

If you are doing async (with fetch library) you need to use a callback!

 

I have no experience with javascript but I think it has something to do with the last 3 lines of this code. I searched for solutions and found a few suggestions, but I don’t know how to implement them correctly and whatever I try the error keeps changing.

 

What is the correct way of using this code to send SMS using javascript on zapier?

 

Thanks

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

13 replies

Userlevel 7
Badge +14

Hi @aristosv 

Good question.

Try using a Webhook - POST Zap action instead of a Code step.

 

Using a webhook will cost me an additional action. And zapier is expensive. I need to do this using code.

Userlevel 7
Badge +14

@aristosv 

You could use the Webhook action instead of the Code step, which would be the same amount of Tasks.

All the Code step is doing is sending a POST request.

I am already using code to perform other tasks. I just need to include sending an sms using code. if I add a webhook action, its additional cost.

 

do you know what need to be changed in the code I posted, to make this work in code?

Userlevel 7
Badge +14

@aristosv 

Help articles for using Code app: https://zapier.com/apps/code/help

As I mentioned, I have no experience with javascript, and I tried various suggestions I found online, but couldn’t make it work. That’s why I need help.

Userlevel 7
Badge +14

@aristosv 

Perhaps consider hiring a Zapier Expert for help: https://zapier.com/experts

Userlevel 7
Badge +11

Hi @aristosv!

As Troy mentioned, you can use Webhooks by Zapier to do this.

Based on what you shared above, and what I found here, this is how you would set it up:

 

It looks like none of these are necessarily required:

 

You can read more about Sender ID here: https://intergo.freshdesk.com/support/solutions/articles/43000513909.

To get your API key I think you go here: https://sms.to/app#/api/client.

Hope that helps!

As I mentioned, using webhooks incurs additional cost. zapier is already comparatively expensive so I’m trying to reduce costs by using code. Never mind though, I figured it out.

Userlevel 7
Badge +12

Hi @aristosv, thanks for letting us know that you were able to figure it out, are you able to share your solution with us?

var myHeaders = new fetch.Headers();
myHeaders.append("Authorization", "Bearer <key>");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
"message": "message_here",
"to": phone_here,
"sender_id": "senderid_here"
});

var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};

fetch("https://api.sms.to/sms/send", requestOptions)
.then(function(res) {
return res.text();
})
.then(function(body) {
var output = {rawHTML: body};
callback(null, output);
})
.catch(callback);

 

Userlevel 7
Badge +12

That’s awesome, nice work and thanks so much for sharing it 😊

Userlevel 7
Badge +11

As I mentioned, using webhooks incurs additional cost. zapier is already comparatively expensive so I’m trying to reduce costs by using code. Never mind though, I figured it out.

 

Ah, my apologies. I’m more familiar with webhooks than javascript, so I offered that. Glad you managed to figure it out, and thanks for sharing!