Prompt
How do I let an agent read from a database and write to Notion?
Latest observation
Oct 9, 2026
OpenAI APIWeb search: off
To let an agent read from a database and write to Notion, you typically give the agent access to two tools/integrations:
- A database connector for reading data
- A Notion integration for creating/updating pages
High-level flow
- Agent receives a task.
- Agent queries the database through a read tool.
- Agent transforms the data as needed.
- Agent writes the result to Notion through a Notion API tool.
1) Give the agent database read access
Option A: Direct SQL access
If your database is something like PostgreSQL, MySQL, or SQLite:
- Create a read-only database user
- Expose a tool that runs parameterized
SELECTqueries - Restrict it to approved tables/views if possible
Example tool idea:
run_sql(query: string) -> rows
Best practices:
- Only allow
SELECT - Prevent destructive queries
- Use query validation or a whitelist
- Prefer views over raw tables
Option B: Use an internal API
If you don’t want the agent connecting directly to the DB:
- Create a backend endpoint like
GET /agent/customer-summary - That endpoint queries the DB safely
- The agent calls the endpoint instead of SQL directly
This is often safer.
2) Set up Notion access
Create a Notion integration
In Notion:
- Go to Settings & members → Connections / Integrations
- Create a new internal integration
- Copy the Notion API token
- Share the target Notion page/database with that integration
Decide what the agent will do in Notion
The agent can:
- Create new pages
- Update existing pages
- Append blocks to pages
- Insert rows into a Notion database
3) Expose a Notion write tool
Typical actions:
create_page(database_id, properties, content)update_page(page_id, properties)append_blocks(page_id, blocks)
Example usage:
- Read a customer record from DB
- Create a Notion page in a “Customer Notes” database
- Populate page properties and a summary block
4) Define the agent’s tools
If you’re using an agent framework, register two tools:
- Database read tool
- Notion write tool
Example conceptual tool set:
get_customer_orders(customer_id)create_notion_page(title, body, tags)
The agent then plans:
- Call DB tool
- Summarize results
- Call Notion tool
5) Example implementation pattern
Database read
def get_sales_data(start_date, end_date):
query = """
SELECT customer_name, total_amount, order_date
FROM orders
WHERE order_date BETWEEN %s AND %s
"""
return db.execute(query, (start_date, end_date))
Notion write
import requests
def create_notion_page(title, summary):
url = "https://api.notion.com/v1/pages"
headers = {
"Authorization": f"Bearer {NOTION_TOKEN}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
}
payload = {
"parent": {"database_id": NOTION_DATABASE_ID},
"properties": {
"Name": {
"title": [{"text": {"content": title}}]
}
},
"children": [
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [{"type": "text", "text": {"content": summary}}]
}
}
]
}
return requests.post(url, headers=headers, json=payload).json()
6) Security considerations
Important:
- Use read-only DB credentials
- Limit what the agent can query
- Store secrets securely
- Validate and sanitize all input
- Log tool usage
- Add approval steps for sensitive writes
7) Common architecture choices
Simple
Agent → DB read function → Notion API write function
Safer
Agent → backend service → DB + Notion
The backend enforces rules and auditing.
More advanced
Agent with workflow orchestration:
- Step 1: fetch data
- Step 2: summarize
- Step 3: human approval
- Step 4: write to Notion
8) If you’re using a specific agent framework
Tell me which one:
- OpenAI Assistants / Responses API
- LangChain
- CrewAI
- AutoGen
- Zapier / Make
- Custom Python/Node agent
I can give you a concrete example for that stack, including code for:
- database access
- Notion integration
- agent tool definitions