Trouble extracting Notion page title via API

I’m struggling to grab a page title from Notion using their API. I’m new to Python and using the notion.py library. When I try to fetch the title and post it to another page, I get an error. Here’s what I’m dealing with:

from notion_client import Client
import time

my_token = 'my_secret_token'
notion = Client(auth=my_token)

source_page_id = 'id_of_page_to_read'
target_page_id = 'id_of_page_to_write'

try:
    source_page = notion.pages.retrieve(source_page_id)
    title = source_page['properties']['title']['title'][0]['plain_text']
    print(f'Page title: {title}')
    
    notion.pages.update(
        page_id=target_page_id,
        properties={'Title': {'title': [{'text': {'content': title}}]}}
    )
except Exception as e:
    print(f'Error: {str(e)}')

The code above is my attempt. It’s not working and I’m not sure why. Can anyone help me figure out what I’m doing wrong? Thanks!

I’ve dealt with similar issues when working with the Notion API. From my experience, the problem might be in how you’re accessing the title property. Notion’s API can be a bit tricky with property names.

Try modifying your code to use the ‘Name’ property instead of ‘title’. Many Notion pages use ‘Name’ as the default title property. Here’s what I’d suggest:

title = source_page['properties']['Name']['title'][0]['plain_text']

If that doesn’t work, you might need to inspect the ‘properties’ dictionary to find the correct key for the title. Sometimes, it can vary depending on how the page was set up.

Also, make sure your API token has the necessary permissions to read from the source page and write to the target page. I’ve been caught out by that before!

If you’re still having trouble, you could try printing out the entire ‘properties’ dictionary to see what’s actually available. That’s helped me debug similar issues in the past.

I’ve encountered similar challenges with the Notion API. One thing to consider is that the structure of the ‘properties’ can vary depending on the page setup. Instead of assuming a specific property name, you might want to iterate through the properties to find the title.

Here’s an approach that’s worked for me:

for prop_name, prop_value in source_page['properties'].items():
    if prop_value['type'] == 'title':
        title = prop_value['title'][0]['plain_text']
        break

if not title:
    raise ValueError('Title property not found')

This method is more flexible and can handle different property configurations. Also, ensure your API token has the correct permissions for both reading and writing operations. If issues persist, logging the full response from the API can provide valuable insights for debugging.

hey there! i’ve run into this before. the issue might be with how ur accessing the title. try printing out the whole properties dict to see what’s actually there:

print(source_page['properties'])

this’ll show u all the properties and their structure. then u can figure out the right way to grab that title. hope this helps!