How to Send SMS Using Apidaze

Posted by Ali Barın on May 20, 2019 at 2:44 PM
Ali Barın
Connect:

 

Internet-supported communication surrounds our everyday lives. Although, that doesn't mean our need for SMS communication is any less critical. Many businesses still rely on SMS to reach their customers so today, we're showing you how to develop a function that sends SMS using our CPaaS product, Apidaze.Sending an SMS with Apidaze CPaaS (Photo by OOI JIET on Unsplash)

Prerequisite

Sending an SMS

 

Sending SMS is as simple as making an HTTP POST request with some data such as phone numbers, like destination and sender, and a text body. You can find additional details on the associated documentation page.

A sample usage with cURL

curl --location --request POST "https://api.apidaze.io//sms/send?api_secret=" \
  --header "Content-Type: application/x-www-form-urlencoded" \
  --data "from=15558675309&to=15551234567&body=Have%20a%20great%20day."

As can be seen above, we need an API key and secret to use this REST API call. Besides the API values, we need to define the content-type of the request as "application/x-www-form-urlencoded" as Apidaze supports it. Once we set all essential values for the request, it is time to give the necessary values for the SMS itself.

Payload

As mentioned, the payload should be a type of "application/x-www-form-urlencoded" and contain three key-values such as from, to and body. Remember that we must give phone numbers without "+" sign, and the number we pass for the "from" key must be assigned to the application on the Apidaze dashboard. Otherwise, a request going to this endpoint will eventually fail.

Involving JavaScript in

JavaScript is one of the most common programming languages used on client and server sides. Speaking of "client-side," we do not endorse directly sending a request to Apidaze from a website since it exposes the API secret! Therefore, we highly recommend developing a server-side application behind authentication and sending an SMS through that to have API usage under control.

Let's start writing our function to send SMS. Firstly, we suggest using an HTTP request library to send requests conveniently. Our preference would be "got" as it is powerful, lightweight, and we plan to use Node.js.

const got = require('got');
const API_KEY = 'key_comes_here';
const API_SECRET = 'secret_comes_here';

const sendSMS = (payload) => {
  return got.post(`https://api.apidaze.io/${API_KEY}/sms/send?api_secret=${API_SECRET}`, {
    body: `from=${payload.from}&to=${payload.to}&body=${payload.text}`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
};

const payload = {
  from: '15552221234',
  to: '17772221234',
  body: 'Have a great day!'
};

sendSMS(payload)
  .then(result => {
    console.log(result);
  })
  .catch(result => {
    console.log(result);
  });

The library we use for HTTP requests "got," returns a "Promise" once it's used. Therefore, we need to attach two handlers to it for then and catch cases. The "then" handler runs when the request succeeds while the "catch" handler runs if it fails.

Now that you're familiar with SMS in Apidaze, it's time to create your SMS function. Just as a reminder, we've recently revamped our SMS pricing:

  • $0.003/message to send
  • Free to receive
  • No carrier surcharges

Contact our team today to get set up!

Contact Us

(Photo by OOI JIET on Unsplash)

Tags: Apidaze, API, REST API, Programmable Telco, SMS

New Call-to-action

Subscribe to Email Updates

Recent Posts