YouTube Transcript:
How to automate Notion with Python | Notion API Tutorial
Skip watching entire videos - get the full transcript, search for keywords, and copy with one click.
Share:
Video Transcript
View:
hi everyone I'm Patrick and in this
tutorial I show you how you can work
with the notion API in Python to
automate stuff so this example here is
an actual database from one of my own
notion boards I have one database where
I store all my blog posts so whenever I
upload a new post to pythonengineer.com
I have one script that automatically
adds this year in notion so here I store
the URL the title and the publish date
and then some more infos that I can
later use to analyze or automate some
more stuff so in this video I show you
how you set up the notion API and then
connect this in Python and then how you
create a database and connect this and
then do operations like reading creating
updating and deleting posts so let's get
started first let's set up the notion
API so for this we can follow this
official guide I will put the link in
the description and step one is to
create an integration so we can go to my
integrate stations and then click on
create new integration and give this a
name I call this demo integration but of
course you can give it a more meaningful
name and down here you can select the
capabilities so in our case we want read
update and insert capabilities but we
don't need user capabilities so you can
click on no user information and then
click on submit and here you get your
API token so let's click on show and
copy and now head over to a python file
and then let's create a new variable
that we call
notion underscore token and then paste
this in here as a string and this was
step one so now step two is to share a
database with your integration so let's
create a new database here first let's
create a new page and I call this demo
and and in here let's type slash and
then database and select full page
database and now let's call this demo
database and here you can configure this
and modify this how you want this in our
case I want to give it the same
columns that we've seen here so make
sure to select not the page but the demo
database and then the first one should
be called
URL then the second one should be called
title and then we also want to change
the type to a text type and then let's
create one more that we call published
and for this we also want to change the
type and select a date so you will learn
how you can work with text and date
fields and now we need to share this
database with our integration so again
make sure to select the database and not
the demo and here let's click on dot dot
dot and then add connections and then
select your integration and confirm this
so now the integration has access and
can modify this then you also need to
get the database ID so for this click on
share copy the link and then you can
paste this into your browser and then
the first part before this question mark
all this part is your database ID so
again let's copy this and then let's
create another variable that we call
database ID equals and then paste this
in here as string so now we can set up
the notion API in Python first let's
learn how we can query the database so
we import requests and we can install
this with Pip install request this this
is what we use to send the API requests
and then later we also use date time to
convert the strings to the date time
object and now we need to set up the
headers like so and by the way you can
find this in the API reference there you
also see the latest notion version and
then let's create a new helper function
that we call get Pages here we query
this URL and we send a payload so by
default we want 100 pages in a moment I
also show you how you can use more and
then we send a post request to this URL
with the Json payload and the headers
and then we can extract the Json data
from the result then here I also dumped
this to a file to show it to you and
then in the end we get the results from
the object and then return this so then
we can call this helper function get
pages and this will be a list so now we
can iterate over this for page in pages
and then extract the different fields
for example we can get the page ID and
then the properties and then here the
inner properties like your L title and
published and here we convert this
string here back to a date time object
and then we print this and this looks a
little bit ugly but this is just how the
Json data will look like so you will see
how this looks in a moment and first
let's add one row to our database and
give this a test URL then a test
title and also a test state so here
let's simply select today and now let's
go back to this and save this and now
run this and you see it printed test URL
test title and the date and if we have a
look at the Json file then you see this
is a very long file and if we have a
look inside then you see here this is
the results key then inside here we have
the properties and then for example we
have the title field where we have the
rich text then this is an array so we
want the first
first item and then we have text and
then content and this is why we also
have the same schema here and then
similar for the publish date we have
published and then date and then the
start field so here published date and
start and also the URL so if we have a
look at the URL field so the first one
is always a little bit special this is
always the called title so don't get
confused with the title column and then
in here we have title type text and then
text content and then again this is what
we have here and this is how we can
query the database and print this and
like I said by default it can query
maximum 100 pages so let's learn how we
can do this if we have more pages so if
we have more pages then we change the
function slightly so we give it an
additional argument num pages and this
is none by default and then we check if
numpages is none we say get all is true
and then we Define the page size as 100
if we want to get all the pages and
otherwise this specific page size so 100
is the maximum number that we can get
with one API call and then we set this
as the page size payload and again send
the post request and extract the Json
data and the results and then we check
while data has more is true and we want
to get all the pages so if we have a
look at the Json file then you will find
that there is a next cursor key and a
has more key and this is true if we have
more results than we got here and in
that case we again send a payload with
the page size and now here we also
include the start cursor key and the
start cursor is data next cursor and
then again we send the post request
extract the data and then extend our
list with the data results and this is
how you can get all the results if you
have more than 100 now let's learn how
we can create a new page so let's create
a new helper function create page that
gets a data parameter and this is a
dictionary with all the fields then the
URL L endpoint is this one and as
payload we have to send the parent field
with the database ID and also the
properties field with the data that we
pass to the function and then we send a
post request to the URL with the headers
and the Json payload and print their
result and return this now we set up the
data so we have a URL a test URL a test
title and for the time I recommend to
convert this to the UTC time zone and
then you also need this in ISO format so
this will gives you a string in ISO
format and then you create your
dictionary with the same Fields with the
same schema that you see in here and
then we simply call this helper function
create page so let's run this and this
prints 200 so this was successful and if
we go to our notion page then it
automatically refreshes and you see we
now have a new item in our database now
let's learn how we can update data in
the database so let's create a helper
function update page that gets a page ID
and the data to update then here this is
the endpoint URL and the payload needs
the properties field with the data and
this time we send a patch request and
now let's set up the data to update so
we Define a page ID and for example you
find this in the results if you query
the database and then under results you
will find the ID for each result so for
each page this is the page ID and then
in this case let's update the title then
again you need to give this this schema
as a dictionary the same that you find
this in here under title title Rich Text
then this is an array so we give this a
list and then the fields text and
content and then we call up update page
so let's run this and again we get
status code 200 and here we see we have
the updated title so this worked and now
the last thing is how to delete a page
so for this let's copy this because this
is actually the same endpoint so let's
create a function delete page and this
doesn't need data and here instead of
sending properties we send archived and
set the parameter to true and then again
we can give it the page ID and then call
delete page and here we only pass the
page ID so let's run this and again we
get 200 and again here we see this was
deleted so this worked as well yeah and
this is all I wanted to show you in this
tutorial this is how you can work with
the notion API in Python I hope you
enjoyed this tutorial and then I hope to
see you in the next one bye
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.
Works with YouTube, Coursera, Udemy and more educational platforms
Get Instant Transcripts: Just Edit the Domain in Your Address Bar!
YouTube
←
→
↻
https://www.youtube.com/watch?v=UF8uR6Z6KLc
YoutubeToText
←
→
↻
https://youtubetotext.net/watch?v=UF8uR6Z6KLc