Function to generate UUID or similar

Hi

Is there any way to via a function or similar generate a unique id string similar to UUID 4 or another way to create a unique id?

Kind regards
Mattias

1 Like

One simple solution could be this:

{{Math.floor(Math.random()*999999999)}}

Depending on how long you want it you can simply add or remove a few 9.

If you want to avoid the chance of a collision and alphanumerical you can combine the current date and a random string like this:

{{(+new Date).toString(36).slice(-5) + Math.random().toString(36).substr(2, 5)}}
1 Like

Thanks it will work for now, but would it possible to add function for a full UUID128-bit in the future?

Should i add it the feature request?

Kind regards
Mattias

2 Likes

Yes please add it as a feature request.

1 Like

As the uuid npm-package is now by default a dependency of n8n as it gets used to generate unique paths for the webhook, it is now possible to create uuids in the Function-Node for example like this:

const uuid = require('uuid');
items[0].json.uuid = uuid.v4();
return items;

That will however only work if the following environment variable got set:

NODE_FUNCTION_ALLOW_EXTERNAL=uuid
4 Likes

Thanks for sharing this, @jan. We just used this for our hackathon project :slight_smile:

3 Likes

and here is me just using this: Online UUID Generator Tool with an HTTP request node.

6 Likes

We’re also planning on adding a convenience function to do this from expressions.

3 Likes

Looks like everyone is using a different approach. :open_mouth:

Not sure if this could help, but here is the UuidV4 implementation in PHP

1 Like

Would any of the examples work in the cloud hosted version?

Hey @Philip_Wiggins,

Using the api with an http request node would do the job.

You can also use the crypto module on n8n cloud like so to generate a UUID:

Result:

No external API required :slight_smile:

1 Like

You can simply use the crypto node:

Or does that not exist in the cloud version?

3 Likes

It is in there, I probably should have known this :joy:

2 Likes

It does actually and I constantly forget about it. That’s definitely the winner here :smiley:

3 Likes

sorry to necro this post - @MutedJam is there a list of available dependencies in the cloud version?

Hi @A_B, I don’t have a definitive list right now but I believe it comes down to crypto and moment.js (and possibly request-promise, though iirc there was talk of removing this). Definitely not a lot, and there’s also no guarantee these will remain in place.

So if you need to use any npm modules self-hosting would be the way to go.

1 Like

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