Reputation: 107
I'm working on a way to upload local images(JPEG) to a page in Notion via Notion-API with Python. I already converted the picture in base64 and now I want to add it in a block, inside a existing page. I know theres a way to upload images from a URL but first I want to try if this is possible.
I tried different URLs of the api to which i send my call, because I'm not completely sure how to set up this up properly. It's my first try to directly interact with an API, so this is a bit of a struggle for me.
This is the part of my Code in which I have problems with. I think it might be just a problem with the json formatting, the url, or I completely missed how to to do it properly.
# Image as base64
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
url1 = "https://api.notion.com/v1/blocks/input_page_id/children"
url2 = "https://api.notion.com/v1/pages"
headers = {
"Authorization": f"Bearer {notion_api_key}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28",
"accept": "application/json"
}
data = {
"parent": {
"type": "page_id",
"page_id": "input_page_id" },
"children": {
"type": "image",
"image": {
"type": "file",
"file_ids": [
{
"url": "data:image/jpeg;base64," + image_base64
}
]
}
}
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
The first URL "url1" is invalid and I tried a few variations of it, but as I understand it should be the right endpoint to update(in my case create) a block and a page in notion is also a block, so I just put in the page id here. The error message i get for "url2" is as follows:
{'object': 'error', 'status': 400, 'code': 'validation_error', 'message': 'body failed validation: body.parent.database_id should be defined, instead was `undefined`.'}
If uploading the images as base64 isn't possible anyway, i would try to use an Cloud Storage like AWS S3 or something which is designed for private use like Dropbox.
Upvotes: 1
Views: 1667
Reputation: 14630
Sadly, the API does not support uploading images, only URLs to remote images.
Sources:
Upvotes: 0