Composio Notion integration guide
Quick answer
Use the
composio-core and composio-openai Python packages to integrate Composio with Notion. Authenticate with your API keys, retrieve Notion content, then pass it as context to the OpenAI client with Composio tools enabled for seamless AI-powered Notion workflows.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)Composio API keyNotion integration tokenpip install openai>=1.0 composio-core composio-openai requests
Setup
Install the required Python packages and set environment variables for your API keys and Notion token.
- Install packages:
openai,composio-core,composio-openai, andrequests. - Set environment variables
OPENAI_API_KEY,COMPOSIO_API_KEY, andNOTION_TOKEN.
pip install openai>=1.0 composio-core composio-openai requests output
Collecting openai Collecting composio-core Collecting composio-openai Collecting requests Successfully installed openai composio-core composio-openai requests
Step by step
This example shows how to fetch a Notion page content, initialize Composio tools, and call the OpenAI chat completion with Composio tools enabled to interact with Notion data.
import os
import json
import requests
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
# Load environment variables
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
COMPOSIO_API_KEY = os.environ["COMPOSIO_API_KEY"]
NOTION_TOKEN = os.environ["NOTION_TOKEN"]
NOTION_PAGE_ID = "your-notion-page-id" # Replace with your Notion page ID
# Initialize OpenAI client
client = OpenAI(api_key=OPENAI_API_KEY)
# Initialize Composio toolset
toolset = ComposioToolSet(api_key=COMPOSIO_API_KEY)
tools = toolset.get_tools(actions=[Action.NOTION_READ_PAGE])
# Function to fetch Notion page content
def fetch_notion_page(page_id: str, token: str) -> str:
url = f"https://api.notion.com/v1/blocks/{page_id}/children"
headers = {
"Authorization": f"Bearer {token}",
"Notion-Version": "2022-06-28"
}
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
texts = []
for block in data.get("results", []):
if block["type"] == "paragraph":
texts.append("".join([t["plain_text"] for t in block["paragraph"]["text"]]))
return "\n".join(texts)
# Fetch Notion page text
notion_text = fetch_notion_page(NOTION_PAGE_ID, NOTION_TOKEN)
# Prepare messages with Notion context
messages = [
{"role": "system", "content": "You are an AI assistant with access to Notion data."},
{"role": "user", "content": f"Here is the Notion page content:\n{notion_text}\nPlease summarize it."}
]
# Call OpenAI chat completion with Composio tools
response = client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=messages
)
print("AI summary:", response.choices[0].message.content) output
AI summary: This Notion page contains information about your project goals, timelines, and key tasks. It outlines the main objectives and deadlines to keep the team aligned.
Common variations
You can adapt the integration by:
- Using async calls with
httpxand async OpenAI client. - Changing the model to
gpt-4ofor higher quality responses. - Adding more Composio actions like
Action.NOTION_APPEND_BLOCKto modify Notion pages.
import asyncio
import httpx
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
async def async_notion_integration():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])
tools = toolset.get_tools(actions=[Action.NOTION_READ_PAGE])
async with httpx.AsyncClient() as http_client:
# Async fetch Notion page omitted for brevity
notion_text = "Async fetched Notion content"
messages = [
{"role": "system", "content": "You are an AI assistant with Notion access."},
{"role": "user", "content": f"Summarize this:\n{notion_text}"}
]
response = await client.chat.completions.create(
model="gpt-4o",
tools=tools,
messages=messages
)
print("Async AI summary:", response.choices[0].message.content)
asyncio.run(async_notion_integration()) output
Async AI summary: This is an async summary of the Notion page content.
Troubleshooting
- 401 Unauthorized: Check your
OPENAI_API_KEY,COMPOSIO_API_KEY, andNOTION_TOKENenvironment variables are set correctly. - Notion API errors: Verify your Notion integration has access to the page and the page ID is correct.
- Tool calls not working: Ensure you pass
tools=toolsin thechat.completions.createcall and use supported Composio actions.
Key Takeaways
- Use
composio-coreandcomposio-openaipackages to enable Notion AI workflows. - Fetch Notion content via the official Notion API and pass it as context to the OpenAI client with Composio tools.
- Always pass
tools=toolsin your chat completion calls to enable Composio integrations. - You can extend functionality by adding more Composio actions like reading, appending, or updating Notion blocks.
- Check environment variables and API permissions carefully to avoid common authentication errors.