# Admin API flow creation with duplicate IDs

**URL:** <https://discourse.nodered.org/t/admin-api-flow-creation-with-duplicate-ids/65261>\
**Category:** Feature Requests\
**Created:** [18 July 2022 11:19 UTC](https://discourse.nodered.org/t/admin-api-flow-creation-with-duplicate-ids/65261 "2022-07-18T11:19:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurago-dev](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@kurago-dev](https://discourse.nodered.org/u/kurago-dev)\
**Post date:** [18 July 2022 11:19 UTC](https://discourse.nodered.org/t/admin-api-flow-creation-with-duplicate-ids/65261/1 "2022-07-18T11:19:36Z")

</div>

Hi,

When creating flows via the [POST method on the Admin API](https://nodered.org/docs/api/admin/methods/post/flow/) (`POST /flow`), if the flow has a node with an ID already in use, the method will fail. As the docs state:

> All of the nodes in the flow must have unique `id` properties. The request will rejected if any of the `id` s are already in use.

However, when importing a flow via the GUI and using the clipboard import, an option to generate IDs is proposed to the user:

> Some of the nodes you are importing already exist in your workspace.  
> Select which nodes to import and whether to replace the existing nodes, or to import a copy of them.

That's implemented via the `importOptions.generateIds` configuration in the [`clipboard.js` file](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/editor-client/src/js/ui/clipboard.js#L1012).

**Proposal:** Would it be possible to add an option (for example in the headers) to generate IDs if duplicates are found?

---

<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:** [18 July 2022 23:29 UTC](https://discourse.nodered.org/t/admin-api-flow-creation-with-duplicate-ids/65261/2 "2022-07-18T23:29:23Z")

</div>

As a workaround you may find this post interesting

> [@How to generate unique node ID when creating a flow via node-red API](https://discourse.nodered.org/t/how-to-generate-unique-node-id-when-creating-a-flow-via-node-red-api/18237/2):
>
> Hi Charlotte, I had the same problem as you. I have found that IDs are generated with this JScript function (1 + Math.random () \* 4294967295) .toString (16); You can find it on file @node-red/util/lib/util.js, function generateId() I hope it is useful for you Ciao

Also the link to the Node-red util file with the code that generates the ids. ([LINK](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/util/lib/util.js#L44))  
You may implement the same method to give the nodes a unique id before the POST ?

**[EDIT]**  
Why would you want to POST a flow with duplicate node ids which in my mind means a duplicate flow ?!  
Maybe you should be using `PUT /flow/:id` that updates a Flow ?

---

<div class="post-metadata">

**Author:** ![kurago-dev](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@kurago-dev](https://discourse.nodered.org/u/kurago-dev)\
**Post date:** [19 July 2022 06:41 UTC](https://discourse.nodered.org/t/admin-api-flow-creation-with-duplicate-ids/65261/3 "2022-07-19T06:41:39Z")

</div>

Hi @UnborN,

Yeah, I've seen that workaround and it's an option, but I think that it'd be great to also be able to create IDs automatically like when using the Web UI.

Regarding the _why_, imagine a Flow where I am reading data from an endpoint and applying some logic to it. Then, the same flow is going to be used for 5 different endpoints. In this scenario, a template flow can be used where only the endpoint has to be changed and the IDs are already defined. When using the **POST** , one could create the 5 flows without dealing with with duplicate IDs externally.

Hope that makes sense.

---

<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:** [17 September 2022 06:42 UTC](https://discourse.nodered.org/t/admin-api-flow-creation-with-duplicate-ids/65261/4 "2022-09-17T06:42:22Z")

</div>

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