---
source_url: https://www.pubnub.com/docs/serverless/functions/functions-apis/uuid
title: UUID
updated_at: 2026-06-04T11:13:45.229Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# UUID

The [uuid](https://www.npmjs.com/package/uuid) library generates RFC-compliant Universally Unique Identifiers (UUIDs) in JavaScript.

The `uuid` module is available via the following `require()` statement:

```javascript
const {v3, v4, v5, validate, version} = require('uuid');
```

## Exposed constructors

Use these constructors in PubNub Functions:

* [v3](https://github.com/uuidjs/uuid?tab=readme-ov-file#uuidv3name-namespace-buffer-offset) — Generates a UUID from the MD5 hash of a namespace identifier (for example, a URL or DNS name) and a name.
* [v4](https://github.com/uuidjs/uuid?tab=readme-ov-file#uuidv4options-buffer-offset) — Generates a random UUID. This is the simplest option because it requires no inputs.
* [v5](https://github.com/uuidjs/uuid?tab=readme-ov-file#uuidv5name-namespace-buffer-offset) — Generates a UUID from a namespace and name using SHA-1 (similar to v3, which uses MD5).

## Exposed methods

Use these methods in your Function code:

* [validate](https://github.com/uuidjs/uuid#uuidvalidatestr) — Check whether a string is a valid UUID.
* [version](https://github.com/uuidjs/uuid#uuidversionstr) — Detect the RFC version of a UUID.

## Examples

### v3

```js
const { v3, validate, version } = require('uuid');

export default req => {
    const namespace = 'f8435596-ee6a-4a3d-a304-e3f0c7bb5321';
    const identifier = 'Hello World';
    const uuidV3 = v3(identifier, namespace);
    
    const expectedV3 = '51bfe3fe-6be8-3192-8ff2-b86ffd390fcc';

    return req.ok({
        v3: {
            uuid: uuidV3,
            valid: validate(uuidV3),
            version: version(uuidV3),
            expectedV3: expectedV3,
        }
    });
};
```

The following response is returned:

```js
{
    "v3": {
        "uuid": "51bfe3fe-6be8-3192-8ff2-b86ffd390fcc",
        "valid": true,
        "version": 3,
        "expectedV3": "51bfe3fe-6be8-3192-8ff2-b86ffd390fcc"
    }
}
```

### v4

```js
const {v4, validate, version} = require('uuid');

export default req => {
    const uuid = v4();
    return req.ok({
        v4: uuid,
        valid: validate(uuid),
        version: version(uuid),
    });
};
```

The following response is returned:

```js
{
    "v4": "ceae183a-8afa-42c4-9b8d-d9675bc6488c",
    "valid": true,
    "version": 4
}
```

### v5

```js
const { v5, validate, version } = require('uuid');

export default req => {
    const namespace = 'f8435596-ee6a-4a3d-a304-e3f0c7bb5321';
    const identifier = 'Hello World';
    const uuidV5 = v5(identifier, namespace);
    
    const expectedV5 = '6e54de1b-b0b5-55df-97c7-5487107d155b';

    return req.ok({
        v5: {
            uuid: uuidV5,
            valid: validate(uuidV5),
            version: version(uuidV5),
            expectedV5: expectedV5,
        }
    });
};
```

The following response is returned:

```js
{
    "v5": {
        "uuid": "6e54de1b-b0b5-55df-97c7-5487107d155b",
        "valid": true,
        "version": 5,
        "expectedV5": "6e54de1b-b0b5-55df-97c7-5487107d155b"
    }
}
```

:::note Functions support
Functions provides a rich set of tools. For help with situations not covered here, contact [PubNub Support](https://www.pubnub.com/docs/mailto:support@pubnub.com).
:::

## Terms in this document

* **PubNub** - PubNub is a real-time messaging platform that provides APIs and SDKs for building scalable applications. It handles the complex infrastructure of real-time communication, including: Message delivery and persistence, Presence detection, Access control, Push notifications, File sharing, Serverless processing with Functions and Events & Actions, Analytics and monitoring with BizOps Workspace, AI-powered insights with Illuminate.
