Serverless SMS Messaging using JavaScript and Twilio Functions
Time to read: 2 minutes
Function As A Service is a solution that allows developers to develop, deploy, and run event-driven functions in the cloud that are fully managed. Without the need for maintaining infrastructure, "serverless" architecture is achieved. In this tutorial, you will learn how to send Twilio Programmable SMS using the new Twilio CLI.
Project Setup
You will need the following items in order to get started:
Getting Started
First, install the Twilio CLI. I'm currently using version 1.8.1. Run the following command in your preferred terminal if on a Mac:
If you are on a different platform, you can find the setup instructions here.
To confirm a successful installation of the CLI has occurred, type twilio
in a new terminal session and the output below should appear.
Login to your Twilio account by running twilio login
in your terminal. You will be prompted for your Twilio Account SID and Auth Token which you can access in your Twilio Console. As seen in the image above, the serverless commands for Twilio functions are disabled by default. To install the serverless plugin run the command below:
Now create a folder where all your project files will be stored.
Configuring Twilio to Send SMS From our Function
After creating a free account on Twilio and purchasing a trial number with SMS capabilities, you will need to double-check if permissions are allowed to send SMS to your country region. If not, navigate to the permission page here and enable it.
Creating our Server Function
Inside your directory, create the serverless project by running twilio serverless:init greetings
. This command creates a project directory and installs all the necessary dependencies. Next, run cd greetings
and open the project in your favorite editor.
The assets
folder holds static files in your project and the functions
folder holds your functions as the name suggests. Delete the functions
folder contents then add your custom file using the following commands.
Run rm -rf functions/*
. Then run cd functions && touch greetings.js
in your terminal.
Navigate to the .env
file and add the following keys if they’re not already present:
Next, inside the greetings.js
file add the following lines of code:
Note: Change the to and from keys in the code to your phone number and your newly purchased Twilio number respectively.
The above piece of code exports a function that when an event is triggered, sends an SMS to the specified recipient. Before you deploy your function, try sending an SMS locally first. Check the “Enable ACCOUNT_SID and AUTH_TOKEN” option in your Functions Configuration so that Twilio can include your account credentials on every function call.
Run twilio serverless:start
, copy the greetings functions URL which should be http://localhost:3000/greetings
and paste it in a new tab in your browser. You should have received a text on your phone.
To deploy your function, run twilio serverless:deploy
. After a successful deployment, you should expect to see a similar screen once you navigate to the functions console.
On your terminal, copy the URL under Functions
. In my case, the URL is https://greetings-1921-dev.twil.io/greetings
. Paste it in your browser and an SMS should be sent to the specified number.
Conclusion
In this tutorial, we have learned how to send an Outbound SMS using Twilio functions. You could extend this application by generating custom responses based on the incoming message.
Happy hacking!
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.