I’m trying to figure out how to add local JPEG images to a Notion page using their API and Python. I’ve already turned the image into base64 but I’m struggling with adding it to a block on an existing page. I know there’s a way to add images from URLs but I want to see if I can do it this way first.
I’m new to working with APIs directly so I’m a bit confused about the correct setup. I’ve tried different API URLs but I’m not sure which one is right. Here’s the part of my code that’s giving me trouble:
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
api_url = 'https://api.notion.com/v1/blocks/page_id_here/children'
headers = {
'Authorization': 'Bearer your_api_key_here',
'Content-Type': 'application/json',
'Notion-Version': '2022-06-28'
}
payload = {
'parent': {'page_id': 'your_page_id_here'},
'children': [{
'type': 'image',
'image': {
'type': 'file',
'file': {'url': f'data:image/jpeg;base64,{image_base64}'}
}
}]
}
response = requests.post(api_url, headers=headers, json=payload)
print(response.json())
I’m getting a 400 error saying something about a database_id being undefined. If this base64 method doesn’t work, I might try using a cloud storage service instead. Any ideas on what I’m doing wrong?