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

# Transforms

> Reshape the body for each destination with a Go template.

A transform changes the body before hooksnode sends it. Write it as a [Go `text/template`](https://pkg.go.dev/text/template). The event body, decoded from JSON, is `.`.

## Examples

<AccordionGroup>
  <Accordion title="Pick fields from a Paystack charge" defaultOpen>
    ```go-template theme={null}
    {
      "reference": {{printf "%q" .data.reference}},
      "amount_kobo": {{printf "%.0f" .data.amount}},
      "email": {{printf "%q" .data.customer.email}}
    }
    ```
  </Accordion>

  <Accordion title="Slack message for a bank credit">
    ```go-template theme={null}
    {"text": {{printf "Credit of %.0f %s from %s" .transaction.amount_minor .transaction.currency .transaction.counterparty | printf "%q"}}}
    ```
  </Accordion>

  <Accordion title="A field that can be missing">
    ```go-template theme={null}
    {"ref": {{if .data.reference}}{{printf "%q" .data.reference}}{{else}}null{{end}}}
    ```
  </Accordion>

  <Accordion title="A list of item IDs">
    ```go-template theme={null}
    {"ids": [{{range $i, $e := .items}}{{if $i}},{{end}}{{printf "%q" $e.id}}{{end}}]}
    ```
  </Accordion>
</AccordionGroup>

## Write valid JSON

The template prints text. It does not make JSON for you. Use these patterns:

| Value          | Write                       | Why                                                                                     |
| -------------- | --------------------------- | --------------------------------------------------------------------------------------- |
| Text           | `{{printf "%q" .name}}`     | Adds quotes and escapes `"` and `\`.                                                    |
| Number         | `{{printf "%.0f" .amount}}` | JSON numbers are decoded as floats. Plain `{{.amount}}` prints `4.5e+06` for 4,500,000. |
| Decimal        | `{{printf "%.2f" .rate}}`   | Fixed decimal places.                                                                   |
| Object or list | Build it from fields        | `{{.}}` prints Go's form, such as `map[a:1]`, not JSON.                                 |

## What you can use

* Field access: `{{.data.reference}}`
* Conditions: `{{if}} … {{else}} … {{end}}`
* Loops: `{{range .items}} … {{end}}`
* Go's built-in functions, such as `printf`, `len`, `index`, `eq`, `and`, `or`, `not`

There are no extra functions. For example, there is no `toJson`.

## Limits

| Limit          | Value     |
| -------------- | --------- |
| Template size  | 8 KB      |
| Output size    | 1 MB      |
| Run time       | 2 seconds |
| Nested `range` | 2 levels  |

For safety, hooksnode also refuses a `range` over a number, a template that calls itself, and `printf` widths of 4 digits or more.

## When a transform fails

If the transform fails, hooksnode sends the **original** body and records a warning on the delivery. Check the warning in the inspector.

<Tip>Use the **Test** panel in the destination editor to run the transform on a sample body before you save.</Tip>
