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

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

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/

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

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

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.

{
  "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

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 ?

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/

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

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

@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 ?

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:

[{"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:

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.