How can I programmatically generate nodes in a flow?

I've done a search and the answer is pretty much no but in the spirit of the truly lazy programmer I will again ask! :wink:

I know you must hate this question but I'm interested in adding a set of nodes (mqtt topic and a button). I can build a json template file of the 3 nodes from an existing node set. Modify the json so it's easy to regexp but the tricky part is adding it to an existing flow (or a new flow if that's easier) then deploying it, all programmatically.

Are there any APIs for the node editor that would allow me do to this?

Perhaps just a way to programmatically add a new flow to the existing flows file and then manually deploy?

I wonder if anything on this page may help.
https://nodered.org/docs/api/

1 Like

Thanks, that is useful. I'm not it has what I need but I'll see what I can do with the information.

This looks like I can add flow

docs -> api -> admin -> methods -> add flow

POST /flow

I'll give this a try and post a bash script with an example.

I can successfully gets the flows, there I see a flow with "id": "c5d7e07d.86e21". If I run the commands that follow, I get 404 (code not found - ???).

ID="c5d7e07d.86e21" # This is the "Flow 1" flow from the get /flows
API=flow/:${ID} ; echo ${API}
TOKEN=$(curl -s http://localhost:1880/auth/token --data 'client_id=node-red-admin&grant_type=password&scope=*&username=admin&password=Password' | jq -r '.access_token')
curl -v -H 'content-type: application/json' -H "Authorization: Bearer ${TOKEN}"  http://localhost:1880/${API} -w '\nHTTP_RESPONSE: %{http_code}\n'

So what am I doing wrong? And before anyone asks, no Password isn't the actual password. :wink:

i dont know curl but 404 page not found means you are requesting to the wrong endpoint
is the colon : symbol included as part of the API variable ? if yes .. try to remove it

I thought I tried that but I get 200 now (yea!). So not : in the API.

Thanks

Still need to generate the flow, which is why this doesn't have a solution yet.

  1. export a simple flow - a selection of nodes (i used an Inject and a Debug node)
  2. copy its json - because obviously i wasnt gonna sit and type all that in :wink:
  3. based on the docs constracted the body of the request and pasted in nodes property the json from step 2
  4. deleted my test flow with the Editor so there wont be a clash of ids / names etc
  5. tested the POST with POSTMAN software
  6. translated the request with it to Curl, for you to have fun
curl --location --request POST 'http://192.168.0.7:1880/flow/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "id": "91ad451.f6e52b8",
    "label": "Sheet 1",
    "nodes": [
        {
            "id": "59bf67591ceff3bd",
            "type": "inject",
            "z": "5ae48088f8ce401d",
            "name": "",
            "props": [
                {
                    "p": "payload"
                },
                {
                    "p": "topic",
                    "vt": "str"
                }
            ],
            "repeat": "5",
            "crontab": "",
            "once": false,
            "onceDelay": 0.1,
            "topic": "",
            "payloadType": "date",
            "x": 290,
            "y": 140,
            "wires": [
                [
                    "7b4c39e88a441a69"
                ]
            ]
        },
        {
            "id": "7b4c39e88a441a69",
            "type": "debug",
            "z": "5ae48088f8ce401d",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "false",
            "statusVal": "",
            "statusType": "auto",
            "x": 490,
            "y": 140,
            "wires": []
        }
    ],
    "configs": []
}'
1 Like

One small change:

/flow/
tp
/flow

Here's mine with the Token included:

TOKEN=$(curl -s http://localhost:1880/auth/token --data 'client_id=node-red-admin&grant_type=password&scope=*&username=admin&password=Password' | jq -r '.access_token')
curl --location --request POST 'http://localhost:1880/flow' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer ${TOKEN}" \
--header "Node-RED-API-Version: v2" \
--data-raw '{
    "id": "91ad451.f6e52b8",
    "label": "Flow 3",
    "nodes": [
        {
            "id": "59bf67591ceff3bd",
            "type": "inject",
            "z": "5ae48088f8ce401d",
            "name": "",
            "props": [
                {
                    "p": "payload"
                },
                {
                    "p": "topic",
                    "vt": "str"
                }
            ],
            "repeat": "5",
            "crontab": "",
            "once": false,
            "onceDelay": 0.1,
            "topic": "",
            "payloadType": "date",
            "x": 290,
            "y": 140,
            "wires": [
                [
                    "7b4c39e88a441a69"
                ]
            ]
        },
        {
            "id": "7b4c39e88a441a69",
            "type": "debug",
            "z": "5ae48088f8ce401d",
            "name": "",
            "active": true,
            "tosidebar": true,
            "console": false,
            "tostatus": false,
            "complete": "false",
            "statusVal": "",
            "statusType": "auto",
            "x": 490,
            "y": 140,
            "wires": []
        }
    ],
    "configs": []
}' \
-w '\nHTTP_RESPONSE: %{http_code}\n'
1 Like

I'll work on figuring out the spacing and a few other details so I can later update the flow. I think there might also be a way to deploy the new flow via the API but I need to think about that.
Thanks

Doesnt it automatically Deploy when you do the POST ?
it does for me

Let me try it without the NR Editor open.

Initially, I got a whole lot of weird messages . I didn't see one that it was deployed. Just that it changed and I could review, merge or something else.

The fact that you got those messages means that it has been deployed. The editor notices that the flow it has is out of date and is asking you what to do about it.

Excellent! My evil plan to take over the world is now coming to fruition! Mwu-ha-ha-ha ... :wink:

Cool, I see that in the main flows is the information about various configurations (such as MQTT brokers) and I see the Tab ID, the node ID and the wires (next node ID). I found searching the forum how to create a new ID:

(1 + Math.random () * 4294967295) .toString (16)

As soon as I figure out the size of the nodes I can fit it to the x+y and add and delete as needed.

Huge thanks

1 Like

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