---
source_url: https://www.pubnub.com/docs/serverless-sdk/modules-and-libraries/ugc
title: ugc
updated_at: 2026-09-30T07:20:08.000Z
---

# ugc

## Documentation index

To discover more PubNub resources:

1. Fetch [PubNub's llms.txt](https://www.pubnub.com/llms-full.txt) for a list of available pages in Markdown format.
2. Identify relevant URLs from that index.
3. Fetch the target pages.

Do not assume a path exists, always check the index first.

The `ugc` built-in module is available only inside a PubNub Function. It calls an [Auto Moderation](https://www.pubnub.com/docs/message-processing/moderation/configure-auto-moderation.md) configuration, identified by `configId`, to moderate a message.

```javascript
const ugc = require('ugc');
```

## moderateMessage

```typescript
function moderateMessage(req: ModerateMessageRequest): Promise<ModerateMessageResponse>
```

### ModerateMessageRequest

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `configId` | String | Yes | UUID of the Auto Moderation configuration created in BizOps Workspace. |
| `message` | JSON | Yes | Full publish body. Policies read the top-level `text` field by default. |
| `channel` | String | Yes | Channel the message publishes to. |
| `userId` | String | Yes | User ID (the `uuid` parameter) of the publisher. |
| `meta` | JSON | No | Optional metadata as custom key-value pairs. Functions receive this as a stringified JSON. Parse it before use. |

### ModerateMessageResponse

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `moderationId` | String | Yes | Unique ID for auditing and correlation. |
| `flagged` | Boolean | Yes | `true` if any moderation rule triggered. |
| `actions` | String[] | Yes | Configuration-defined actions, for example `["block", "wordMasked", "report"]`. |
| `categories` | `Record<string, CategoryResult>` | Yes | Per-category results. |
| `transform` | Object | No | Suggested changes to apply. May be empty. |
| `transform.message` | JSON | No | Message with masks or edits applied. |
| `transform.meta` | JSON | No | Meta augmented with moderation fields, for example a report identifier. |

`CategoryResult`

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `flagged` | Boolean | Yes | Whether this category triggered. |
| `details` | Object | No | Category-specific details. |
| `details.maskedWords` | String[] | No | Present for word-list violations. |

```javascript
export default (request) => {
    const ugc = require('ugc');
    const metaFromClient = typeof request.params.meta === 'string' ? JSON.parse(request.params.meta) : request.params.meta;

    return ugc.moderateMessage({
        configId: '<YOUR_MODERATION_CONFIG_ID>',
        message: request.message,
        userId: request.params.uuid,
        channel: request.channels[0],
        meta: metaFromClient,
    }).then((res) => {
        if (res.flagged && res.actions.includes('block')) {
            request.status = 403;
            return request.abort('Flagged');
        }
        if (res.transform?.message) request.message = res.transform.message;
        return request.ok();
    }).catch((err) => {
        console.log('Moderation error:', err);
        return request.ok();
    });
};
```

### Validation errors

| Field | Condition | Error message |
| --- | --- | --- |
| `configId` | Missing | `configId` must be provided |
| `message` | Undefined or null | `message` must be provided |
| `channel` | Missing or not a string | `channel` must be provided and must be a string |
| `userId` | Missing or not a string | `userId` must be provided and must be a string |

### Service and transport errors

| Scenario | Behavior | Notes |
| --- | --- | --- |
| Moderation API returns a non-2xx response | Promise resolves with the parsed error body when available | Built-in retries, up to 3 total attempts |
| Network or transport failure | Promise rejects | Counts toward the XHR-per-execution limit |

Each channel supports one Before Publish or Fire Function per keyset. Consolidate moderation and other pre-publish logic into that single Function. Calling `ugc.moderateMessage` from it is the supported pattern for combining custom logic with an Auto Moderation configuration.

## Related reference

* [Modules and libraries](https://www.pubnub.com/docs/serverless-sdk/modules-and-libraries/overview.md). Every built-in module available inside a Function.
* [API limits](https://www.pubnub.com/docs/serverless-sdk/api-limits.md). Per-execution XHR request cap, which `moderateMessage` counts against.

Last updated at: 2026-09-30T07:20:08.000Z
