26
loading...
This website collects cookies to deliver better user experience
name, genre, and completed
.Settings => Integrations => Develop your own integrations
. After that, you specify a name for your integration and click submit. In the end, you will get to this screen:Share
button, press Invite
, and then you can select the integration that you just created from a list:https://www.notion.so/19f00145217c4437afb06cfdbb2ad994?v=51972570a71c4b8b9b1feeec01e87eb5
. The database id is 19f00145217c4437afb06cfdbb2ad994
.import json
import requests
token = 'secret_from_notion_integration'
database_id = 'database_id_from_link'
getMovies
.def getMovies():
url = f'https://api.notion.com/v1/databases/{database_id}/query'
r = requests.post(url, headers={
"Authorization": f"Bearer {token}",
"Notion-Version": "2021-08-16"
})
result_dict = r.json()
movie_list_result = result_dict['results']
print(movie_list_result)
def mapNotionResultToMovie(result):
# you can print result here and check the format of the answer.
movie_id = result['id']
properties = result['properties']
genre = properties['Genre']['rich_text'][0]['text']['content']
name = properties['Name']['title'][0]['text']['content']
completed = properties['Completed']['checkbox']
return {
'genre': genre,
'name': name,
'completed': completed,
'movie_id': movie_id
}
getMovies
. The function should contain the following code:def getMovies():
url = f'https://api.notion.com/v1/databases/{database_id}/query'
r = requests.post(url, headers={
"Authorization": f"Bearer {token}",
"Notion-Version": "2021-08-16"
})
result_dict = r.json()
movie_list_result = result_dict['results']
movies = []
for movie in movie_list_result:
movie_dict = mapNotionResultToMovie(movie)
movies.append(movie_dict)
return movies
movies = getMovies()
# json.dumps is used to pretty print a dictionary
print('Movie list:', json.dumps(movies, indent=2))
createMovie
. For this one, we will need to construct a payload similar to the response from getMovies
.def createMovie(name, genre, completed=False):
url = f'https://api.notion.com/v1/pages'
payload = {
"parent": {
"database_id": database_id
},
"properties": {
"Name": {
"title": [
{
"text": {
"content": name
}
}
]
},
"Genre": {
"rich_text": [
{
"text": {
"content": genre
}
}
]
},
"Completed": {
"checkbox": completed
}
}}
r = requests.post(url, headers={
"Authorization": f"Bearer {token}",
"Notion-Version": "2021-08-16",
"Content-Type": "application/json"
}, data=json.dumps(payload))
movie = mapNotionResultToMovie(r.json())
return movie
createdMovie = createMovie('Movie1', 'Genre1', False)
print('Created Movie:', json.dumps(createdMovie, indent=2))
movieId
. We create the payload in a similar way, but we also add the movieId
in the URL.def updateMovie(movieId, movie):
url = f'https://api.notion.com/v1/pages/{movieId}'
payload = {
"properties": {
"Name": {
"title": [
{
"text": {
"content": movie['name']
}
}
]
},
"Genre": {
"rich_text": [
{
"text": {
"content": movie['genre']
}
}
]
},
"Completed": {
"checkbox": movie['completed']
}
}}
r = requests.patch(url, headers={
"Authorization": f"Bearer {token}",
"Notion-Version": "2021-08-16",
"Content-Type": "application/json"
}, data=json.dumps(payload))
movie = mapNotionResultToMovie(r.json())
return movie
getMovies()
. In that response, you can get a movieId
(in the ex: fdd0fa87-4729-43e6-ae3f-823d48b382ee
) and use it like this:updatedMovie = updateMovie('fdd0fa87-4729-43e6-ae3f-823d48b382ee', {
'name': 'UpdatedMovie1',
'genre': 'UpdatedGenre1',
'completed': True
})
print('Update movie', json.dumps(updatedMovie, indent=2))
deleteMovie
. In Notion, pages are using a property called archived
. If we set that to true, then the page will be deleted. So, this function will use the update endpoint in order to change the value of the 'archived' boolean.def deleteMovie(movieId):
url = f'https://api.notion.com/v1/pages/{movieId}'
payload = {
"archived": True
}
r = requests.patch(url, headers={
"Authorization": f"Bearer {token}",
"Notion-Version": "2021-08-16",
"Content-Type": "application/json"
}, data=json.dumps(payload))
movieId
and if you check the database in Notion, that specific row will be deleted.deleteMovie('a19e538d-10cc-40ec-91bb-f7237c93e428')