Build Your Own Amazing Integrations with the Notion API – Part 1

Example Heroku Files

#requirements.txt
flask
flask_restful
requests
gunicorn
#runtime.txt
python-3.7.10
#Procfile
web: gunicorn app:app

Example “Hello World” Code

#app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello World!'
if __name__ == '__main__':
    app.run()

Example Code to Create New Page with Custom Title

#api.py
import datetime
import requests	
from flask import Flask, request, jsonify #import main Flask class and request object
from flask_restful import Resource, Api
#Define Variables
apikey = '[[NOTION API KEY]]'
token = "Bearer " + apikey
#BUJO DB ID
databaseid = '[[TARGET API KEY]]'
app = Flask(__name__) #create the Flask app
#Create the new title with like 19.05 - Wed
x = datetime.date.today()
pagetitle = x.strftime('%d.%m - %a')
@app.route('/newpage', methods=['POST']) #GET requests will be blocked
def newpage():
    #This section does the call to Notion's API
    headers = {
        'Authorization': token,
        'Content-Type': 'application/json',
        'Notion-Version': '2021-05-13',
    }
    data = '{ "parent": { "database_id": "' + databaseid + '" }, "properties": { "Name": { "title": [ { "text": { "content": "' + pagetitle +'" } } ] } } }'
    response = requests.post('https://api.notion.com/v1/pages', headers=headers, data=data)
    jsonResponse = response.json()
    pageid = jsonResponse["id"]
    pageid = pageid.replace('-', '')
    #print (pageid)
    newurl = "notion://www.notion.so/" + pageid
    #print (newurl)
    return (str(newurl))
if __name__ == '__main__':
    app.debug = True
    app.run(debug=True, port=5000, host='0.0.0.0') #run app in debug mode on port 5000