---
source_url: https://www.pubnub.com/docs/message-processing/serverless/create-function
title: Create a Function
updated_at: 2026-09-30T07:20:08.000Z
---

# Create a Function

## 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.

In this tutorial, you will create a Before Publish Function that modifies messages on a channel, deploy it to a keyset, and confirm it is running by publishing a test payload in the Debug console.

## What you will build

By the end of this tutorial, your Function will intercept every message published on the `hello-world` channel and add `hello: 'world!'` to the payload before PubNub delivers it to subscribers. You will verify this transformation in the Admin Portal, without writing any client code.

## Before you start

Before starting, confirm you have:

* A PubNub account with at least one keyset. If you do not have one, see [Set up your account](https://www.pubnub.com/docs/architecture/authentication/set-up-your-account.md).
* The Functions developer role, or Account Admin access. Without one of these, you cannot create Packages or manage deployments.

Every PubNub plan includes Functions.

## Create a Package

A Package groups related Functions. You need one before you can create a Function.

1. Log in to the [Admin Portal](https://admin.pubnub.com) and go to **Functions**.
2. Click **Create package**.
3. Choose **Configure your own**.
4. Enter a name and description for the Package, then click **Next**.

PubNub creates your Package and takes you to the Function creation screen.

## Create a Function

1. Enter a name for the Function.
2. Select **Before Publish or Fire** as the event type.
3. Set the trigger channel to `hello-world`.
4. Click **Create package**.

PubNub creates the Package with your Function inside. You will see an initial Revision in the Package list.

For more on trigger types and when to use each one, see [Functions](https://www.pubnub.com/docs/message-processing/serverless/overview.md).

## Add your code

1. Click **Edit code** or **Edit Latest** on the Package Revision list to open the editor.
2. The editor shows a sample Function. Replace all of it with the following code:

   ```javascript
   export default async (request) => {

       console.log('request', request);
       request.message.hello = 'world!'; // augment hello = world
       console.log('The message: ', request.message, ' was published on ', request.channels[0], ' via', request.meta.clientip);

       return request.ok(); // An async handler always returns a Promise
   }
   ```

3. Click **Save**. PubNub creates a new Revision. Enter a Revision name and description, then save.

This Function logs the incoming request, adds `hello: 'world!'` to the message payload, and calls `request.ok()` to pass the modified message on for delivery.

For details on the `request` object and handler structure, see [Functions development](https://www.pubnub.com/docs/message-processing/serverless/development.md).

## Deploy to a keyset

A Revision must be deployed to a keyset before it processes live traffic.

1. Click **Deploy** on the Revision.
2. Click **Add keyset** and select the keyset where you want the Function to run.
3. Click **Create**.
4. Click **Deploy** to confirm. For this example, skip the secrets step if you have no secrets configured.

Your Function is now active on the selected keyset.

## Test in the Debug console

1. Click **Edit code** or **Edit Latest** on the Package Revision list to open the editor.
2. Switch to the **Debug** tab.
3. Click **Publish** in the **Test payload** section.

You will see log output appear in the console. The response shows your message with the `hello: 'world!'` field added. This confirms the Function intercepted the message and transformed it before delivery.

## What you built

You created a Before Publish Function that adds `hello: 'world!'` to every message on the `hello-world` channel, saved it as a Revision, deployed it to a keyset, and verified the transformation in the Debug console. Any client publishing to `hello-world` on this keyset now has the field added before subscribers receive the message.

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