Build a “Serverless” SMS Crypto Price Checker in 6 Minutes with MessageBird and StdLib

Jacob Lee
6 min readMay 1, 2018

Update: The MessageBird API on Standard Library has been deprecated. Your existing SMS hubs will continue to function, but we recommend using our utils.sms API instead.

Investing in cryptocurrency is not for the faint of heart. Prices swing wildly from day to day, or even minute to minute:

A kid asks his dad for $10 worth of Bitcoin. His dad responds: $8.32? What do you need $15.58 for?

How can anyone keep up? One solution, which I’ll show you how to build in this article, is to create an SMS bot that can look up prices via text! No data, no problem!

I’ll walk you through setting up MessageBird telephony on StdLib, claiming your first free phone number, and finally, creating and setting an SMS response handler to retrieve cryptocurrency data and text back a result. It’ll also be easy to tweak the end result to support additional functionality.

Example results from querying for cryptocurrency prices

If you’re not familiar with StdLib yet, you’re in for a treat! We’re a serverless API development and publishing platform that can help you build and ship custom logic in record time.

What You’ll Need Beforehand

  • 1x Command Line Terminal With Node.js Installed
  • 6x Minutes (or 360x Seconds)
  • A US Cell Phone Number
  • 1x Nerves of Steel

Minute 1: Initialize MessageBird on StdLib

The first step is to get started with MessageBird on StdLib by claiming your first phone number. We’re going to be using a service called messagebird.numbers to interface with MessageBird’s API.

Navigate to the messagebird.numbers service page on StdLib and accept the terms of service by clicking the link underneath the service description. You’ll be prompted to create an account or log in.

Accept the MessageBird terms of service to get started

After you’ve done that, you can claim your first number directly from the browser! Scroll down the page to the available method on the numbers API:

The “available” method in the messagebird.numbers API

Currently, only Canadian and US numbers are available — choose the appropriate country code based on your location. Select one of your StdLib tokens from the dropdown and press the “Run Function” button to see a list of available numbers.

Copy one from the list, then scroll down a bit farther to the initialize method.

The “initialize” method in the messagebird.numbers API

The initialize function will claim an initial phone number for personal use, and is free. Enter the number you copied above into the number parameter input, and press “Run Function” to claim your number.

Congratulations! You’ve successfully initialized MessageBird on StdLib! You can test this by sending a text message using the messagebird.sms service. Navigate to the API page and try the create function with your personal phone number as a recipient — you should receive a text message from the number you’ve just claimed!

The “create” method in the messagebird.sms API

Minute 2: Set Up the StdLib Command Line Tools

To create and manage your own StdLib services, you’ll have to install the StdLib CLI. Open up a terminal and run:

$ npm install lib.cli -g

Next, we’re going to create a workspace for your StdLib functions. Create a new folder and initialize it with the following commands:

$ mkdir stdlib-workspace
$ cd stdlib-workspace
$ lib init

Log in with the StdLib credentials you created earlier.

Minute 3: Fork the SMS Handler Sourcecode

Now that you’ve claimed your first MessageBird phone number and set up the CLI, we’ll walk you through the process of creating a handler that will respond to incoming SMS messages. From your main StdLib workspace in your terminal, fork the starter sourcecode by running:

$ lib create -s @messagebird/messagehub

You can name your service whatever you’d like — the remainder of the tutorial will assume you’ve called your service “messagebirdCryptoHandler”.

The code will appear in the folder <username>/messagebirdCryptoHandler. Run $ cd <username>/messagebirdCryptoHandler and run $ code . or something similar to open the directory in your favorite text editor.

Your handler service contains three endpoints within the functionsdirectory:

  1. __main__.js, the main endpoint of your handler. This function will receive events triggered by incoming SMS messages to your MessageBird number and will call other handlers appropriately
  2. messaging/more.js, a handler for incoming messages that contain the word “more” as their sole contents
  3. messaging/__notfound__.js, a “not found” handler. If the incoming message can not be mapped to a named function (like more), this handler will be triggered

For this application we will be modifying messaging/__notfound__.js.

Copy the code below and replace the contents of messaging/__notfound__.js

A quick explanation of how it works: MessageBird passes in four parameters to an SMS handler. These are the MessageBird number receiving the message, which will be the number you claimed earlier, the outside number that sent the incoming message, the contents of the message, and the time the message was sent.

The handler above takes the contents of the message and attempts to match it to info retrieved from jacoblee.cryptoInfo, a different published service on StdLib that wraps the CoinMarketCap API, using the name or symbol of the cryptocurrency. It then calls messagebird.sms.create to send a reply text with currency info back to the sender of the original message. You can make whatever changes you want though!

Minute 4: Setting Your StdLib Token

You may have noticed that the definition of lib in the above code contains a process.env.STDLIB_TOKEN environment variable. We’ll have to add this to our service’s env.json file.

First, fetch your StdLib Token from the StdLib Dashboard. You’ll be asked to log in again — once you’ve done so, click on Library Tokens on the left and you’ll see this page:

Click “Show Token” to see your library token

This is your StdLib auth token, click Show token to see its value and copy and paste it to your env.json file under STDLIB_TOKEN for "local" and "dev" environments.

Default contents of the env.json file

Minute 5: Deploying Your Handler

When you’re ready, run the following command from your service directory:

$ lib up dev

This will release your service to a mutable development environment. Your service will be available at https://<username>.api.stdlib.com/messagebirdCryptoHandler@dev, but you won’t need to call it directly.

Minute 6: Setting Your Handler

The final step is setting the service you just deployed as a handler. Run the following command from your command line:

$ lib messagebird.handlers.sms.set \
--number <your previously claimed phone number> \
--identifier <username>.messagebirdCryptoHandler[@dev]

You can see your claimed numbers by running $ lib messagebird.numbers.list if you need to see your claimed number again.

Then, simply send an SMS message matching either the name or symbol of a cryptocurrency (for example, “Dogecoin” or “ETH”) to your claimed number to see the current price!

And that’s it! You now have a personal SMS-based crypto price checker!

Customizing Your Handler

You can customize the handler to run any kind of code you’d like — text back responses from other APIs, use message contents to query a database, forward incoming messages to Slack using Slack’s API — the sky is the limit! Just install whatever npm packages you need and modify the functions/messaging/__notfound__.js handler with whatever logic you want.

Thanks for reading! For more content, features, and announcements, stay tuned by following us on Twitter @StdLibHQ or me @hacubu. You can also join our Slack channel by going to our homepage and requesting an invite under the “Community” tab. Reach out anytime.

I can’t wait to see what you build!

Jacob Lee is one of the co-founders of StdLib and firm believer in the FaaS future. He’s an east-coast transplant and ex-Googler who loves skiing, playing squash, and traveling. Follow him on Twitter @hacubu!

--

--

Jacob Lee

Co-founder at Standard Library (@StdLibHQ). Formerly engineer @Google Photos. Spent WAY too much time in front of a screen as a kid.