Search code examples
pythonnotion-api

How to create a database (Calendar) in a page in notion use to python?


I want to create a calendar for the database in the page. But I get the following error. The error code 400 occurs and I want to get the same result as the picture below. ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

My code:

from wsgiref import headers
import requests, json


token = '~~~~'


parent_pageID="df0f2f4b-ca24-46e3-a48e-85650cbd5ba5"

page_header={
"Authorization": "Bearer " + token,
"Content-Type": "application/json",
"Notion-Version": "2022-06-28"
}

def Createpage(pageId,headers):
createDBURL=f"https://api.notion.com/v1/databases"

DBData={
    "object": "database",
    
    "title": [
        {
            "type": "text",
            "text": {
                "content": "new page"                   
            },                
            "plain_text": "new page"                
        }
    ],
    "properties": {
        "date": {
            "name": "date",
            "type": "date",
            "date": {}
        },
        "content": {                
            "name": "content",
            "type": "rich_text",
            "rich_text": {}
        },
        "to do": {
            "id": "title",
            "name": "to do",
            "type": "title",
            "title": {}
        }
    },
    "parent": {
    "type": "page_id",
    "page_id": parent_pageID
    }
    
}

res = requests.request("POST", createDBURL, headers=headers, data=DBData)

print(res.status_code)
print(res.text)


Createpage(parent_pageID,page_header)

Error code:

{"object":"error","status":400,"code":"invalid_json","message":"Error parsing JSON body."}

Solution

  • Since you have a dictionary instead of json, you just need to convert DBData.

    print(type(DBData))
    d = json.dumps(DBData)
    print(type(d))
    
    <class 'dict'>
    <class 'str'>