How to build an API for sending SMS using your Phone (Node.js and Flutter)

How to build an API for sending SMS using your Phone (Node.js and Flutter)

Simple SMS API Gateway service using Node.js and Flutter

Hello Gang,

As a developer, we come across situations where we would need to send SMS to users via website or apps. For example, an app can have OTP based feature or any other SMS service. We often tend to use third party messaging services like Twilio, BulkSMS etc. Using these service is a quick and easy solution but for developers this is not a budget friendly option.

Wouldn’t it be amazing if we could have an API hosted on our server which sends SMS via our phone?

YESSSSSS !!! You read it right !

Today we will be building an API to send SMS from our phone sim card.

Technologies used:

  • Node.js
  • Flutter
  • Firebase

We will be targeting 3 main Tech stacks/Technologies here:

  1. Node Js for exposing REST API.
  2. Flutter for redirecting notification to SMS.
  3. Firebase for sending push notifications.

Note: We will not be getting into in-depth explanation here as this is not the scope of this tutorial but will be attaching the link for source codes below for reference.

So let us get this implemented...

We will have this implemented in 3 steps

1. Create a firebase project using firebase console

Here we use firebase for sending push notifications to respective devices.

Firebase Console

Create an Android app in the same firebase project, (For iOS app configuration is almost similar except that you may have to upload your APNS key.)

If you see the below screenshot you will see a server key will be generated. Copy it , We will have this used later.

Screenshot 2021-07-21 at 8.33.27 AM.jpg

2. Create a new Flutter project

The flutter app is used to read the message in the push notification received and forward the same message as SMS to the phone number in the notification.

Add following flutter packages to your pubspec.yaml file

  • firebase_core:
  • firebase_messaging:
  • background_sms:

Replace main.dart and home_page_widget.dart with code from attached reference.

FirebaseMessaging.onBackgroundMessage is responsible for receiving push notifications when app is in background.

FirebaseMessaging.onMessage is responsible for receiving push notifications when app is in foreground.

BackgroundSms.sendMessage forwards the message received from push notification to respective Phone Number received in the push notification body.

FirebaseMessaging.instance.getToken() Generates a unique token , Have this printed and copied, We will use this later.

We are almost there

3. Create a Node.js app (using npm init)

The sole purpose of node app is to create an API for reading the Phone Number and message and forward the message as Push Notification via FCM.

Add the following dependencies in your package.json

  • body-parser
  • cors
  • express
  • fcm-node

Copy app.js and sms_route.js to your node project folder.

In the sms_route.js file add the serverKey and token which we copied above from firebase and flutter app respectively.

The following params used to place your message and receiver’s Phone Number. The parameters 'message' and 'phone' should not be altered as the flutter app parses them.

 data: {  
            'message': message,
            'phone': phone
        }

We use fcm.send to send push notification to Android app. FCM takes cares of send ing them to the right device (This is where the token plays its role).

Now host this Node.js app on your server or try running it locally.

You can test this using following URL pattern.

https://your-server-url/send-sms?phone=XXX-yyy-77&message=message

There you go.

We have successfully implemented our own sms sending API.

Source Code

Firebase app creation

I may make mistakes too and I would love to learn from you , Beat me up in the comments section below.

Thanks for reading...