# Use Node-red in nodejs code for creating flows in code base

**URL:** <https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618>\
**Category:** General\
**Tags:** api\
**Created:** [2 December 2024 09:14 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618 "2024-12-02T09:14:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anshikamadan788](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anshikamadan788/32/96833_2.png) [@anshikamadan788](https://discourse.nodered.org/u/anshikamadan788)\
**Post date:** [2 December 2024 09:14 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618/1 "2024-12-02T09:14:11Z")

</div>

Hello,

I want to use Node.js in a way that allows me to do whatever I am doing through the GUI of the Node.js dashboard, like creating rule engines through flows. I want to do that through the code in Node. js-based applications. Is it possible to do so? If so, can anybody share any references to it?

thank you

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [2 December 2024 09:19 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618/2 "2024-12-02T09:19:32Z")

</div>

Welcome to the forums @anshikamadan788

This is a very broad topic.  
Your first step, is to fully understand the Node RED API(s)

From authenticating, to publishing/editing flows & nodes - the UI is doing all this via the API.

[https://nodered.org/docs/api/admin/](https://nodered.org/docs/api/admin/)

---

<div class="post-metadata">

**Author:** ![anshikamadan788](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anshikamadan788/32/96833_2.png) [@anshikamadan788](https://discourse.nodered.org/u/anshikamadan788)\
**Post date:** [2 December 2024 09:56 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618/3 "2024-12-02T09:56:35Z")

</div>

hi @marcus-j-davies  
thank you for the detail can you please be more specific about how can I create the same flows in code that I have created in GUI itself

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [2 December 2024 10:12 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618/4 "2024-12-02T10:12:40Z")

</div>

> [@anshikamadan788](#):
>
> please be more specific about how can I create the same flows in code that I have created in GUI itself

Refer to the link I provided, and refer to the `POST: /flow` method.

[https://nodered.org/docs/api/admin/methods/post/flow/](https://nodered.org/docs/api/admin/methods/post/flow/)

This API method is what is used, to publish your flow.  
The body of your request is expected to be an array of nodes.

```auto
{
  "id": "91ad451.f6e52b8",
  "label": "Flow tab 1",
  "nodes": [
    {
      "id": "123",
      "type": "inject",
      "x": 0,
      "y": 0,
      "z": "456",
      "wires": []
    }
  ],
  "configs": []
}

```

See also the types in the same link.  
[https://nodered.org/docs/api/admin/types](https://nodered.org/docs/api/admin/types)

---

<div class="post-metadata">

**Author:** ![anshikamadan788](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anshikamadan788/32/96833_2.png) [@anshikamadan788](https://discourse.nodered.org/u/anshikamadan788)\
**Post date:** [2 December 2024 11:16 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618/5 "2024-12-02T11:16:56Z")

</div>

Hi @marcus-j-davies,

I have gone through the docs, I just wanted to confirm that we will be passing the nodes same we made while creating the flow ....and just paste it as as same in the array right.

but how will that published flow be used as a API ?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [2 December 2024 11:23 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618/6 "2024-12-02T11:23:18Z")

</div>

> [@anshikamadan788](#):
>
> that we will be passing the nodes same we made while creating the flow

If you're **updating** an already deployed flow.

You `PUT` to `/flow/{id}` (where `id` is the tab/flow id)

[https://nodered.org/docs/api/admin/methods/put/flow/](https://nodered.org/docs/api/admin/methods/put/flow/)

`POST /flow`  
Adds a new tab

`PUT /flow/{id}`  
Updates a tab.

Im pretty sure, once you submit - its active immediately.

see all methods here: [Admin API Methods : Node-RED](https://nodered.org/docs/api/admin/methods/)

@knolleary  
Please do correct me, if this is not the case

---

<div class="post-metadata">

**Author:** ![anshikamadan788](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/anshikamadan788/32/96833_2.png) [@anshikamadan788](https://discourse.nodered.org/u/anshikamadan788)\
**Post date:** [2 December 2024 13:05 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618/7 "2024-12-02T13:05:18Z")

</div>

@marcus-j-davies  
I used the mentioned apis as per the admin apis ...to create the flow so the flow will the same as the one we created in GUI as we used ... so we will be using the same flow and pass in node and publish it but will it not be same as theGUI ?

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [3 December 2024 09:40 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618/8 "2024-12-03T09:40:16Z")

</div>

> [@anshikamadan788](#):
>
> but how will that published flow be used as a API ?

Are you referring to the use-case whereby you define an API as a flow and want to update that flow (and hence the API) on Node-RED?

For example, I want to build an API that returns the same value that I submit, this flow:

```auto
[{"id":"1bf50e3cd4595706","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"0835a666bda903a2","type":"http in","z":"1bf50e3cd4595706","name":"","url":"/myapi/v1/:value","method":"get","upload":false,"swaggerDoc":"","x":670,"y":562,"wires":[["33466762a8e9f546"]]},{"id":"33466762a8e9f546","type":"change","z":"1bf50e3cd4595706","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{ \"value\": $$.req.params.value }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":951,"y":533,"wires":[["5bd25c7207a95de1"]]},{"id":"5bd25c7207a95de1","type":"http response","z":"1bf50e3cd4595706","name":"","statusCode":"200","headers":{"content-type":"application/json"},"x":1192,"y":580,"wires":[]}]

```

It defines a http-in node with a path of `/myapi/v1/:value`, the response becomes a json:

```auto
prompt> curl http://mynoderedhost:1880/myapi/v1/dddd
{"value":"dddd"}

```

that for me is a flow that "defines an API" that can be accessed from external servers.

If I modify that flow and want to update Node-RED with it, then I have to use the admin API to do a `PUT /flow/1bf50e3cd4595706` (since the id of the flow is `1bf50e3cd4595706`). So the Node-RED instance is the host of the API and needs updating if the API should change.

That would update the flow which defines the API endpoint. It would also restart NodeRED so the change should be immediate.

---

<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:** [3 March 2025 09:41 UTC](https://discourse.nodered.org/t/use-node-red-in-nodejs-code-for-creating-flows-in-code-base/93618/9 "2025-03-03T09:41:01Z")

</div>

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