Back to APIs

Create, update and delete pages and content in your team's workspace.

Using the Notion API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Notion background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Example code using Notion

Below is a working code example of how you can use Notion with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
2
import { Client } from "@notionhq/client";
3
import z from "zod";
4
5
// API Reference: https://developers.notion.com/reference/intro
6
// Create a new integration and access your token at https://www.notion.so/my-integrations
7
// The integration needs to be added as a 'connection' to the page
8
// Here is how you do it:
9
// 1. Click on three dots at the top right corner of your parent notion page
10
// 2. Click on 'Add connection' and choose your integration and confirm
11
// 3. Now it will have access to page and all the child pages too
12
13
const notion = new Client({
14
auth: process.env.NOTION_TOKEN!,
15
});
16
17
client.defineJob({
18
id: "notion-get-page",
19
name: "Notion Get Page",
20
version: "1.0.0",
21
trigger: eventTrigger({
22
name: "notion-get-page",
23
schema: z.object({
24
pageId: z.string(), // The ID of the Notion page
25
}),
26
}),
27
run: async (payload, io, ctx) => {
28
const page = await io.runTask(
29
"Get Notion page",
30
async () => {
31
// Retrieve the Notion page
32
return notion.pages.retrieve({ page_id: payload.pageId });
33
},
34
35
// Add metadata to improve how the task displays in the logs
36
{ name: "Get page", icon: "notion" }
37
);
38
39
return page;
40
},
41
});