> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hightouch.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a function

> Create, test, and connect a function to transform events before they sync to a destination.

Write JavaScript to modify event properties, validate fields, or enrich event data on a [Hightouch Events](https://docs.hightouch.com/events/overview.md) sync. This guide walks through creating, testing, and connecting a function.

### Creating a function

To create a new function, go to **Event Collection → Functions** and
select **Create function**.

![Create function screenshot](https://mintcdn.com/hightouch-3c037897/L_vQfA0JQEpeGUUo/images/events/functions/create-function.png?fit=max\&auto=format\&n=L_vQfA0JQEpeGUUo\&q=85\&s=004789d38b073bb5c73cc395f02c1dad)

When you create a function, you’ll start with a default function definition,
which you can edit to add custom transformation logic.

Keep these requirements in mind:

* All code must have a `transformEvents` function definition, which receives a
  batch of events, and returns a batch of transformed events
* Output events must conform to the core [Hightouch Event spec](https://docs.hightouch.com/events/event-spec.md)
* Event batches can include multiple event types (so make sure to check
  `event.type` if you need to handle event types differently)
* Batch sizes are variable based on sync volume and event size
* Functions time out after 5 seconds of execution time
* Events can be filtered out by omitting them from the function return

### Testing

Test the function works as expected by using the test panel in the function
code editor view.

Modify the input in the test panel, and select **Test**. Results will be
displayed in the output panel, along with any `console` logs. If the function
errors during execution, the error and stack trace will be displayed.

![Test function screenshot](https://mintcdn.com/hightouch-3c037897/L_vQfA0JQEpeGUUo/images/events/functions/test-function.png?fit=max\&auto=format\&n=L_vQfA0JQEpeGUUo\&q=85\&s=127d1d0d47f68250dc352f34f543a7b3)

### Save & connect

To save a function, hit **Save changes** at the bottom of the code editor.
Upon save, if the function is connected to any event syncs, the new function
code will immediately be used.

To connect a function to a sync, use the **Connected syncs** tab, where you can
connect or disconnect a function from a sync. When a function is connected
to a sync, events in the target event sync will immediately begin to be transformed by
the function.

![Connect function screenshot](https://mintcdn.com/hightouch-3c037897/L_vQfA0JQEpeGUUo/images/events/functions/connect-function.png?fit=max\&auto=format\&n=L_vQfA0JQEpeGUUo\&q=85\&s=72f891d36959d3a118ff3e32c53a905b)

Note: a function can be connected to multiple syncs, but a sync can only have
one function connected to it at a time.

### Runtime and dependencies

Functions are executed in a Node.js 22 runtime environment.

Additional available modules:

* [Crypto](https://developer.mozilla.org/en-US/docs/Web/API/Crypto) is available as `crypto`
* [Fetch\_API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) is available as `fetch`

### Debugging

Errors from functions will be displayed in the event sync error logs, along
with sample event payloads that received that error.

### Other notes

* Functions are executed after contract validation (if a contract is defined
  for the respective event source). This means events blocked by contracts
  are not able to be transformed using functions.
* Use `Promise.all` to run async code in parallel, reducing the chance of
  hitting a timeout
* If a function errors, the entire batch is failed and will be retried
  based on the sync retry configuration
