Text directly to your Notion with the Notion API, Twilio, and Brev

Written by Nader Khalil
@brevdev

Build an SMS Bot to Send Text Messages To Your Notion in <10 minutes

Notion Labs has published their long awaited public beta API to allow custom use cases to be built right in your notes! Something that always prevented me from using Notion instead of Apple Notes on my phone was the time it took to put a note in the appropriate place. Ideas are fleeting, so when I have one, it’s crucial for me to just jot it down, and worry about the where later. Startups like Roam Research popped up for this exact reason. Before smartphones, I used to send text messages to myself in lieu of a native notes app. This tutorial will show you how to make an SMS bot that takes anything you send it directly to Notion!

Step 1/3: Set Up Twilio for SMS

  1. Create a Twilio account here.
  2. Claim a phone number at the twilio console by clicking the “Buy a number” button (it’s $1/mo but you should have ~$15 of credits in your account).

Twilio-Your-Numbers

Click on your phone number in the table and you should have a page with a messaging section.

Twilio-Your-Numbers

Hold on to this tab, we’ll come back to it when our backend is ready— we’ll paste our API url here.

Step 2/3: Setup Your Notion Bot

  1. Create an integration, which just means a way to programatically write to your Notion. Go to Notion integrations. Click ”+ new integration”. Give it a name and image. I’m naming it BrevBot and using this picture of Brev stacks. When you’re done, you should see this page:

Notion-Your-Integration

Hold onto this tab. We’ll need the internal integration token for our backend.

  1. In Notion, create a new table by typing “/table”, and select “Full Page”.

Notion-New-Table

  1. Click into your table and share it with your integration bot.

Notion-Share-BrevBot

Note:

Look at the URL for your Notion page. If you’re using the Notion native app, click share -> copy link to get the URL. It should look like this:

https://www.notion.so/9d8c4144c25242e781e33b8cf6ca9392?v=01d33cecdb65433bae788570faaf254e

Copy the page ID, which is everything after “notion.so/” and before the ”?“.

notion.so/9d8c4144c25242e781e33b8cf6ca9392?v=01d33cecdb65433bae788570faaf254e

You’ll need this for the backend shortly.

3. Final Step: Build the backend on Brev

Our last step is to build a backend API to tie it all together.

  1. Create a Brev account here or login here if you already have an account.
  2. Add secrets from Twilio and Notion Secrets are just passwords that let your API talk to Notion and Twilio. Brev has a built-in encrypted secrets manager. Open up the .env on the bottom action bar.

Brev-Encrypted-Secrets

In the Brev secrets manager:

  • Type TWILIO_ACCOUNT_SID for Var Name, and use the value from the twilio console
  • Type TWILIO_AUTH_TOKEN for Var Name, and also use the value from the twilio console

Twilio-Tokens

  • While you’re at it, also add a Brev variable called TWILIO_NUMBER, and set it equal to your Twilio phone number “+1415XXXXXXX” (no dashes or parenthesis needed).
  • Type NOTION_DB_ID for Var name, and for the value, use the notion page ID that you got from the URL in the step above: notion.so/9d8c4144c25242e781e33b8cf6ca9392?v=01d33cecdb65433bae788570faaf254e
  • Finally, type NOTION_TOKEN for Var name, and get the value from the integration page in Notion. Notion-Share-BrevBot

    1. Install the Twilio package using the package manager test
    2. Create a new endpoint by clicking the ”+ endpoint” button on the left in the Brev dashboard. You can name it anything. Click the new endpoint file to open it, and copy the following code over. Use the copy button or the code might not copy properly.
import variables import shared import requests from global_storage import storage_context from twilio.twiml.messaging_response import MessagingResponse from twilio.rest import Client from pydantic import BaseModel from fastapi import FastAPI, Response, Form def makeNotionRequest(message): data = { "parent": {"database_id": variables.NOTION_DB_ID}, "properties": { "Name": { "title": [ { "text": { "content": f"{message}" } } ] } } } headers = { "Authorization": f"Bearer {variables.NOTION_TOKEN}", "Content-Type": "application/json", "Notion-Version": "2021-05-13" } resp = requests.post('https://api.notion.com/v1/pages', json=data, headers=headers) return resp def post(From: str = Form(...), Body: str = Form(...), db=storage_context("notes")): client = Client(variables.TWILIO_ACCOUNT_SID, variables.TWILIO_AUTH_TOKEN) db['notes'].append(Body) resp = makeNotionRequest(Body) if (resp.status_code >= 200 or resp.status_code < 300): message = client.messages.create( from_=variables.TWILIO_NUMBER, body='Added to your notion ✅', to=From ) return {"note_submitted": "yes"} else: message = client.messages.create( from_=variables.TWILIO_NUMBER, body='An error occured with the Notion API. ❌', to=From ) return {"note_submitted": "no"}

Your code on Brev is instantly deployed. All that’s left is giving your API url to Twilio.

Above the code editor in Brev, you should see your API URL. Click to copy, and go back to the Twilio page from the first step.

Twilio-Your-Numbers

Under the messaging section, when “A MESSAGE COMES IN”, select webhook and paste your Brev API, and make sure HTTP POST is selected on the right dropdown. Make sure to save.

You’re done! Text your Twilio number and your message contents will appear in Notion. Congrats :)

Final Thoughts

Brev is a simple dev environment that let’s you code without worrying about infrastructure, environments, deploying, hosting, etc by automatically and instantly handling that for you.There’s a lot more you can do with it, including using the build in database to also persist your notes.

If you have any questions, text me at 415-818-0207.

Something not working?

If something isn’t working, you can go to https://www.twilio.com/console/sms/logs which will open up any errors from the Twilio side.