0

Update Page Properties

by
Published Apr 16, 2024

Set a page's title, icon, or property values.

Script notion Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 days ago
1
//native
2

3
/**
4
 * Update Page Properties
5
 * Set a page's title, icon, or property values. `properties` takes Notion property values keyed by name, e.g. {"Status": {"status": {"name": "Done"}}}; properties left out are unchanged.
6
 */
7
export async function main(
8
  auth: RT.Notion,
9
  page_id: string,
10
  title: string | undefined,
11
  properties: { [key: string]: any } | undefined,
12
  icon_emoji: string | undefined
13
) {
14
  const body: { [key: string]: any } = { properties: { ...properties } }
15
  if (title !== undefined && title !== "") {
16
    body.properties.title = { title: [{ text: { content: title } }] }
17
  }
18
  if (icon_emoji !== undefined && icon_emoji !== "") {
19
    body.icon = { type: "emoji", emoji: icon_emoji }
20
  }
21

22
  const response = await fetch(`https://api.notion.com/v1/pages/${page_id}`, {
23
    method: "PATCH",
24
    headers: {
25
      Authorization: `Bearer ${auth.token}`,
26
      "Content-Type": "application/json",
27
      Accept: "application/json",
28
      "Notion-Version": "2026-03-11",
29
    },
30
    body: JSON.stringify(body),
31
  })
32

33
  if (!response.ok) {
34
    throw new Error(`${response.status} ${await response.text()}`)
35
  }
36

37
  return await response.json()
38
}
39

Other submissions
  • Submitted by henri186 Bun
    Created 516 days ago
    1
    // Define the Notion resource type as required for the function
    2
    type Notion = {
    3
      token: string
    4
    }
    5
    
    
    6
    export async function main(notion: Notion, pageId: string, propertyUpdates: any): Promise<any> {
    7
      // URL to update a specific page
    8
      const pageUrl = `https://api.notion.com/v1/pages/${pageId}`;
    9
    
    
    10
      // Set up the request headers
    11
      const headers = {
    12
        'Authorization': `Bearer ${notion.token}`,
    13
        'Notion-Version': '2022-06-28', // Use the latest version supported by your integration
    14
        'Content-Type': 'application/json'
    15
      };
    16
    
    
    17
      // Set up the body with property updates
    18
      const body = {
    19
        properties: propertyUpdates
    20
      };
    21
    
    
    22
      // Perform the fetch request to update the Notion page
    23
      try {
    24
        const response = await fetch(pageUrl, {
    25
          method: 'PATCH', // The Notion API requires a PATCH request to update a page
    26
          headers: headers,
    27
          body: JSON.stringify(body)
    28
        });
    29
    
    
    30
        // Check if the request was successful
    31
        if (!response.ok) {
    32
          throw new Error(`Failed to update Notion page: ${response.status} ${response.statusText}`);
    33
        }
    34
    
    
    35
        // Parse the JSON response
    36
        const data = await response.json();
    37
    
    
    38
        // Return the updated data
    39
        return data;
    40
      } catch (error) {
    41
        console.error('Error updating Notion page:', error);
    42
        throw error; // Rethrow the error to be handled by the caller
    43
      }
    44
    }