Inserting Local JPEG Images into Notion Page Content Using Python and Notion API

Hey everyone! I’m trying to figure out how to add local JPEG images to a Notion page using Python and the Notion API. I’ve already turned the image into base64, but I’m stuck on how to put it in 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 with local files first. This is my first time working directly with an API, so I’m a bit lost.

Here’s what I’ve tried so far:

image_base64 = base64.b64encode(image_bytes).decode('utf-8')

api_url = 'https://api.notion.com/v1/blocks/my_page_id/children'

headers = {
    'Authorization': 'Bearer my_api_key',
    'Content-Type': 'application/json',
    'Notion-Version': '2022-06-28'
}

payload = {
    'parent': {'type': 'page_id', 'page_id': 'my_page_id'},
    '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 an error about validation failing. Am I formatting the JSON wrong? Or using the wrong API endpoint? If base64 doesn’t work, I might try using cloud storage instead. Any help would be awesome!

I understand the difficulties that come with using the Notion API for local image uploads, avamtz. I encountered similar challenges when I first experimented with this approach. The key issue is that Notion’s API does not accept direct file uploads or base64 encoded images. In my experience, uploading the image to a temporary hosting service such as Imgbb or Cloudinary and then using the provided URL in the API call is the most reliable method. It may feel like an extra step, but it ensures smoother integration with Notion.

I’ve encountered this issue before. While base64 encoding seems like a logical approach, Notion’s API doesn’t support it for image insertion. Your best bet is to use a cloud storage solution. I’ve had success with Amazon S3 for this purpose. Once you’ve uploaded your image there, you can use the generated URL in your API call. It adds an extra step, but it’s currently the most reliable method for inserting local images into Notion pages via the API. If you need help setting up S3 or adjusting your code for this approach, feel free to ask.

hey avamtz, i’ve been there! notion’s api can be tricky. unfortunately, base64 won’t work directly. you’ll need to upload the image to a cloud service first (like aws s3 or imgur) and then use that url in the api call. it’s a bit of a pain, but that’s how notion handles local images. good luck!