-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion_flask_app.py
53 lines (44 loc) · 1.47 KB
/
notion_flask_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import json
import os
from flask import Flask, request, jsonify
import requests
import json
import os
app = Flask(__name__)
# Configura tus credenciales y detalles de la base de datos
NOTION_TOKEN = os.environ.get('NOTION_TOKEN')
DATABASE_ID = os.environ.get('DATABASE_ID')
# URL de la API de Notion
url = "https://api.notion.com/v1/pages"
# Cabeceras para la solicitud
headers = {
"Authorization": f"Bearer {NOTION_TOKEN}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28" # Asegúrate de usar la versión más reciente
}
@app.route('/add_record', methods=['POST'])
def add_record_to_notion():
data = request.json
if not data or 'title' not in data:
return jsonify({"error": "Se requiere el campo 'title'"}), 400
notion_data = {
"parent": {"database_id": DATABASE_ID},
"properties": {
"Name": {
"title": [
{
"text": {
"content": data['title']
}
}
]
}
}
}
response = requests.post(url, headers=headers, json=notion_data)
if response.status_code == 200:
return jsonify({"message": "Registro agregado exitosamente a Notion"}), 200
else:
return jsonify({"error": f"Error al agregar registro: {response.status_code}", "details": response.text}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)