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

# Trasformazioni

> Modifica i payload webhook di storekit con JavaScript personalizzato prima che raggiungano il tuo endpoint. Cambia URL, metodo, corpo o header senza un server proxy.

Le trasformazioni ti permettono di scrivere codice JavaScript che modifica le proprietà del webhook prima che venga consegnato al tuo endpoint. Puoi cambiare il metodo HTTP, l'URL di destinazione, il payload, aggiungere header personalizzati o persino annullare webhook specifici.

## Casi d'uso

Le trasformazioni sono utili quando devi:

* **Modificare i payload** per adattarli al formato atteso dal tuo sistema
* **Reindirizzare i webhook** a URL diversi in base al contenuto del payload
* **Aggiungere header personalizzati** per autenticazione o instradamento
* **Filtrare i webhook** annullando quelli che non ti servono
* **Trasformare i dati** (ad es. convertire formati di valuta, rinominare campi)

## Accedere alle trasformazioni

1. Vai alle [impostazioni Webhook](https://dashboard.storekit.com/developers/webhooks) nella tua dashboard
2. Clicca su un endpoint per visualizzarne i dettagli
3. Naviga nella scheda **Advanced**
4. Scorri fino alla scheda **Transformations**
5. Attiva l'interruttore per abilitare le trasformazioni

## Scrivere una trasformazione

Le trasformazioni utilizzano una funzione JavaScript `handler` che riceve i dati del webhook e restituisce la versione modificata.

### Proprietà di input

La funzione `handler` riceve un oggetto con queste proprietà:

| Proprietà   | Tipo   | Descrizione                      |
| ----------- | ------ | -------------------------------- |
| `method`    | string | Metodo HTTP (`"POST"` o `"PUT"`) |
| `url`       | string | L'URL dell'endpoint              |
| `payload`   | object | Il payload del webhook come JSON |
| `eventType` | string | Il tipo di evento (sola lettura) |

### Proprietà di ritorno

Restituisci lo stesso oggetto con le tue modifiche. Puoi anche aggiungere:

| Proprietà | Tipo    | Descrizione                                        |
| --------- | ------- | -------------------------------------------------- |
| `cancel`  | boolean | Impostalo a `true` per annullare questo webhook    |
| `headers` | object  | Header personalizzati da aggiungere alla richiesta |

## Esempi

### Modificare il payload

Appiattire dati annidati o rinominare campi:

```javascript theme={null}
function handler(webhook) {
  // Add a custom field
  webhook.payload.source = "storekit";
  
  // Rename a field
  webhook.payload.orderId = webhook.payload.order_id;
  delete webhook.payload.order_id;
  
  return webhook;
}
```

### Reindirizzare in base al payload

Inviare i webhook a URL diversi in base al contenuto:

```javascript theme={null}
function handler(webhook) {
  if (webhook.payload.priority === "high") {
    webhook.url = "https://example.com/webhooks/priority";
  }
  return webhook;
}
```

### Aggiungere header personalizzati

Includere header aggiuntivi per il tuo endpoint:

```javascript theme={null}
function handler(webhook) {
  webhook.headers = {
    "X-Custom-Header": "my-value",
    "X-Store-ID": webhook.payload.store_id
  };
  return webhook;
}
```

### Annullare webhook specifici

Filtrare i webhook che non vuoi ricevere:

```javascript theme={null}
function handler(webhook) {
  // Don't send webhooks for test orders
  if (webhook.payload.test_mode === true) {
    webhook.cancel = true;
  }
  return webhook;
}
```

### Cambiare il metodo HTTP

Passare da POST a PUT se necessario:

```javascript theme={null}
function handler(webhook) {
  webhook.method = "PUT";
  return webhook;
}
```

## Testare le trasformazioni

Prima di salvare la tua trasformazione:

1. Clicca **Test** nell'editor delle trasformazioni
2. Seleziona un tipo di evento o inserisci un payload personalizzato
3. Rivedi l'output trasformato
4. Verifica che le modifiche siano corrette prima di salvare

<Warning>
  I webhook annullati appaiono come consegne riuscite nei tuoi log. Usa l'annullamento con attenzione per evitare di perdere eventi importanti.
</Warning>

<Tip>
  Mantieni le trasformazioni semplici e veloci. Logiche complesse possono causare timeout o comportamenti inaspettati.
</Tip>
