DEV Community

Cover image for Node express query Notion database
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Node express query Notion database

In this article, we'll query the Notion database using a Node express server.

For this tutorial, you'll need the following two tutorials as a basis:

  1. Getting started with the Notion API
  2. Configure Tailwind JIT for a node express app

This third article will combine these two and create a server that will query the API for us.

Creating the express server to query the Notion API

If you want to work with what we have got so far, head over to GitHub and clone this repo.

First of all, open your terminal and add the Notion package with this command.

npm i @notionhq/client dotenv
Enter fullscreen mode Exit fullscreen mode

We also add the dotenv package so we can keep our secrets there.
Quickly head over to your gitignore and add the .env file.

.env
node_modules
Enter fullscreen mode Exit fullscreen mode

Now create this .env file in the root of your directory.

NOTION_API_KEY= YOUR KEY HERE
NOTION_API_DATABASE= DATABASE ID
Enter fullscreen mode Exit fullscreen mode

Then we can create a modules folder, and inside, let's create a file called notion.js.

This file will keep the logic for the Notion connection.

The first thing we need to do in this file is define all the variables we need:

require('dotenv').config();
const {Client} = require('@notionhq/client');
const notion = new Client({auth: process.env.NOTION_API_KEY});
const databaseId = process.env.NOTION_API_DATABASE;
Enter fullscreen mode Exit fullscreen mode

As you can see, we load the env, define a new notion client, and define our database ID.

I then chose to create an export since we will be using the functions from another file.

module.exports = {
  getDatabase: async () => {
    // Function code
  },
};
Enter fullscreen mode Exit fullscreen mode

This allows us to load this function in another file like such:

const {getDatabase} = require('./modules/notion');
Enter fullscreen mode Exit fullscreen mode

Anyway, let's not get ahead of ourselves and create this function first.

Inside this function we want to query the notion database, this JavaScript SDK has a built-in function for that:

const response = await notion.databases.query({database_id: databaseId});
Enter fullscreen mode Exit fullscreen mode

This will already give us the complete object, as we saw in our postman example.
However, we want to map it into more useable data.

return response.results.map((page) => {
  return {
    id: page.id,
    name: page.properties.Name.title[0]?.plain_text,
    tags: page.properties.Tags.multi_select.map((tag) => tag.name),
    watched: page.properties.Watched.checkbox,
    banner: page.properties.Banner.files[0].external.url,
  };
});
Enter fullscreen mode Exit fullscreen mode

Let's see what happens for each element.

  • id: Returns the unique ID for this element
  • name: We return the plain text version for the first title we find
  • tags: We map an array of tag names, as the name is the only element we need.
  • watched: This is a checkbox in Notion so it returns true or false
  • banner: Returns external image URLs

If you are curious to see how this Notion data looks like, here is the public Notion document for this Movie setup.

Calling our Notion express endpoint from our server

Now that we created this function, we need some way to call it from our server.

Head over to your server.js file and add the function:

const {getDatabase} = require('./modules/notion');
Enter fullscreen mode Exit fullscreen mode

Now, let's define a route to get all entries in our database. This route will be available on the /movies endpoint.

app.get('/movies', async (req, res) => {
  const movies = await getDatabase();
  res.json(movies);
});
Enter fullscreen mode Exit fullscreen mode

Then let's run our server and see if we can open up this endpoint.

npm start

# Open http://localhost:8000
Enter fullscreen mode Exit fullscreen mode

This will show us a JSON result:

Express JSON result Notion API

And there you go, we now created a middleware notion function to retrieve all our movies from the Notion database.
And we created a public endpoint to retrieve these results.

Keep an eye out for the next article where we return this data to our front end.

You can find today's complete code on GitHub.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Latest comments (0)