# How can I programmatically generate nodes in a flow?

**URL:** https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267
**Category:** General
**Created:** [5 November 2021 15:54 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267 "2021-11-05T15:54:15Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [5 November 2021 15:54 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/1 "2021-11-05T15:54:15Z")

</div>

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! 😉

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?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [5 November 2021 15:58 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/2 "2021-11-05T15:58:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [5 November 2021 16:42 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/3 "2021-11-05T16:42:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [5 November 2021 16:54 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/4 "2021-11-05T16:54:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [5 November 2021 23:06 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/5 "2021-11-05T23:06:17Z")

</div>

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 - ???).

```auto
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. 😉

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [6 November 2021 00:09 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/6 "2021-11-06T00:09:57Z")

</div>

> [@ncherry](#):
>
> `API=flow/:${ID}`

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

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [6 November 2021 02:47 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/7 "2021-11-06T02:47:52Z")

</div>

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

Thanks

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [6 November 2021 02:48 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/8 "2021-11-06T02:48:42Z")

</div>

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

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [6 November 2021 03:16 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/9 "2021-11-06T03:16:48Z")

</div>

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 😉
3. based on the [docs](https://nodered.org/docs/api/admin/methods/post/flow/) 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

```auto
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": []
}'

```

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [6 November 2021 11:32 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/10 "2021-11-06T11:32:19Z")

</div>

One small change:

/flow/  
tp  
/flow

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [6 November 2021 11:33 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/11 "2021-11-06T11:33:23Z")

</div>

Here's mine with the Token included:

```auto
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'

```

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [6 November 2021 11:36 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/12 "2021-11-06T11:36:28Z")

</div>

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

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [6 November 2021 11:45 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/13 "2021-11-06T11:45:26Z")

</div>

> [@ncherry](#):
>
> I think there might also be a way to deploy the new flow via the API

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

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [7 November 2021 00:55 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/14 "2021-11-07T00:55:01Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [7 November 2021 07:15 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/15 "2021-11-07T07:15:26Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)
#### Post date: [7 November 2021 14:29 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/16 "2021-11-07T14:29:43Z")

</div>

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

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:

```auto
(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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [21 November 2021 14:29 UTC](https://discourse.nodered.org/t/how-can-i-programmatically-generate-nodes-in-a-flow/53267/17 "2021-11-21T14:29:56Z")

</div>

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