0:00 hi everyone I'm Patrick and in this
0:01 tutorial I show you how you can work
0:03 with the notion API in Python to
0:05 automate stuff so this example here is
0:08 an actual database from one of my own
0:10 notion boards I have one database where
0:13 I store all my blog posts so whenever I
0:16 upload a new post to pythonengineer.com
0:19 I have one script that automatically
0:22 adds this year in notion so here I store
0:24 the URL the title and the publish date
0:27 and then some more infos that I can
0:28 later use to analyze or automate some
0:31 more stuff so in this video I show you
0:33 how you set up the notion API and then
0:36 connect this in Python and then how you
0:38 create a database and connect this and
0:41 then do operations like reading creating
0:44 updating and deleting posts so let's get
0:47 started first let's set up the notion
0:49 API so for this we can follow this
0:51 official guide I will put the link in
0:53 the description and step one is to
0:56 create an integration so we can go to my
0:59 integrate stations and then click on
1:01 create new integration and give this a
1:04 name I call this demo integration but of
1:07 course you can give it a more meaningful
1:09 name and down here you can select the
1:12 capabilities so in our case we want read
1:15 update and insert capabilities but we
1:18 don't need user capabilities so you can
1:21 click on no user information and then
1:23 click on submit and here you get your
1:26 API token so let's click on show and
1:30 copy and now head over to a python file
1:33 and then let's create a new variable
1:36 that we call
1:38 notion underscore token and then paste
1:42 this in here as a string and this was
1:45 step one so now step two is to share a
1:50 database with your integration so let's
1:53 create a new database here first let's
1:56 create a new page and I call this demo
1:59 and and in here let's type slash and
2:03 then database and select full page
2:06 database and now let's call this demo
2:09 database and here you can configure this
2:13 and modify this how you want this in our
2:16 case I want to give it the same
2:18 columns that we've seen here so make
2:22 sure to select not the page but the demo
2:26 database and then the first one should
2:29 be called
2:30 URL then the second one should be called
2:34 title and then we also want to change
2:39 the type to a text type and then let's
2:42 create one more that we call published
2:45 and for this we also want to change the
2:49 type and select a date so you will learn
2:52 how you can work with text and date
2:54 fields and now we need to share this
2:57 database with our integration so again
3:00 make sure to select the database and not
3:03 the demo and here let's click on dot dot
3:06 dot and then add connections and then
3:09 select your integration and confirm this
3:12 so now the integration has access and
3:15 can modify this then you also need to
3:18 get the database ID so for this click on
3:21 share copy the link and then you can
3:25 paste this into your browser and then
3:29 the first part before this question mark
3:33 all this part is your database ID so
3:36 again let's copy this and then let's
3:39 create another variable that we call
3:42 database ID equals and then paste this
3:46 in here as string so now we can set up
3:49 the notion API in Python first let's
3:52 learn how we can query the database so
3:55 we import requests and we can install
3:58 this with Pip install request this this
4:00 is what we use to send the API requests
4:03 and then later we also use date time to
4:06 convert the strings to the date time
4:08 object and now we need to set up the
4:11 headers like so and by the way you can
4:14 find this in the API reference there you
4:17 also see the latest notion version and
4:20 then let's create a new helper function
4:22 that we call get Pages here we query
4:26 this URL and we send a payload so by
4:30 default we want 100 pages in a moment I
4:33 also show you how you can use more and
4:36 then we send a post request to this URL
4:39 with the Json payload and the headers
4:42 and then we can extract the Json data
4:45 from the result then here I also dumped
4:48 this to a file to show it to you and
4:51 then in the end we get the results from
4:54 the object and then return this so then
4:57 we can call this helper function get
4:59 pages and this will be a list so now we
5:03 can iterate over this for page in pages
5:06 and then extract the different fields
5:08 for example we can get the page ID and
5:11 then the properties and then here the
5:14 inner properties like your L title and
5:17 published and here we convert this
5:19 string here back to a date time object
5:22 and then we print this and this looks a
5:26 little bit ugly but this is just how the
5:29 Json data will look like so you will see
5:31 how this looks in a moment and first
5:34 let's add one row to our database and
5:37 give this a test URL then a test
5:44 title and also a test state so here
5:47 let's simply select today and now let's
5:50 go back to this and save this and now
5:53 run this and you see it printed test URL
5:58 test title and the date and if we have a
6:02 look at the Json file then you see this
6:05 is a very long file and if we have a
6:08 look inside then you see here this is
6:10 the results key then inside here we have
6:14 the properties and then for example we
6:17 have the title field where we have the
6:20 rich text then this is an array so we
6:23 want the first
6:24 first item and then we have text and
6:27 then content and this is why we also
6:32 have the same schema here and then
6:35 similar for the publish date we have
6:38 published and then date and then the
6:41 start field so here published date and
6:44 start and also the URL so if we have a
6:49 look at the URL field so the first one
6:52 is always a little bit special this is
6:53 always the called title so don't get
6:56 confused with the title column and then
6:59 in here we have title type text and then
7:02 text content and then again this is what
7:06 we have here and this is how we can
7:09 query the database and print this and
7:12 like I said by default it can query
7:14 maximum 100 pages so let's learn how we
7:18 can do this if we have more pages so if
7:21 we have more pages then we change the
7:24 function slightly so we give it an
7:27 additional argument num pages and this
7:30 is none by default and then we check if
7:33 numpages is none we say get all is true
7:36 and then we Define the page size as 100
7:39 if we want to get all the pages and
7:42 otherwise this specific page size so 100
7:45 is the maximum number that we can get
7:48 with one API call and then we set this
7:51 as the page size payload and again send
7:53 the post request and extract the Json
7:56 data and the results and then we check
7:59 while data has more is true and we want
8:02 to get all the pages so if we have a
8:05 look at the Json file then you will find
8:07 that there is a next cursor key and a
8:11 has more key and this is true if we have
8:14 more results than we got here and in
8:17 that case we again send a payload with
8:20 the page size and now here we also
8:23 include the start cursor key and the
8:26 start cursor is data next cursor and
8:31 then again we send the post request
8:33 extract the data and then extend our
8:36 list with the data results and this is
8:39 how you can get all the results if you
8:42 have more than 100 now let's learn how
8:44 we can create a new page so let's create
8:47 a new helper function create page that
8:49 gets a data parameter and this is a
8:52 dictionary with all the fields then the
8:54 URL L endpoint is this one and as
8:57 payload we have to send the parent field
8:59 with the database ID and also the
9:03 properties field with the data that we
9:05 pass to the function and then we send a
9:08 post request to the URL with the headers
9:10 and the Json payload and print their
9:12 result and return this now we set up the
9:15 data so we have a URL a test URL a test
9:19 title and for the time I recommend to
9:22 convert this to the UTC time zone and
9:26 then you also need this in ISO format so
9:29 this will gives you a string in ISO
9:32 format and then you create your
9:34 dictionary with the same Fields with the
9:37 same schema that you see in here and
9:39 then we simply call this helper function
9:42 create page so let's run this and this
9:46 prints 200 so this was successful and if
9:49 we go to our notion page then it
9:52 automatically refreshes and you see we
9:54 now have a new item in our database now
9:57 let's learn how we can update data in
10:00 the database so let's create a helper
10:02 function update page that gets a page ID
10:04 and the data to update then here this is
10:07 the endpoint URL and the payload needs
10:10 the properties field with the data and
10:12 this time we send a patch request and
10:15 now let's set up the data to update so
10:18 we Define a page ID and for example you
10:21 find this in the results if you query
10:24 the database and then under results you
10:28 will find the ID for each result so for
10:30 each page this is the page ID and then
10:34 in this case let's update the title then
10:37 again you need to give this this schema
10:40 as a dictionary the same that you find
10:43 this in here under title title Rich Text
10:47 then this is an array so we give this a
10:50 list and then the fields text and
10:53 content and then we call up update page
10:56 so let's run this and again we get
10:59 status code 200 and here we see we have
11:02 the updated title so this worked and now
11:05 the last thing is how to delete a page
11:08 so for this let's copy this because this
11:10 is actually the same endpoint so let's
11:13 create a function delete page and this
11:16 doesn't need data and here instead of
11:19 sending properties we send archived and
11:23 set the parameter to true and then again
11:28 we can give it the page ID and then call
11:32 delete page and here we only pass the
11:37 page ID so let's run this and again we
11:41 get 200 and again here we see this was
11:45 deleted so this worked as well yeah and
11:47 this is all I wanted to show you in this
11:49 tutorial this is how you can work with
11:51 the notion API in Python I hope you
11:53 enjoyed this tutorial and then I hope to
11:55 see you in the next one bye