Webhook Relay
Search…
Manipulate webhook request body
Learn how to perform basic request body modifications on webhooks using Functions.
This example will demonstrate how to modify webhook requests on the fly using Webhook Relay Functions that provide FaaS (Function as a Service) functionality. We will create a Lua 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:
1
-- my_function.lua
2
local json = require("json")
3
4
local body, err = json.decode(r.RequestBody)
5
if err then error(err) end
6
7
local message = "Webhook received, user:" .. body["data"]["user"]
8
9
-- Preparing new payload
10
local new_payload = {
11
type= "webhook_event",
12
message= message}
13
14
local result, err = json.encode(new_payload)
15
if err then error(err) end
16
17
-- Set request header to application/json
18
r:SetRequestHeader("Content-Type", "application/json")
19
-- Set request method to PUT
20
r:SetRequestMethod("PUT")
21
-- Set modified request body
22
r:SetRequestBody(result)
Copied!
Now, add this function to your account:
1
relay function create my_function.lua
Copied!
To view your functions:
1
$ relay function ls
2
ID NAME DRIVER SIZE AGE UPDATED AGO
3
6f65e856-374a-49f8-92a2-e6db2281a177 my_function lua 330 B 3 seconds 3 seconds
Copied!

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..)
For the sake of this example we will use https://bin.webhookrelay.com/ 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:
1
relay forward --bucket modify-req-with-func --function my_function --type public https://bin.webhookrelay.com/v1/webhooks/xxx
Copied!
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.
  • https://bin.webhookrelay.com/v1/webhooks/xxx is our webhook destination.
Once the command is executed, you should display

Send test payload

Let’s send a test payload:
1
curl --request POST \
2
--url https://my.webhookrelay.com/v1/webhooks/80d49ea0-0a96-4933-be6d-f128972e1a7d \
3
--data '{
4
"data": {
5
6
}
7
}'
Copied!
You can view modified requests in Webhook Relay dashboard and in the Webhook Bin Service:
modified request payload
Where body is now:
1
{
2
"message": "Webhook received, user:[email protected]",
3
"type": "webhook_event"
4
}
Copied!
and HTTP request method is PUT.