Node express query Notion database


Query a Notion database from a Node-express application

8 Sep, 2021 Β· 4 min read

This article will query the Notion database using a Node express server.

This is a series on building a Node powered Notion API:

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

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


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


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 defines all the variables we need:

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

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

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

const { getDatabase } = require('./modules/notion');

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 });

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

return => {
  return {
    tags: =>,

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 what 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');

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();

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

npm start

# Open http://localhost:8000

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

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next πŸ“–

Creating a Discord slash command bot

8 Jan, 2022 Β· 5 min read

Creating a Discord slash command bot

Building a Fastify NodeJS server

7 Jan, 2022 Β· 4 min read

Building a Fastify NodeJS server

Join 1509 devs and subscribe to my newsletter