Send and Receive SMS Messages with Firebase Functions

Published January 24, 2020 by Kelly J Andrews

The Firebase platform allows developers to build an application backend rapidly. It’s also enjoyable to use as well. For this tutorial, I wanted to start using it for some SMS messaging with Nexmo. After this walk-through, you will be able to create an SMS message log, and a response to the sender using Firebase Functions and Firestore alongside the Nexmo SMS API.

Before You Get Started

You will need a few items to get going – so take a moment and make sure you have both of these ready to go.

  1. Firebase
  2. Nexmo

Setting up Firebase

The first step is to set up a Firebase project. The following will walk you through setting up a new project using the Firebase console.

Create the Firebase Project

  1. Go to Firebase console
  2. Click add project

    Click Add Project

  3. Add a name and click continue

    Name Project

  4. Leave Google Analytics on and click continue (not required)

  5. Select a Google Analytics account and then click Create Project (if added)

    Add analytics

  6. Wait a bit for the project to be created – takes less than a minute

  7. Set the Billing type under ⚙️ -> Usage and Billing -> Details & Settings to Blaze. The Pay-as-you-go plan is required to use a third-party API. For more details regarding billing with Google, go here.
    Change Billing
  8. Set the Google Cloud Platform (GCP) resource location in ⚙️ -> Project Settings
    Update Location

Install Firebase Tools

Most everything you will need to do with Firebase can be done directly from the command line with the toolset they provide.

  1. Install the Firebase tools with npm
    npm install -g firebase-tools
  2. Log in to Firebase using firebase login. The login process will open your browser for authentication.

Setup Local Environment

Writing Firebase functions requires some initialization work to get started, but it’s mostly done for you using Firebase Tools commands.

  1. Create a project folder mkdir nexmo-project && cd nexmo-project

  2. Initialize Firebase Functions firebase init functions

Since you already created a project in the dashboard, you can select Use an existing project which will prompt you to choose the desired project. If you haven’t done this, use Create a new project and give it a unique name to create one. You would still need to go to the console to update the location and billing, but it is another option to create Firebase projects.

  1. Select the project name you created
  2. Select JavaScript
  3. Choose Y for ESLint if you desire (I recommend it)
  4. Install all dependencies now

These steps will create the folders and files required to build Firebase Functions and installs all dependencies. Once NPM completes, switch to the functions directory and open index.js in your favorite editor to start adding code.

Create Your First Function

The first function you create will act as a webhook to capture and log incoming SMS messages from Nexmo.

The index.js file has some example code provided you won’t need. Delete everything and start at the top to add the following code.

Calling admin.initializeApp(); allows the functions to read and write to the Firebase Real-Time database. Next, use the following method to create your function.

The inboundSMS method listens for HTTPS requests – which is precisely what Nexmo webhook needs. The Firebase Function will capture the req.body and send it to the /msgq object in the Real-Time Database as a log.

Since we are using req.body, the webhook will need to be a POST Method. If you’d prefer to (or you have to) use the GET method for Nexmo webhooks, just replace it with req.query, and the GET method will work the same way.

Now that you have some code written be sure to save your file an deploy the function to Firebase:

The vital piece from the output is Function URL (inboundSMS). This URL is required to set up the webhook in Nexmo, which you will do next.

Setting up Nexmo

There are a few quick steps to setting up Nexmo – all done from the command line.

  1. Install the CLI
    npm install -g nexmo-cli
  2. Setup the CLI with your API key and secret from the dashboard
  3. Purchase a new phone number
    nexmo number:buy --country_code US

    1. This command retrieves the first available phone number in the US. For more info on Nexmo numbers, you can view the [numbers documentation](
    2. Type confirm, then copy the number
  4. Link the phone number to the webhook using this command

Give the process a few seconds to completely provision, then test out the new function to make sure the message is getting logged.

Grab your phone and send a message to the phone number. Open up the Firebase console and navigate to database page, and you should see something like this:

Real-time Database Nexmo Message Entry

Now that there is a way to log incoming messages, you can write a function to do something with the incoming message.

Create the Send Function

So far, you have created a Firebase Function linked to a Nexmo phone number for capturing inbound SMS messages. Firebase Functions can also react to database updates. Upon a new entry, the code sends an echo of the original text.

Start by adding Nexmo to the dependency list – make sure you do this in the functions directory:

Add the following environment variables to the Firebase config

Next, open index.js add nexmo to the requirements at the top, and import the environment variables to initialize Nexmo:

Now you can create the new function for Firebase to send the response:

The new function will watch for new messages added to the /msgq database object. When triggered, the full Nexmo object gets passed as message . This object includes msisdn, which is the originating phone number – yours in this case, and the to number, which is the Nexmo virtual number you purchased.

With the phone numbers in hand, as well as the text message, you can now do any number of things. You can create a lookup table to respond with specific data based on the keyword, forward to another system, or in our case, send the original message.

Deploy the Firebase Functions again from the command line:

Grab your phone, send another message, and then you should get a response back that looks something like You sent the following text: Test message.

Wrap Up

You have now completed all the steps for this tutorial. You can see the full code on Github.

Now that the initial steps to send and receive messages are complete, my next few posts will take this concept and expand it into controlling some of my home automation via text messages. I would love to hear what you plan to do as well so send me a message on Twitter and let me know.

Further Reading

Leave a Reply

Your email address will not be published.

Get the latest posts from Nexmo’s next-generation communications blog delivered to your inbox.

By signing up to our communications blog, you accept our privacy policy , which sets out how we use your data and the rights you have in respect of your data. You can opt out of receiving our updates by clicking the unsubscribe link in the email or by emailing us at