Parse Airtable with SMS for Climate Non-Profits with JavaScript and Twilio Serverless
Tags: serverless, airtable, node, twilio
Published: 2022-04-21 17:52:17

---
title: Parse Airtable with SMS for Climate Non-Profits with JavaScript and Twilio Serverless
published: true
description: Learn how to build a serverless JavaScript app to parse an Airtable of eco-friendly non-profits, and see what they are working on via SMS.
tags: serverless, airtable, node, twilio
cover_image: https://twilio-cms-prod.s3.amazonaws.com/images/fS2ewrvZcihbdHa1b_l7Tyjm7i4hqhKcmToSJcUy0hX_w.width-1616.png
---
This blog post was written for [Twilio](https://www.twilio.com/) and [originally published on the Twilio blog](https://www.twilio.com/blog/earth-month-airtable-js-serverless).

April is Earth Month and has Earth Day coming up! My teammate [Michelle Glauser](twitter.com/michelleglauser) and I were talking about Earth-related organizations and how hard it is to find some to support or volunteer with. To fix this problem, read on to learn how to build a serverless JavaScript app to parse an Airtable of eco-friendly non-profits, and see what they are working on via SMS.
![example SMS](https://twilio-cms-prod.s3.amazonaws.com/images/cCu1ZLORd8CxuaSccyVuGNYtYsSGQHfY3HTxoCfZOkzdZ.width-1000.png)
### Prerequisites
- A Twilio account - [sign up for a free one here and receive an extra $10 if you upgrade through this link](http://www.twilio.com/referral/iHsJ5D)
- Node.js installed - [download it here](https://nodejs.org/en/download/)
- A Twilio number with SMS capabilities ([this quickstart will help you get going](https://www.twilio.com/docs/conversations/quickstart#sign-up-for-twilio-and-provision-your-first-sms-enabled-number))
- An [Airtable account](https://airtable.com/)
You’re going to need your <strong>Airtable API key</strong> and a <strong>base ID</strong> to access the base from your app. After logging in to Airtable, get the API key from your [account page](https://airtable.com/account)–keep this a secret! While logged in, make a copy of [this project's Airtable base](https://airtable.com/invite/l?inviteId=inv016fVue6nLssoQ&inviteToken=9ff959fd77cd32ecc337d07521bdd02264e5ffc82ca3a23a86d8227edc8fab4e&utm_source=email) to add it to your Airtable account.
### Setup your Airtable
You can then get the base ID from the [auto-generated API documentation](https://airtable.com/api), which will now show the Airtable x Twilio blog base). You can retrieve the base ID from either the URL or the <em>introduction</em> section.
![docs](https://twilio-cms-prod.s3.amazonaws.com/images/KMQLSi_Ezz_cUp-Dyl4FBHNmNo422x6VWwM5eooC9T4uA.width-1000.png)
In the copied Airtable base, select the <em>climate orgs</em> tab. It contains an <em>org</em> column containing non-profit organization names you could and should support. It also includes their corresponding <em>overview</em>, <em>website</em>, and <em>category</em> columns. The possible categories include <em>climate</em>, <em>environment</em>, <em>water</em>, <em>food+agriculture</em>, and <em>transportation</em>.
### Get Started with the Twilio Serverless Toolkit
The Serverless Toolkit is CLI tooling that helps you develop locally and deploy to [Twilio Runtime](https://www.twilio.com/docs/runtime). The best way to work with the Serverless Toolkit is through the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart). If you don't have the Twilio CLI installed yet, run the following commands on the command line to install it and the Serverless Toolkit:
```bash
npm install twilio-cli -g
twilio login
twilio plugins:install @twilio-labs/plugin-serverless
```
Create your new project and install our lone requirement `airtable`, an Airtable client library for Node.js, by running:
```bash
twilio serverless:init ecofriendly-orgs-airtable-sms
cd ecofriendly-orgs-airtable-sms
npm install airtable
```
Open the `.env` file and add an environment variable for your Airtable API key from the Airtable page you kept open with your key as well as your Airtable Base ID! In this blog post, the API key is called <em>AIRTABLE_API_KEY</em> and the Base ID is called <em>AIRTABLE_BASE_ID</em>.
```
AIRTABLE_API_KEY=xxxx...
AIRTABLE_BASE_ID=xxxx...
```
### Make a Twilio Function with JavaScript
`cd` into the `/functions` directory and make a new file called `sms.js` containing the following code:
```javascript
const airtable = require("airtable");
exports.handler = function (context, event, callback) {
  const base = new airtable({
   apiKey: context.AIRTABLE_API_KEY,
 }).base(context.AIRTABLE_BASE_ID);
 const twiml = new Twilio.twiml.MessagingResponse();
 const category = event.Body.toLowerCase().trim();
 let randArr = [];
 let randRecord;
 return base("climate orgs")
 .select()
 .all()
 .then((records) => {
   records.forEach((record) => {
     if (category.includes(String(record.get("category")))) { 
       randArr.push(record);
      } //if
    }); //records.forEach
    if(randArr.length > 0) {
      let randNum = Math.random()*randArr.length;
      randRecord = randArr[Math.floor(randNum)];
      twiml.message(`A random ${category} org. is ${randRecord.get("org")}. \n\n${randRecord.get("overview")} More at ${randRecord.get("website")}. \n\nThese are the categories you can text📲 to discover non-profits about: energy⚡️, transportation🚴🏻‍♀️, water💧, food+agriculture🐷.`);
      callback(null, twiml);
    }
    twiml.message(`Send one of these categories : energy, environment, climate, transportation, water, food+agriculture.`);
    callback(null, twiml);
   });
};
```
This code imports the Airtable API, makes a [Twilio Messaging Response object](https://www.twilio.com/docs/sms/api/message-resource), creates a new Airtable object with our API key and base ID, loops through the records in our base under <em>climate orgs</em>, checks that the inbound message is in the Airtable base's <em>categories</em> column, and if it is, returns a random record corresponding to that category so the user can learn about a non-profit related to it.

You can [view the complete app on GitHub here](https://github.com/elizabethsiegle/ecofriendly-startups-airtable-sms).
### Configure the Function with a Twilio Phone Number
To open up our app to the web with a public-facing URL, go back to the <em>ecofriendly-orgs-airtable-sms</em> root directory and run `twilio serverless:deploy`. Once you see a Function URL ending in `/sms`, go to the [phone numbers section of your Twilio Console](https://www.twilio.com/console/phone-numbers/incoming) and select the Twilio number you purchased and scroll down to the <em>Messaging</em> section. Under <em>A MESSAGE COMES IN</em>, change <em>Webhook</em> to <em>Function</em> and then under <em>Service</em> select <em>ecofriendly-orgs-airtable-sms</em>. For <em>Environment</em> select <em>dev-environment</em>, and then for <em>Function Path</em> select <em>/sms</em>. Click the <strong>Save</strong> button below and tada! You can now text your Twilio number a category and receive an eco-related non-profit organization relating to that category.
![sms example](https://twilio-cms-prod.s3.amazonaws.com/images/cCu1ZLORd8CxuaSccyVuGNYtYsSGQHfY3HTxoCfZOkzdZ.width-1000.png)
### What's Next for the Planet and Twilio Serverless?
How are you celebrating Earth Month this April? How are you fighting climate change or making water clean or helping the planet? I plan on walking, taking public transit, and trying to do my part not just this month, but every month.

Twilio's Serverless Toolkit makes it possible to deploy web apps quickly, Twilio Runtime seamlessly handles servers for you, and Airtable makes storing and accessing data easy.

Let me know online what you're building with Serverless and/or Airtable!
- Twitter: [@lizziepika](twitter.com/lizziepika)
- GitHub: [elizabethsiegle](github.com/elizabethsiegle)
- Email: lsiegle@twilio.com
- Livestreams: [twitch.tv/lizziepikachu](twitch.tv/lizziepikachu)