First time using cURL - Notion.so API

Hello!

I'm trying to do my first API cURL, following guidance from the API documentation for Notion.so.

Combined with some Googling, I'm just struggling to get this going through Node-RED. Could anyone provide some guidance? I would really appreciate it.

In node-red you would convert the curl to a http request, or you could send a curl using the exec node and the OS.
Here is an example of the curl from the docs you posted, used in a http request node.
You need to add you db id in the inject payload JSON and your api key in the http request node, delete all asterisks and everything inbetween and replace with your id and key.

[{"id":"5515489234ec93df","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"parent\":{\"database_id\":\"****your_db_id****\"},\"properties\":{\"title\":{\"title\":[{\"text\":{\"content\":\"Yurts in Big Sur, California\"}}]}}}","payloadType":"json","x":150,"y":3000,"wires":[["912704151f038109"]]},{"id":"912704151f038109","type":"http request","z":"da8a6ef0b3c9a5c8","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://api.notion.com/v1/pages","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"other","keyValue":"Authorization","valueType":"other","valueValue":"Bearer ****your_api_key*****"},{"keyType":"other","keyValue":"Content-Type","valueType":"other","valueValue":"application/json"},{"keyType":"other","keyValue":"Notion-Version","valueType":"other","valueValue":"2021-08-16"}],"x":330,"y":3020,"wires":[["b1a470fe0ca38d66"]]},{"id":"b1a470fe0ca38d66","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 224","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":2980,"wires":[]}]

[edit] fixed typo in Authorization header.

1 Like

You are AWESOME! Thank you so much. I was able to change the couple of things needed and it worked immediately. This gives me a great jumping off point for my project.

Thank you very much for your time! You made my day.

EDIT: Now if I'm being picky here, I'm actually using Node-RED with Home Assistant. I was hoping to be able to insert the state of a sensor, something like {{states.sensor.my_sensor}} but it's outputting this as raw text. Do you know how I would insert that?

Here is an example of an incoming sensor value, it is in the inject node as sensor_value

[{"id":"5515489234ec93df","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"sensor_value","v":"12","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":70,"y":2940,"wires":[["a96b7eed97e8e573"]]},{"id":"a96b7eed97e8e573","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t   \"parent\":{\"database_id\":\"****your_db_id****\"},\t   \"properties\":{\t       \"title\":{\t           \"title\":[\t               {\t                   \"text\":{\t                       \"content\": $$.sensor_value\t                   }\t               }\t           ]\t       }\t   }\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":2980,"wires":[["912704151f038109"]]},{"id":"912704151f038109","type":"http request","z":"da8a6ef0b3c9a5c8","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://api.notion.com/v1/pages","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"other","keyValue":"Authorization","valueType":"other","valueValue":"Bearer ****your_api_key****"},{"keyType":"other","keyValue":"Content-Type","valueType":"other","valueValue":"application/json"},{"keyType":"other","keyValue":"Notion-Version","valueType":"other","valueValue":"2021-08-16"}],"x":370,"y":3040,"wires":[["b1a470fe0ca38d66"]]},{"id":"b1a470fe0ca38d66","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 224","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":2980,"wires":[]}]

Now in the original example i have added it to the payload json, but instead of using JSON in the change node i have switched to JSONata. I have added the value using $$.sensor_value.

You can do the same with your incoming data.

In future if you want a more related example to what you are attempting, it is best to supply examples flows with data examples added or pasted separately in text format.

I don't use HA but i believe you can use JSONata in most HA nodes.

1 Like

This worked great and, once again, I have some working nodes to go off of and experiment with!

Since you're so helpful and knowledgable, I was attempting to adjust a property field in the flow and having some trouble with the indentations/nesting, I believe. Would you be able to help me with the JSON formatting like in this example:

      "Grocery item": {
        "type": "title",
        "title": [{ "type": "text", "text": { "content": "Tomatoes" } }]

I think I am getting lost in the brackets.

In the Json editor there are vertical lines, these are there to help


The closing bracket is inline with the starting brackets line first char or bracket.

I believe yours has a missing } at end, difficult to tell though as you have not posted a full syntaxually correct JSON., rather a portion of one.

Close but you are missing the final }

Here is the flow I have been experimenting with, but I can't seem to make the editor happy. I'm still very new to all of this, so I do apologize.

[{"id":"e22ea111d6ef50b4","type":"change","z":"81f0b9e0.caa338","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t   \"parent\":{\t   \"database_id\":\"**DATABASE_ID**\"\t},\t   \"properties\":{\t   \"Grocery item\": {\t       \"type\": \"title\",\t       \"title\": [\t           {\t               \"type\": \"text\",\t               \"text\": { \"content\": \"Tomatoes\" } \t           }\t       ]\t         \"title\":{\t           \"title\":[\t               {\t                   \"text\":{\t                       \"content\": $$.sensor_value\t                   }\t               }\t           ]\t       }\t   }\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":360,"wires":[["4a8ce503cfc04df1"]]},{"id":"4a8ce503cfc04df1","type":"http request","z":"81f0b9e0.caa338","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://api.notion.com/v1/pages","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"other","keyValue":"Authorization","valueType":"other","valueValue":"Bearer **API_KEY**"},{"keyType":"other","keyValue":"Content-Type","valueType":"other","valueValue":"application/json"},{"keyType":"other","keyValue":"Notion-Version","valueType":"other","valueValue":"2021-08-16"}],"x":750,"y":360,"wires":[["3ae014fa9b9ba61c"]]},{"id":"3ae014fa9b9ba61c","type":"debug","z":"81f0b9e0.caa338","name":"debug 224","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":360,"wires":[]},{"id":"8b202a4b07d5c862","type":"inject","z":"81f0b9e0.caa338","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":360,"wires":[["dad5990bebb00cfe"]]},{"id":"dad5990bebb00cfe","type":"api-current-state","z":"81f0b9e0.caa338","name":"Number of Restarts","server":"b2e380c6.9da53","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"counter.homeassistant_restarts","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"sensor_value","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":350,"y":360,"wires":[["e22ea111d6ef50b4"]]},{"id":"b2e380c6.9da53","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

As said I don't run HA, so can not run your flow. Can you give a debug output of the HA node. In a text format, use the copy value button in the debug sidebar. It will appear when you hove mouse ovee property name. Copy whole message.

The change node is where the error is showing. Here's a flow with the HA node removed. I can re-add it now that I understand how to use the $$.sensor_value you pointed out.

[{"id":"e22ea111d6ef50b4","type":"change","z":"81f0b9e0.caa338","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t   \"parent\":{\t   \"database_id\":\"**DATABASE_ID**\"\t},\t   \"properties\":{\t   \"Grocery item\": {\t       \"type\": \"title\",\t       \"title\": [\t           {\t               \"type\": \"text\",\t               \"text\": { \"content\": \"Tomatoes\" } \t           }\t       ]\t         \"title\":{\t           \"title\":[\t               {\t                   \"text\":{\t                       \"content\": $$.sensor_value\t                   }\t               }\t           ]\t       }\t   }\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":360,"wires":[["4a8ce503cfc04df1"]]},{"id":"4a8ce503cfc04df1","type":"http request","z":"81f0b9e0.caa338","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://api.notion.com/v1/pages","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"other","keyValue":"Authorization","valueType":"other","valueValue":"Bearer **API_KEY**"},{"keyType":"other","keyValue":"Content-Type","valueType":"other","valueValue":"application/json"},{"keyType":"other","keyValue":"Notion-Version","valueType":"other","valueValue":"2021-08-16"}],"x":750,"y":360,"wires":[["3ae014fa9b9ba61c"]]},{"id":"3ae014fa9b9ba61c","type":"debug","z":"81f0b9e0.caa338","name":"debug 224","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":360,"wires":[]},{"id":"8b202a4b07d5c862","type":"inject","z":"81f0b9e0.caa338","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":360,"wires":[["e22ea111d6ef50b4"]]}]

When I attempt to Deploy, Node-RED gives the following error:

The workspace contains some nodes that are not properly configured:

[Testing] set msg.payload (change)

Yes I know but it is helpful to have all the information.

If some one asks for info, it is best to answer the question, as they are kind enough to help in first place.

The change node has a missing } and a ,, to close grocery and open title

Not sure but this maybe correct.

[{"id":"e22ea111d6ef50b4","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t    \"parent\":{\t        \"database_id\":\"**DATABASE_ID**\"\t    },\t   \"properties\":{\t        \"Grocery item\": {\t            \"type\": \"title\",\t            \"title\": [\t                {\t                    \"type\": \"text\",\t                    \"text\": { \"content\": \"Tomatoes\" } \t                }\t            ]\t        },\t        \"title\":{\t            \"title\":[\t                {\t                    \"text\":{\t                        \"content\": $$.sensor_value\t                    }\t                }\t            ]\t        }\t    }\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":3180,"wires":[["4a8ce503cfc04df1"]]}]

I understand, sorry about that. The debug output from my HA node shows:
1672871879673

I am also seeing the following error now in the request node:

{"object":"error","status":400,"code":"validation_error","message":"Grocery Item is expected to be rich_text."}

Your change did fix the error within the change node. Just chasing a different problem now, it seems.

I would expect from the error message and Docs that content value of the text object is not text, add a debug after the change node and see what you are sending.

The change node is sending this:

{"parent":{"database_id":"*DATABASE_ID*"},"properties":{"Grocery Item":{"type":"title","title":[{"type":"text","text":{"content":"Tomatoes"}}]},"title":{"title":[{"text":{"content":"2"}}]}}}

I am only guessing now, as I do not have anything to test. I think the Grocery Item is expected to be of type rich_text and not title. I think you are going to need someone using the api, or ask on there slack feed.

I will do some experimenting! You got me much further along than I expected. Thank you so much. I genuinely appreciate your time!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.