Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example: Create Meeting Notes Page in Notion
import os, uuid, time, requests
from datetime import datetime
# Notion uses a single endpoint to process transactions
NOTION_URL = 'https://www.notion.so/api/v3/saveTransactions'
# Pull the values we need from the environment, these will differ for each Notion user
token = os.environ['NOTION_KEY']
user = os.environ['NOTION_USER_ID']
space_id = os.environ['NOTION_SPACE_ID']
shard_id = os.environ['NOTION_SHARD_ID']
# Create a cookies dictionary for the user token, this is required for authorization
cookies = dict(token_v2=token)
# Lock in the time and date
today = datetime.today().strftime('%Y-%m-%d')
millis = int(round(time.time() * 1000))
# Setup out meeting title and sections
meeting_name = "{} Weekly Strategy".format(today)
meeting_template = [
'Goals',
'Highest-Leverage Customer Activities',
'Highest-Leverage Product Activities',
'What are the open customer action items?',
'What are the open product action items?',
'Action Items'
]
# Create UUIDs for the transaction and request.
# As well as the page and content block we are going to create
transaction_id = str(uuid.uuid4())
request_id = str(uuid.uuid4())
page_id = str(uuid.uuid4())
block_id = str(uuid.uuid4())
# Construct the transaction that we want Notion to save
# Notion Transactions are comprised of several operations to create, link and update resources
operation = {
"requestId": request_id,
"transactions": [
{
"id": transaction_id,
"shardId": shard_id,
"spaceId": space_id,
"operations": [
# First, create the block that will respresent our meeting page
{
"id": page_id,
"table": "block",
"path": [],
"command": "set",
"args": {
"type": "page",
"id": page_id,
"version": 1
}
},
# Next, set the page title
{
"id": page_id,
"table": "block",
"path": ["properties", "title"],
"command": "set",
"args": [
[meeting_name]
]
},
# Link the page we've created to the table of meeting notes
{
"id": page_id,
"table": "block",
"path": [],
"command": "update",
"args": {
"parent_id": parent_table,
"parent_table": "collection",
"alive": True
}
},
# Now, we canc reate our first content block
{
"id": block_id,
"table": "block",
"path": [],
"command": "set",
"args": {
"type": "text",
"id": block_id,
"version": 1
}
},
# For illustration purposes, we are create a single plain text block
{
"id": block_id,
"table": "block",
"path": ["properties", "title"],
"command": "set",
"args": [[f"{h}\n\n"] for h in meeting_template]
},
# We then need to associate our content block with the page we've created
{
"id": block_id,
"table": "block",
"path": [],
"command": "update",
"args": {
"parent_id": page_id,
"parent_table": "block",
"alive": True
}
},
{
"id": page_id,
"path": ["content"],
"table": "block",
"command": "listAfter",
"args": {
"id": block_id
}
},
# The reaminaing operations primarly update the created by values
# and complete any linking that Notion requires
# Set the Created By User
{
"table": "block",
"id": page_id,
"path": [
"created_by_id"
],
"command": "set",
"args": user_id
},
{
"table": "block",
"id": page_id,
"path": [
"created_by_table"
],
"command": "set",
"args": "notion_user"
},
# Set the Created Time
{
"table": "block",
"id": page_id,
"path": [
"created_time"
],
"command": "set",
"args": millis
},
# Set the Last Edited Time
{
"table": "block",
"id": page_id,
"path": [
"last_edited_time"
],
"command": "set",
"args": millis
},
# Set the Lasted User to Edit
{
"table": "block",
"id": page_id,
"path": [
"last_edited_by_id"
],
"command": "set",
"args": user_id
},
{
"table": "block",
"id": page_id,
"path": [
"last_edited_by_table"
],
"command": "set",
"args": "notion_user"
},
]
}
]
}
r = requests.post(NOTION_URL, cookies=cookies, json=operation)
if r.ok:
print(f"Successfully created Notion page: {meeting_name}")
else:
print("Encountered an error creating Notion page")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.