Getting started with the Notion API
Tags: api, webdev
Published: 2021-09-06 06:34:40

This article will explore the Notion API, mainly getting an API key and query a database.

For this article, we shall work with a request testing tool.

## What is Notion

Notion is a component-based note-taking app. It offers databases, calendars, files, and much more.

I use Notion as the primary source for my blog articles, meaning this is where I keep track of what articles I'm writing and what status they have.

> You can read more on [my writing process](https://daily-dev-tips.com/posts/%F0%9F%A4%AB-the-secret-to-my-writing-process/) here

You might be wondering, ok, but why do we need an API connection? Let's go back to what Notion offers us, and that included databases, right?

Meaning Notion can be a super easy database source for any application we wish to make.

## Getting a Notion API key

Before we get started, you must be an admin of a workspace or create a new workspace yourself.

Once you established that, head over to [https://www.notion.com/my-integrations](https://www.notion.com/my-integrations) and click the "+ New integration" button.

![Notion new integration](https://cdn.hashnode.com/res/hashnode/image/upload/v1630390630241/nVhdBBEY6z.png)

Give this new integration a name. As you can see above, I called one "API". Also, select the workspace it's associated too.

On the last screen, you will get an "Internal Integration Token" this is what we will be using to query our Notion database.
So copy this to a safe location for now.

![Notion Internal Integration Token](https://cdn.hashnode.com/res/hashnode/image/upload/v1630390765751/v0JxPMIrf.png)

## Make database available for integration

By default, our databases won't be available for this integration. We have to give access to it explicitly.

<video autoplay loop muted playsinline>
  <source src="https://res.cloudinary.com/daily-dev-tips/video/upload/v1630391058/notion_rkir4n.webm" type="video/webm" />
  <source src="https://res.cloudinary.com/daily-dev-tips/video/upload/v1630391057/notion_bqkmnu.mp4" type="video/mp4" />
</video>

This will make the current page available for our integration.
The next thing we need is the ID of this database.

Press the three dots on your table and click the "Copy link" button.

![Notion copy link](https://cdn.hashnode.com/res/hashnode/image/upload/v1630391222337/RrFgvc3Mr.png)

This will give you a URL that will look similar to this:

```text
https://www.notion.so/6d6703dae1864e8ba381a9430447fc29?v=6b4520e686ef4b88a5f16b0ee54ffd5d
```

We need the part before the `?` so in our case: `6d6703dae1864e8ba381a9430447fc29`.

## Query the Notion database

By now, we have everything that we need to test out the Notion API.

- An Notion integration
- Notion API Key
- Database in Notion (Table)
- Integration has access to our table
- Table ID

Head over to your [favorite API Client](https://daily-dev-tips.com/posts/testing-api-calls-in-insomnia/).

First, let's query the database and get the structure of our table.

As the URL, use the following setup:

```text
https://api.notion.com/v1/databases/6d6703dae1864e8ba381a9430447fc29
```

Where you will need to change the ID to the table ID we just got.

Then head over to the Auth section and choose to use a Bearer Token.

![Notion bearer token](https://cdn.hashnode.com/res/hashnode/image/upload/v1630391586135/r_-InzMo8.png)

Then there is one last thing we need to add, and that is a Notion-Version header.
Head over to the header section of your API client and add one like this:

```text
Notion-Version: 2021-05-13
```

![Notion version header](https://cdn.hashnode.com/res/hashnode/image/upload/v1630391708571/HcZv3gNmu.png)

Now hit the send button and see what we'll get in return.

![Notion return value](https://cdn.hashnode.com/res/hashnode/image/upload/v1630391759438/RGyqE4NHe.png)

As you can see, this includes a database object and the fields that belong to it under the `properties` section.

In our case, there are two so far beings:

- Tags
- Name

Of course, we also want to query for the actual data.
For that, we need to make a `POST` request to the same URL, but append `/query` at the end like this:

```text
https://api.notion.com/v1/databases/6d6703dae1864e8ba381a9430447fc29/query
```

![Notion query results](https://cdn.hashnode.com/res/hashnode/image/upload/v1630391949207/m7GShebt7.png)

Here you can see we get multiple results, each containing numerous properties.
In the screenshot, you can see one of these properties having the same set of properties.

And that's as far as we go today. We can create a new Notion integration and query a database table in Notion.

We'll go on and create a real-world example in another article.

### 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](https://www.facebook.com/DailyDevTipsBlog) or [Twitter](https://twitter.com/DailyDevTips1)