Skip to content

Instantly share code, notes, and snippets.

@lgaud
Last active September 27, 2023 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lgaud/6aa2ea6bbb317973d94ba52c1d41ad91 to your computer and use it in GitHub Desktop.
Save lgaud/6aa2ea6bbb317973d94ba52c1d41ad91 to your computer and use it in GitHub Desktop.
Notion Hello World in Python
"""
1. Create an integration at https://www.notion.so/my-integrations
2. Set an environment variable NOTION_KEY with the value of the "Internal Integration Token"
3. Create a page and connect it to your integration.
Ensure you have requests installed in your environment `pip install requests`
"""
import os
import json
import requests
NOTION_KEY = os.environ.get("NOTION_KEY")
headers = {'Authorization': f"Bearer {NOTION_KEY}", 'Content-Type': 'application/json', 'Notion-Version': '2022-06-28'}
def pretty_print(response):
print(json.dumps(response.json(), indent=2))
search_response = requests.post('https://api.notion.com/v1/search', headers=headers)
print("--- Search Response ---")
pretty_print(search_response)
search_results = search_response.json()["results"]
if len(search_results) == 0:
print("No pages shared with integration!")
else:
page_id = search_results[0]["id"]
print("--- First Page Id ---")
print(page_id)
create_page_body = {
"parent": { "page_id": page_id },
"properties": {
"title": {
"title": [{ "type": "text", "text": { "content": "Hello World!" } }]
}
},
"children": [
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [{ "type": "text", "text": { "content": "This page was made using an APi call!" } }]
}
}
]
}
create_response = requests.post("https://api.notion.com/v1/pages", json=create_page_body, headers=headers)
print("--- Create Page Response ---")
pretty_print(create_response)
created_id = create_response.json()["id"]
blocks_response = requests.get(f"https://api.notion.com/v1/blocks/{created_id}/children", headers=headers)
pretty_print(blocks_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment