Skip to main content

notion.notion

Home > @runlightyear/notion > Notion

Notion class

Connector to the Notion API

Signature:
declare class Notion extends RestConnector 

Extends:

RestConnector

Example 1

Create a database

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "createDatabase",
title: "Create Database",
apps: ["notion"],
variables: ["parentPageId"],
run: async ({ auths, variables }) => {
const notion = new Notion({
auth: auths.notion,
});
const response = await notion.createDatabase({
parent: {
pageId: variables.parentPageId!,
},
title: [
{
text: {
content: "Grocery List",
},
},
],
properties: {
Name: {
title: {},
},
Description: {
richText: {},
},
"In Stock": {
checkbox: {},
},
"Food Group": {
select: {
options: [
{
name: "🥦 Vegetable",
color: "green",
},
{
name: "🍎 Fruit",
color: "red",
},
{
name: "🍞 Carbs",
color: "yellow",
},
],
},
},
Price: {
number: {
format: "dollar",
},
},
"Last Ordered": {
date: {},
},
"Store Availability": {
multiSelect: {
options: [
{
name: "Duc Loi Market",
color: "blue",
},
{
name: "Rainbow Grocery",
color: "gray",
},
{
name: "Nijiya Market",
color: "purple",
},
{
name: "Gus's Community Market",
color: "yellow",
},
],
},
},
"+1": {
people: {},
},
Photo: {
files: {},
},
},
});
console.log("Database: ", response.data);
},
});

Example 2

Create a database with items

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "createDatabaseItems",
title: "Create Database Items",
apps: ["notion"],
variables: ["parentPageId"],
run: async ({ auths, variables }) => {
const notion = new Notion({ auth: auths.notion });
const response = await notion.createDatabase({
parent: {
pageId: variables.parentPageId!,
},
title: [
{
text: {
content: "Shopping List",
},
},
],
properties: {
Name: {
title: {},
},
Description: {
richText: {},
},
Quantity: {
number: {
format: "number",
},
},
},
});

const newDatabaseId = response.data.id;

await notion.createPage({
parent: {
databaseId: newDatabaseId,
},
properties: {
Name: {
title: [
{
text: {
content: "🥦 Broccoli",
},
},
],
},
Description: {
richText: [
{
text: {
content: "A green vegetable",
},
},
],
},
Quantity: {
number: 1,
},
},
});

await notion.createPage({
parent: {
databaseId: newDatabaseId,
},
properties: {
Name: {
title: [
{
text: {
content: "🍎 Apple",
},
},
],
},
Description: {
richText: [
{
text: {
content: "A red fruit",
},
},
],
},
Quantity: {
number: 2,
},
},
});

await notion.createPage({
parent: {
databaseId: newDatabaseId,
},
properties: {
Name: {
title: [
{
text: {
content: "🍞 Bread",
},
},
],
},
Description: {
richText: [
{
text: {
content: "A yellow carb",
},
},
],
},
Quantity: {
number: 3,
},
},
});
},
});

Example 3

Query a database

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "queryDatabase",
title: "Query Database",
apps: ["notion"],
variables: ["databaseId"],
run: async ({ auths, variables }) => {
const notion = new Notion({
auth: auths.notion,
});
const response = await notion.queryDatabase({
databaseId: variables.databaseId!,
});
console.log("Query result: ", response.data);
},
});

Example 4

Query a database with filter

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "queryDatabaseWithFilter",
title: "Query Database With Filter",
apps: ["notion"],
variables: ["databaseId"],
run: async ({ auths, variables }) => {
const notion = new Notion({
auth: auths.notion,
});
const response = await notion.queryDatabase({
databaseId: variables.databaseId!,
filter: {
property: "Name",
richText: {
isNotEmpty: true,
},
},
});
console.log("Result: ", response.data);
},
});

Example 5

Update a database item

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "updateDatabaseItem",
title: "Update Database Item",
apps: ["notion"],
variables: ["databaseItemId"],
run: async ({ auths, variables }) => {
const notion = new Notion({ auth: auths.notion });
const response = await notion.updatePageProperties({
pageId: variables.databaseItemId!,
properties: {
Name: {
title: [
{
text: {
content: "Updated name",
},
},
],
},
},
});
console.log("Updated database item:", response.data);
},
});

Example 6

Retrieve a database

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "retrieveDatabase",
title: "Retrieve Database",
apps: ["notion"],
variables: ["databaseId"],
run: async ({ auths, variables }) => {
const notion = new Notion({
auth: auths.notion,
});
const response = await notion.retrieveDatabase({
databaseId: variables.databaseId!,
});
console.log("Result: ", response.data);
},
});

Example 7

Create a page

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "createPage",
title: "Create Page",
apps: ["notion"],
variables: ["existingPageId"],
run: async ({ auths, variables }) => {
const notion = new Notion({ auth: auths.notion });

const response = await notion.createPage({
parent: {
pageId: variables.existingPageId!,
},
properties: {
title: {
title: [
{
text: {
content: "Hello World",
},
},
],
},
},
});
console.log("Created page", response.data);
},
});

Example 8

Create a page with children

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "createPageWithChildren",
title: "Create Page With Children",
apps: ["notion"],
variables: ["existingPageId"],
run: async ({ auths, variables }) => {
const notion = new Notion({ auth: auths.notion });

const response = await notion.createPage({
parent: {
pageId: variables.existingPageId!,
},
properties: {
title: {
title: [
{
text: {
content: "Hello World, I have children!",
},
},
],
},
},
children: [
{
heading2: {
richText: [
{
text: {
content: "This is a heading",
},
},
],
},
},
{
paragraph: {
richText: [
{
text: {
content: "This is a paragraph I just wrote",
},
},
],
},
},
],
});
console.log("Created page", response.data);
},
});

Example 9

Retrieve a page

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "retrievePage",
title: "Retrieve Page",
apps: ["notion"],
variables: ["pageId"],
run: async ({ auths, variables }) => {
const notion = new Notion({ auth: auths.notion });
const response = await notion.retrievePage({
pageId: variables.pageId!,
});
console.log("Page:", response.data);
},
});

Example 10

Retrieve a block's children

import { defineAction } from "@runlightyear/lightyear";
import { Notion } from "@runlightyear/notion";

defineAction({
name: "retrieveBlockChildren",
title: "Retrieve Block Children",
apps: ["notion"],
variables: ["blockId"],
run: async ({ auths, variables }) => {
const notion = new Notion({
auth: auths.notion,
});
const response = await notion.retrieveBlockChildren({
blockId: variables.blockId!,
});
console.log("Block children:", response.data);
},
});

Constructors

ConstructorModifiersDescription
(constructor)(props)Constructs a new instance of the Notion class

Properties

PropertyModifiersTypeDescription
authTypestaticAuthType
OAuthstatictypeof NotionOAuth

Blocks Methods

MethodModifiersDescription
retrieveBlockChildren(props)Retrieve block children

Databases Methods

MethodModifiersDescription
createDatabase(props)Create a database
queryDatabase(props)Query a database
retrieveDatabase(props)Retrieve a database

Listeners Methods

MethodModifiersDescription
onNewDatabaseItems(props)staticOn New Database Items
onUpdatedDatabaseItems(props)staticOn Updated Database Items

Pages Methods

MethodModifiersDescription
createPage(props)Create a page
retrievePage(props)Retrieve a page
updatePageProperties(props)Update page properties

Other Methods

MethodModifiersDescription
getBaseUrl()