Webhook Relay
PricingLogin
  • Introduction
  • Quick Start - Forwarding
  • Quick Start - Tunnels
  • 🛠️Installation
    • Relay CLI
      • Install
      • Auto-start
      • Run config.yaml reference
    • Containerized
      • Kubernetes Installation
      • Podman
      • Docker
      • Docker Compose
  • Products
    • 🛰️Webhook Forwarding
      • Glossary
      • WebSocket Server
      • Authentication
      • Custom Domains
    • ⚡Functions
      • Managing functions
      • Edit request/response
      • Working with JSON
      • 🦾Advanced
        • Working with time
        • Testing functions in CLI
        • Making HTTP Requests
        • Multipart Form Data
        • URLEncoded Form Data
        • GCP BigQuery
        • Sending Emails
        • JWT authentication
        • Base64, Hashes, Encryption
      • 🤖Integrating into CI/CD
    • 🔃Tunnels
      • Using tunnels
      • Custom Domains
      • Encryption (HTTPS)
      • Regions
  • 📝Examples
    • Intro to examples
    • Webhooks
      • Receiving webhooks on localhost
      • Receive webhooks inside your JavaScript app
      • Execute shell scripts on remote machines
    • Functions
      • Enrich webhooks from 3rd party APIs
      • Convert DockerHub webhook to Slack notification
      • Allowing only POST requests through
      • Manipulate webhook request body
    • Tunnels
      • Ingress for any Kubernetes environment
      • Demoing your website
    • 🏠Home Automation
      • Home Assistant
      • Node-RED
      • Raspberry Pi
  • Platform
    • CLI Basics
    • Using CLI Behind Proxy
    • Self-hosting Server
      • Server deployment
      • Client configuration
    • Security & Tech
Powered by GitBook
On this page
  • Create the function
  • Create routing configuration
  • Send test payload

Was this helpful?

  1. Examples
  2. Functions

Manipulate webhook request body

Learn how to perform basic request body modifications on webhooks using Functions.

PreviousAllowing only POST requests throughNextTunnels

Last updated 2 years ago

Was this helpful?

This example will demonstrate how to modify webhook requests on the fly using Webhook Relay Functions that provide (Function as a Service) functionality. We will create a function, configure routing and then send a test payload.

Create the function

This function will parse JSON payload and then will construct a new JSON payload. It will also change HTTP method (to PUT) and set a content type header. Save this function to file my_function.lua:

-- my_function.lua
local json = require("json")

local body, err = json.decode(r.RequestBody)
if err then error(err) end

local message = "Webhook received, user:" .. body["data"]["user"]

-- Preparing new payload
local new_payload = {
    type= "webhook_event", 
    message= message}

local result, err = json.encode(new_payload)
if err then error(err) end

-- Set request header to application/json
r:SetRequestHeader("Content-Type", "application/json")
-- Set request method to PUT
r:SetRequestMethod("PUT")
-- Set modified request body
r:SetRequestBody(result)

Now, add this function to your account:

relay function create my_function.lua

To view your functions:

$ relay function ls
ID                                     NAME                 DRIVER              SIZE                AGE                 UPDATED AGO
6f65e856-374a-49f8-92a2-e6db2281a177   my_function          lua                 330 B               3 seconds           3 seconds

Create routing configuration

Now, we will need some target where to send webhooks. Normally it would be just your system that is supposed to receive them (your backend application, Zapier, Slack, etc..)

relay forward --bucket modify-req-with-func --function my_function --type public https://bin.webhookrelay.com/v1/webhooks/xxx

Here:

  • –bucket option with ‘modify-req-with-func’ instructs to create a new bucket or reuse existing one. They are used to group inputs and outputs.

  • –function option specifies to use our newly created function.

  • -type specifies to treat the destination as public one so user doesn’t need to start the relay agent on any computer or server.

Once the command is executed, you should display

Send test payload

Let’s send a test payload:

curl --request POST \
  --url https://my.webhookrelay.com/v1/webhooks/80d49ea0-0a96-4933-be6d-f128972e1a7d \
  --data '{
    "data": {
                        "user": "user@example.com"
                }
}'

You can view modified requests in Webhook Relay dashboard and in the Webhook Bin Service:

Where body is now:

{
    "message": "Webhook received, user:user@example.com",
    "type": "webhook_event"
}

and HTTP request method is PUT.

For the sake of this example we will use service. Once you enter the site you should be able to see a generated webhook inbox, copy your endpoint (https://bin.webhookrelay.com/v1/webhooks/xxxx) and use relay forward command:

is our webhook destination.

modified request payload
📝
FaaS
Lua
https://bin.webhookrelay.com/
https://bin.webhookrelay.com/v1/webhooks/xxx