|
| 1 | +# Introduction to using Notion's SDK for Python |
| 2 | + |
| 3 | +## Learn how to make Public API requests |
| 4 | + |
| 5 | +Use this sample code to learn how to make Public API requests with varying |
| 6 | +degrees of difficulty. |
| 7 | + |
| 8 | +The sample code is split into two sections: |
| 9 | + |
| 10 | +1. `basic` |
| 11 | +2. `intermediate` |
| 12 | + |
| 13 | +(An `advanced` section will soon be added, as well.) |
| 14 | + |
| 15 | +If you are new to Notion's SDK for Python, start with the code samples in the |
| 16 | +`/basic` directory to get more familiar to basic concepts. |
| 17 | + |
| 18 | +The files in each directory will build on each other to increase in complexity. |
| 19 | +For example, in `/intermediate`, first you will see how to create a database, |
| 20 | +then how to create a database and add a page to it, and finally create a |
| 21 | +database, add a page, and query/sort the database. |
| 22 | + |
| 23 | +## Table of contents |
| 24 | + |
| 25 | +In case you are looking for example code for a specific task, the files are |
| 26 | +divided as follows: |
| 27 | + |
| 28 | +- `basic/1_add_block.py`: Create a new block and append it to an existing |
| 29 | + Notion page. |
| 30 | +- `basic/2_add_linked_block.py`: Create and append new blocks, and add a link |
| 31 | + to the text of a new block. |
| 32 | +- `basic/3_add_styled_block.py`: Create and append new blocks, and apply text |
| 33 | + styles to them. |
| 34 | +- `intermediate/1_create_a_database.py`: Create a new database with defined |
| 35 | + properties. |
| 36 | +- `intermediate/2_add_page_to_database.py`: Create a new database and add new |
| 37 | + pages to it. |
| 38 | +- `intermediate/3_query_database.py`: Create a new database, add pages to it, |
| 39 | + and filter the database entries (pages). |
| 40 | +- `intermediate/4_sort_database.py`: Create a new database, add pages to it, |
| 41 | + and filter/sort the database entries (pages). |
| 42 | +- `intermediate/5_upload_file.py`: Upload a file to Notion and attach it to a |
| 43 | + page as an image block. |
| 44 | + |
| 45 | +## Running locally |
| 46 | + |
| 47 | +### 1. Clone project and install dependencies |
| 48 | + |
| 49 | +To use this example on your machine, clone the repo and move into your local |
| 50 | +copy: |
| 51 | + |
| 52 | +```bash |
| 53 | +git clone https://github.com/ramnes/notion-sdk-py.git |
| 54 | +cd notion-sdk-py |
| 55 | +``` |
| 56 | + |
| 57 | +Next, move into this example in the `/examples` directory, and install its |
| 58 | +dependencies: |
| 59 | + |
| 60 | +```bash |
| 61 | +cd examples/intro_to_notion_api |
| 62 | +pip install -r requirements.txt |
| 63 | +``` |
| 64 | + |
| 65 | +Alternatively, you can create a virtual environment: |
| 66 | + |
| 67 | +```bash |
| 68 | +cd examples/intro_to_notion_api |
| 69 | +python -m venv venv |
| 70 | +source venv/bin/activate # On Windows use: venv\Scripts\activate |
| 71 | +pip install -r requirements.txt |
| 72 | +``` |
| 73 | + |
| 74 | +### 2. Set your environment variables in a `.env` file |
| 75 | + |
| 76 | +A `.env.example` file has been included and can be renamed `.env` (or you can |
| 77 | +run `cp .env.example .env` to copy the file). |
| 78 | + |
| 79 | +Update the environment variables below: |
| 80 | + |
| 81 | +```bash |
| 82 | +NOTION_API_KEY=<your-notion-api-key> |
| 83 | +NOTION_PAGE_ID=<notion-page-id> |
| 84 | +``` |
| 85 | + |
| 86 | +`NOTION_API_KEY`: Create a new integration in the |
| 87 | +[integrations dashboard](https://www.notion.com/my-integrations) and retrieve |
| 88 | +the API key from the integration's `Secrets` page. |
| 89 | + |
| 90 | +`NOTION_PAGE_ID`: Use the ID of any Notion page that you want to test adding |
| 91 | +content to. |
| 92 | + |
| 93 | +The page ID is the 32 character string at the end of any page URL. |
| 94 | + |
| 95 | + |
| 96 | +### 3. Give the integration access to your page |
| 97 | + |
| 98 | +Your Notion integration will need permission to interact with the Notion page |
| 99 | +being used for your `NOTION_PAGE_ID` variable. To provide access, do the |
| 100 | +following: |
| 101 | + |
| 102 | +1. Go to the page in your workspace. |
| 103 | +2. Click the `•••` (more menu) on the top-right corner of the page. |
| 104 | +3. Scroll to the bottom of the menu and click `Add connections`. |
| 105 | +4. Search for and select your integration in the `Search for connections...` |
| 106 | + menu. |
| 107 | + |
| 108 | +Once selected, your integration will have permission to read content from the |
| 109 | +page. |
| 110 | + |
| 111 | +**If you are receiving authorization errors, make sure the integration has |
| 112 | +permission to access the page.** |
| 113 | + |
| 114 | +### 4. Run individual examples |
| 115 | + |
| 116 | +You have several options to run the examples: |
| 117 | + |
| 118 | +#### Option 1: Run directly with python |
| 119 | + |
| 120 | +```bash |
| 121 | +python basic/1_add_block.py |
| 122 | +python basic/2_add_linked_block.py |
| 123 | +python basic/3_add_styled_block.py |
| 124 | + |
| 125 | +python intermediate/1_create_a_database.py |
| 126 | +python intermediate/2_add_page_to_database.py |
| 127 | +python intermediate/3_query_database.py |
| 128 | +python intermediate/4_sort_database.py |
| 129 | +python intermediate/5_upload_file.py |
| 130 | +``` |
| 131 | + |
| 132 | +#### Option 2: Run as modules |
| 133 | + |
| 134 | +```bash |
| 135 | +python -m basic.1_add_block |
| 136 | +python -m intermediate.1_create_a_database |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## Additional resources |
| 142 | + |
| 143 | +To learn more, read the |
| 144 | +[Public API docs](https://developers.notion.com/) for additional information |
| 145 | +on using Notion's API. The API docs include a series of |
| 146 | +[guides](https://developers.notion.com/docs) and the |
| 147 | +[API reference](https://developers.notion.com/reference/intro), which has a |
| 148 | +full list of available endpoints. |
| 149 | + |
| 150 | +To see more examples of what you can build with Notion, see our other sample |
| 151 | +integrations in the parent `/examples` directory. To learn how to build an |
| 152 | +internal integration with an interactive frontend, read the |
| 153 | +[Build your first integration](https://developers.notion.com/docs/create-a-notion-integration) |
| 154 | +guide. |
| 155 | + |
| 156 | +To connect with other developers building with Notion, join the |
| 157 | +[Notion Developers Slack group](https://join.slack.com/t/notiondevs/shared_invite/zt-20b5996xv-DzJdLiympy6jP0GGzu3AMg). |
0 commit comments