Transformations
A transformation lets you modify the payload of a event prior to delivery.Transformations in pabbly hook allow you to modify, reformat, or map data from incoming webhook requests before passing it to the destination. transformations enable customized data handling and adjustments, which is especially useful when the incoming data doesn’t match the exact format needed by the receiving system. Transformations provide flexibility, allowing for operations such as renaming fields, changing values, adding or removing fields, and applying conditional logic.
Use Case Example: Adjusting Data Format for Compatibility
Suppose an incoming webhook sends a payload with a date in MM/DD/YYYY format, but the receiving application needs it in YYYY-MM-DD format. A transformation can be applied to reformat this date before forwarding it.You can use transformations to solve many problems. Here are a few common scenarios:
- Change payload formats: For example, converting XML to JSON
- Unify models across sources: For example, conforming WooCommerce orders and Shopify orders to a single standard
- Add compatibility when routing to an API: For example, adding keys and reformatting the payload
- Add additional properties to payloads: For example, to more clearly indicate the event type for use later in the pipeline in a filter or in your own application logic.
Filter rules are run after any transformation has been performed.
Transformations syntax
Pabbly Hook allows for arbitrary transformations on request data using JavaScript (ES7).Function : (request, context) => {
return request;
}
Setting up a Transformation
- Navigate to the Transformation Section: In Pabbly hook, locate the connection where you want to apply a transformation. Under the hook settings, find the "Transformations" section.
- Click Transform to add a transformation rule.
- Click Create new transformation.
- Configure the transformation using the supported transformations syntax in the right-hand pane.
5. Define the Transformation Logic: You can specify transformations using code to alter the incoming request's body, headers, or path parameters and add code snippet for transformation.
6. Once written, test your transformation.
- The left pane's Input tab shows the request your transformation will be tested against. To change this request, click edit tab and select a recent payload, or edit the text directly.
- Click Run to test the transformation. Once run, the left pane's Output tab shows the resulting payload.
8. Name the transformation using the input field in the pop-up box, clicking Save to save the transformation.
9. Make sure to select the saved transformation from dropdown and click Save again on the connection form to apply your changes.
Here’s an example code snippet to reformat a date field and rename a parameter for better compatibility.
10. From this point forward, events received on the connection are transformed prior to delivery to their destination.
Code:
(request, context) => {
// Modify the `order_date` from `MM/DD/YYYY` to `YYYY-MM-DD`
if (request.payload.order_date) {
const [month, day, year] = request.payload.order_date.split('/');
request.payload.order_date = `${year}-${month}-${day}`;
}
// Rename `orderID` to `order_id`
if (request.payload.orderID) {
request.payload.order_id = request.payload.orderID;
delete request.payload.orderID;
}
// Example: Add a new field if `total_amount` is above a threshold
if (request.payload.total_amount && request.payload.total_amount > 1000) {
request.payload.high_value_order = true;
}
return request;
}
Transformation Code Example Explanation
- Date Formatting: This code transforms order_date from MM/DD/YYYY to YYYY-MM-DD.
- Renaming Fields: It renames orderID to order_id for consistency.
- Adding Conditional Fields: If total_amount exceeds 1000, a new field high_value_order is added to indicate a high-value order.
# Example Transformation Code Including Payload , Headers , Query Params.
Code:
(request, context) => {
// Modify the `order_date` from `MM/DD/YYYY` to `YYYY-MM-DD` in payload
if (request.payload.order_date) {
const [month, day, year] = request.payload.order_date.split('/');
request.payload.order_date = `${year}-${month}-${day}`;
}
// Rename `orderID` to `order_id` in payload
if (request.payload.orderID) {
request.payload.order_id = request.payload.orderID;
delete request.payload.orderID;
}
// Add a new field in payload if `total_amount` is above a threshold
if (request.payload.total_amount && request.payload.total_amount > 1000) {
request.payload.high_value_order = true;
}
// Modify `api_key` header for consistency
if (request.headers['api_key']) {
request.headers['API_KEY'] = request.headers['api_key'];
delete request.headers['api_key'];
}
// Add a new header if `Content-Type` is missing
if (!request.headers['Content-Type']) {
request.headers['Content-Type'] = 'application/json';
}
// Update query param `status` from "pending" to "in_progress"
if (request.queryParams.status === 'pending') {
request.queryParams.status = 'in_progress';
}
return request;
}
Transformation Code Explanation
- Payload Adjustments:
- Reformats order_date to YYYY-MM-DD.
- Renames orderID to order_id.
- Adds high_value_order if total_amount is greater than 1000.
- Header Adjustments:
- Renames api_key to API_KEY.
- Sets Content-Type to application/json if it was missing.
- Query Parameter Adjustments:
- Changes status from "pending" to "in_progress" if necessary.
Input Example
Here’s an example of how the input might look before applying the transformation:Output Example
After applying the transformation, the output will look like this:Edit a transformation
Editing a transformation changes how payload data is transformed before delivery.- Open the connection rules configuration.
- Navigate to the Transformation Section: Choose the transformation you want to update.
- Next to the transformation rule, click Editor Button(Screenshot for your's refrence).
- Configure the transformation using the supported transformation syntax in the right-hand pane.
- Once written, test your transformation.
- The left pane's Input tab shows the request your transformation will be tested against. To change this request, click Change and select a recent payload, or edit the text directly.
- Click Run to test the transformation. Once run, the left pane's Output tab shows the resulting payload.
- Optionally, you may rename the transformation using the input field in the pop up box. Click Update to confirm the name change.
- Once satisfied, click Update.
- Make sure to click Update again on the connection form to apply your changes.
Attachments
Last edited: