Notion API master series 11 - Analysis - Create a page (First)

소찬 (Chan)·2022년 9월 19일
0
post-thumbnail

Reference API URL : Create a page

Before "create a page," API Bot has to have write rights.
Click the share button and add the bot, but we have a curious thing. Indeed, the API bot can create a page on the uppermost?
You already guess The concept that adding a bot in the uppermost is not exist. So it is impossible to create a page at the uppermost, and the API bot can create a page shared with the bot in the page or database.
However, it doesn't mean Notion API doesn't have the concept which is the uppermost. Refer to the parent named workspace at the below link.
https://developers.notion.com/reference/parent-object#workspace-parent
Anyway, let's find out parameter.

  • parent (json) required
    A database parent or page parent
    Fill in the parent page id, but why does this parameter require json type? It seems to need not only id.
    json type means a from a key with a key's value in dictionary {} type.
    Just bind a form like as below.
    {"database_id": "638746d9-9bd4-4a2f-a015-d38b41e0bd5e"}
    {"page_id": "aca05c96-1fa9-4e7c-b539-24533cb4da37"}
    API document mentioned why we write as above, Refer to the link, https://developers.notion.com/reference/page#database-parent
  • properties (json) required
    Property values of this page. The keys are the names or IDs of the property and the values are property values.
    Property is a bunch that is classification and option value information. If a form is slightly different, API won't create at all, so if you create a page correctly, make a reference page first, set the wish property, retrieve the page, and refer to the information retrieved from the page.

For example, bring properties with the retrieve method.

 'parent': {'database_id': '021eb424-7a41-47ae-9252-f67d4f6e6b05',
            'type': 'database_id'},
 'properties': {'상태': {'id': '%3EQQn',
                       'select': {'color': 'brown',
                                  'id': '588ecfe9-05b0-40c2-aa32-3f26ee0b5281',
                                  'name': '★★★★'},
                       'type': 'select'},
                '이름': {'id': 'title',
                       'title': [{'annotations': {'bold': False,
                                                  'code': False,
                                                  'color': 'default',
                                                  'italic': False,
                                                  'strikethrough': False,
                                                  'underline': False},
                                  'href': None,
                                  'plain_text': '네오피자',
                                  'text': {'content': '네오피자', 'link': None},
                                  'type': 'text'}],
                       'type': 'title'}}

Now leave it as properties value except the 'id' value. And then change values from the original data for creation.

properties_new = {'상태': {'select': {'color': 'red',
                                     'name': '★★★★★'},
                          'type': 'select'},
                  '이름': {'id': 'title',
                          'title': [{'annotations': {'bold': False,
                                                     'code': False,
                                                     'color': 'default',
                                                     'italic': False,
                                                     'strikethrough': False,
                                                     'underline': False},
                          'href': None,
                          'plain_text': '피자샵',
                          'text': {'content': '피자샵', 'link': None},
                          'type': 'text'}],
                  'type': 'title'}

Of course, you can input the value manually, but bringing the properties key value and modifying the value needed is better.

page_sample = notion.pages.retrieve(page_id='1f217cc1-225f-4580-8e22-cd7eb8db3c5e')
properties_new = page_sample['properties']

properties_new['상태']['select']['color'] = 'red'
properties_new['상태']['select']['name'] = '★★★★★'
properties_new['이름']['title'][0]['plain_text'] = '피자샵'
properties_new['이름']['title'][0]['text']['content'] = '피자샵'

Then, if we modify the value needed, these properties remain id. Is it ok to create a page?
Occasionally, it can create without an issue. But rarely, because of id, an error occurs for creating a page, so it is better to delete id info if possible.

del properties_new['상태']['id']
del properties_new['상태']['select']['id']

Now create a page based on the above information.

notion.pages.create(parent={'database_id': '021eb424-7a41-47ae-9252-f67d4f6e6b05'},
                    properties=properties_new)

properties are sensitive, so fix the error from returned information if an error occurs when the calling API occurs.
For instance
I give one example.

notion_client.errors.APIResponseError: Select option color doesn't match existing for 588ecfe9-05b0-40c2-aa32-3f26ee0b5281.

What does the above error mean? If '★★★★★' characters set 'yellow' color option like as above, an error occurs that the color doesn't match.
This case change 'color option to 'yellow' then the problem will solve.

properties_new['상태']['select']['color'] = 'yellow'
profile
QA Specialist

0개의 댓글