Send SMS Messages with Cloud Functions For Firebase Gen 2
Tags: firebase, javascript
Published: 2024-04-11 09:53:37

## Overview

W﻿e have a tutorial on [How to Send and Receive SMS With Firebase Functions](https://developer.vonage.com/en/blog/send-and-receive-sms-messages-with-firebase-functions-dr); this blog post tackles the same use case. However, we will create and deploy a second-generation HTTP function using the Google Cloud console and the [Vonage Messages API](https://developer.vonage.com/en/messages/overview?source=messages) instead of the [SMS API](https://developer.vonage.com/en/messaging/sms/overview).

## Setup

You will need a few items to get going:

* [Firebase project](https://console.firebase.google.com)
* [Vonage API credentials](https://dashboard.nexmo.com)

### Firebase Setup

Create a Firebase project in the [Firebase console](https://console.firebase.google.com).

Give your project a name that will generate a project ID that you will later choose from the terminal/ command prompt.


![project creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvgmxo2rz0s3eu25hqqi.jpg)

If you use Google Analytics, I will not do this project.


![Google Analytics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28p12xnuizmwozjuyafp.jpg)

![Wait for project creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whp5t7ndl98dpqbaf45m.jpg)

> Note: Remember to choose the pay-as-you-go billing plan; otherwise, you might get an error similar to "Error: Your project <project_name> must be on the Blaze (pay-as-you-go) plan to complete this command. Required API artifactregistry.googleapis.com can't be enabled until the upgrade is complete."

Install the Firebase CLI from the command prompt/ terminal:

```bash
npm install -g firebase-tools
```

Login and initialize your project from the command prompt/ terminal:

```bash
firebase login
firebase init
```

You will be prompted to select diverse options, as seen below.

**? Which Firebase features do you want to set up for this directory? Press Space to select features, then Enter to confirm your choices.** Realtime Database: Configure a security rules file for Realtime Database and (optionally) provision default instance, Functions: Configure a Cloud Functions directory and its files

**? Please select an option: Use an existing project ? Select a default Firebase project for this directory:** vonage-sms-project (vonage-sms-project) (Select here the name of the project you have created. In my case, it is vonage-sms-project (vonage-sms-project)

**? It seems like you haven’t initialized Realtime Database in your project yet. Do you want to set it up? (Y/n)** Choose Y

**? Please choose the location for your default Realtime Database instance:** 

**us-central1** 

**❯ europe-west1** 

**asia-southeast1** 

I﻿n my case Europe-west1 is the closest to me; choose the one that makes more sense to you.  

**? What file should be used for Realtime Database Security Rules?** (database.rules.json)

**? What language would you like to use to write Cloud Functions? (Use arrow keys)** 

❯ JavaScript  (Choose JavaScript)

TypeScript 

Python

**? Do you want to use ESLint to catch probable bugs and enforce style?** (y/N)

**? Do you want to install dependencies with npm now? (Y/n)**  ChooseYes. And wait for the dependencies to be installed.

This will set up the required folders and files to get started.

### Enable Google Cloud APIs

From the Google Cloud Console, [enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs](https://console.cloud.google.com/flows/enableapi?apiid=cloudfunctions,cloudbuild.googleapis.com,artifactregistry.googleapis.com,run.googleapis.com,logging.googleapis.com,pubsub.googleapis.com&redirect=https://cloud.google.com/functions/docs/console-quickstart).

## First Function: InboundSMS

I’ll show you how to create and deploy the function from the IDE/ code editor. Alternatively, you can [create functions from the Google Cloud Functions dashboard](https://cloud.google.com/functions/docs/console-quickstart#create_a_functionahttps://cloud.google.com/functions/docs/console-quickstart#create_a_function).

Open [functions/index.js](https://github.com/Vonage-Community/blog-sms_api-firebase_functions/blob/master/functions/index_second_generation.js), delete all the code you can see and add the following code to capture incoming SMS messages and log them to the Firebase Realtime Database.

```javascript
const { onRequest } = require("firebase-functions/v2/https");

const admin = require("firebase-admin");

admin.initializeApp();

exports.inboundSMS = onRequest(async (request, response) => {
  params = request.body;
  await admin.database().ref("/msgq").push(params);
  response.sendStatus(200);
});
```

Deploy the function from the functions folder.

```bash
firebase deploy --only functions
```

After the function is deployed, you will be given an HTTP URL needed to add to our webhook in the Vonage setup in the next session. It should look like something along the lines of <https://us-central1-vonage-project.cloudfunctions.net/inboundSMS>; or <https://inboundsms-njjebckulq-uc.a.run.app>. 

> Note: Note it because we will need it soon. In case you didn’t make a note of it, you can go to the [Google Cloud Functions Overview page](https://console.cloud.google.com/functions/list), click on the inboundSMS function, and you can find the URL right at the top.


![You can find the URL right at the top](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmqbxe4v77q9bee2cc8d.jpg)

## Vonage Setup

<sign-up number></sign-up>

From the Vonage Dashboard, set the webhook URL for your number to the function endpoint URL from deployment. It should look like something along the lines of <https://us-central1-vonage-project.cloudfunctions.net/inboundSMS> or <https://inboundsms-njjebckulq-uc.a.run.app>.


![Inbound SMS URL added to inbound and status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2exjfsojw34y5gz94bp.jpg)

Install the [Vonage Server SDK for Node.js (@vonage/server-sdk)](https://github.com/Vonage/vonage-node-sdk).

```bash
npm install @vonage/server-sdk
```

Next, add dotenv to the dependency list.

```
npm install dotenv --save
```

Add a file named `.env` with your Vonage credentials, which can be found in the [Vonage Dashboard](https://dashboard.nexmo.com), and add the following environment variables:


![Vonage Dashboard, red arrows showing where to find the API Key and the API Secret](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omcpdqf9inxxlurwlwpf.jpg)

```bash
VONAGE_API_KEY=
VONAGE_API_SECRET=
VONAGE_APPLICATION_ID=
VONAGE_PRIVATE_KEY=
```

A﻿dd Vonage to your `functions/index.js` file.

```j﻿avascript
const { Vonage } = require("@vonage/server-sdk");

const vonage = new Vonage({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET,
  applicationId: VONAGE_APPLICATION_ID,
  privateKey: VONAGE_PRIVATE_KEY
});
```

Make sure there's a phone number linked to this application.

Send an SMS to the virtual phone number you purchased and linked to this application. 

If you head for your Firebase Realtime database on the dashboard, you can see the `msgq` node has been updated with the message you sent from your phone.

## Second Function: SendSMS

Add the send function that will allow us to send the [SMS Message Using the Vonage Messages API](https://developer.vonage.com/en/tutorials/send-sms-with-messages/send-sms?lang=javascript) that will be logged in the Firebase Realtime database.

```javascript
const { SMS } = require("@vonage/messages");

const { onValueCreated } = require("firebase-functions/v2/database"); 

exports.sendSMS = onValueCreated("/msgq/{pushId}", async (message) => {
  const { from, text } = message.data.val();

  vonage.messages
    .send(
      new SMS({
        text: text,
        to: from,
        from: "Vonage APIs",
      })
    )
    .then((resp) => console.log(resp))
    .catch((err) => console.error(err));
});
```

Deploy the functions again from the functions folder.

```bash
firebase deploy --only functions
```

Send an SMS to the virtual phone number you purchased and linked to this application. 

If you head for your Firebase Realtime database on the dashboard, you can see the `msgq` node has been updated with the message you sent from your phone.

When a new message is added to `/msgq`, this function will be triggered. It will use the Vonage Messages API to respond to the user. The messages sent and submitted will be logged in our Firebase Realtime database.

> Note: If you get an error message “Error: secretOrPrivateKey must be an asymmetric key when using ES256” make sure your private key is valid and correct and being imported correctly.

## Test it Out

After it is deployed, test it out by sending an SMS message to the number linked to the Vonage application, and you will see the message added to the Firebase Realtime database.


![Realtime database containing three nodes under msgq](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/776dwr55te7uzpw03x7s.jpg)

## C﻿onclusion

Now, you can send and receive SMS messages using Firebase Cloud Functions Gen 2 and the Vonage Messages API! You have now completed all the steps for this tutorial. [You can see the full code on GitHub](https://github.com/Vonage-Community/blog-sms_api-firebase_functions/blob/master/functions/index_second_generation.js).

Do you have any questions about this blog post? Join our [Vonage Community Slack](https://developer.vonage.com/en/community/slack) or [message us on X](https://www.twitter.com/VonageDev).