🚀 Configuring Notion API Key and Permissions
This guide provides a step-by-step process for configuring the Notion API key and permissions, along with code examples and setup instructions for Claude.
📦 Creating a Notion API Key
-
Access Notion Integrations:
- Visit Notion Integrations and log in with your Notion account.
-
Create a New Integration:
- Click "New Integration", fill in the required fields (e.g., integration name), and select the workspace you want to connect.
-
Save the Integration:
- After submission, an API key will be generated. Copy this key and save it securely for subsequent API request authentication.
-
Retrieve the API Key:
- Copy the generated API key and ensure its security.
🔧 Configuring Page Permissions for Integration Use
-
Open a Notion Page:
- Open the Notion page to which you want to grant permissions.
-
Share the Page:
- Click the "Share" button on the top-right of the page.
-
Invite the Integration:
- Enter the name of your integration in the "Invite" text box and select it from the dropdown list. This grants the integration access to the page.
-
Set Permissions:
- Ensure that the integration has the necessary permissions to perform the required operations (e.g., read, write).
💻 Code Updates
In the notion_sdk.py
file, update PAGE_ID
to the ID of the page you created earlier. You can obtain this ID by copying the page link.
Example Notion Page ID
https://www.notion.so/MCP-Parent-Page-1d21f7216bdb80789f88fccd964b5031
Curl Example
Here is an example of using the Notion API to create a new page:
curl --location --request POST 'https://api.notion.com/v1/pages' \
--header 'Authorization: Bearer $NOTION_API_KEY' \
--header 'Content-Type: application/json' \
--header 'Notion-Version: 2021-05-13' \
--data '{
"parent": { "page_id": "1d21f7216bdb80789f88fccd964b5031" },
"properties": {
"title": [
{
"text": {
"content": "New Page Title"
}
}
]
}
'
🛠️ Setting Up in Claude
Installing the uv
Tool
If you are using a Mac, install it via the following command:
brew install uv
Verify the installation:
uv --version
Run a Python script:
uv run <script>.py
Setting Environment Variables
In Claude, you need to add NOTION_API_KEY
to the environment variables. For example:
export NOTION_API_KEY=your_api_key_here
Alternatively, specify the executable path in the configuration file:
{
"executors": {
"default_python": {
"command": "/usr/bin/python3",
"env": {
"NOTION_API_KEY": "your_api_key"
}
}
}
}
📥 Installing the Notion Library
Install notion-client
using pip:
pip install notion-client
📚 Generating Docstring Examples
The following are docstring examples for functions to help LLMs and humans understand the usage and parameters of the tools:
Example Function and Its Docstring
def get_page_content(page_id: str) -> dict:
"""
Get the content of a specified page.
Args:
page_id (str): The ID of the page whose content needs to be retrieved.
Returns:
dict: The page content, including the title and other attributes.
"""
# Specific implementation code
Another Example
def create_new_page(parent_id: str, title: str) -> str:
"""
Create a new page under a specified parent page.
Args:
parent_id (str): The ID of the parent page.
title (str): The title of the new page.
Returns:
str: The ID of the newly created page.
"""
# Specific implementation code
Usage Notes
- Summary: Briefly describe the purpose of the function.
- Parameters: Explain each input parameter, its type, and its use in detail.
- Return Value: Clearly state the type and meaning of the return value.
By following these guidelines, you can ensure that docstrings are both human-friendly and model-friendly.







