Question

Using randomUUID() in Zapier to generate UUID

  • 20 July 2022
  • 1 reply
  • 1078 views

I'm using Zapier's code module to write some Nodejs to generate a UUID.

I know that I can use node's crypto "library" because I've used it in the past to hash some text (MD5) to send to an API endpoint. I also know that I can use the crypto function randomInt.

For example, the following will correctly without errors return a number in the stated range:

```
const crypto = require('crypto'); 
let num = crypto.randomInt(1, 7);
output = [{id: num}];
```
When I try to use the randomUUID function (documentation), like the following, I get the error "TypeError: crypto.randomUUID is not a function" from Zapier. Their logs are showing  that the code step cannot find `module 'uuid'`:

```
const crypto = require('crypto'); 
let uuid = crypto.randomUUID();
output = [{id: uuid}];
```
I almost gave up there, but tried one last weird thing:

```
const crypto = require('crypto'); 
let uuid = crypto.randomUUID; //changed this
output = [{id: uuid}];
```
Note that I removed the `()` and this didn't throw an error and returned something like:

`LT6TvivKgpFu5Yk3OvQmti1Hq1aNy5ZM`

That looks a lot like a proper UUID since it has the right number of characters, but not the expected hyphens like (for example) `88368f2a-d5db-47d8-a05f-534fab0a0045`

So, two questions:

 - Why is Zapier saying that `randomUUID()` is not a function? Does it not support this or am I coding something weirdly wrong or not requiring something needed? Is there a different way to do this?
 - When I use `randomUUID` (no `()`) is this actually a reliable UUID?

 

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Userlevel 7
Badge +12

Hi @jonmrich 

I do not think you can require any libraries in a Zapier code block. You can however, require from developer.zapier.com so i would suggest you head there and build a custom action to generate a UUID. I have done this in the past using uuidv4. (be sure to use z.require though) 

if you throw some console logs into your code you will see that UUID is null and what you are getting in your response is a Zapier generated id. 

You would get the same output just returning {};