- Use Case
- Log in →
- Webhook Forwarding
- Functions (transform) [beta]
- Exec Commands
- WebSocket Server
- HTTP Tunnels
- TLS Tunnels
- Global Infrastructure
Internet of Things
- Home Automation
- Internet of Things (IoT)
- CLI commands
- Proxy Configuration
- Self-hosted deployment
- Client configuration
Functions (transform) [beta]
Functions are currently in beta stage. Please report any issues to [email protected].
Webhook Relay Functions provide an easy way to extract values from webhooks and transform HTTP requests before passing them to their final destination. Function can be reused by any number of Inputs and/or Outputs. Since user doesn’t have to run a server, this type of function service is also known as FaaS (Function as a Service) or Serverless.
Functions on Inputs transform webhooks when they enter the system and also allows you to customize both response that Webhook Relay will return and webhook request that will be passed along to all outputs.
Functions on Outputs allow you to customize webhooks for each destination. This way you can have a custom webhook arriving at each destination (Slack, Mattermost or some CRM system like HubSpot or Zoho).
Currently, you can write functions in:
- Lua language (perfect for most of the tasks).
- WASM (WebAssembly) which is good when you need to perform more complex tasks or just want to produced a statically compiled function using languages such as Rust.
During Functions beta they will only be manageable using our CLI.
To create a new function, simply use
relay function create command:
relay function create <file name>.lua
You can also specify name, source and driver using optional flags:
- –driver lua or wasm (defaults to the extension.)
- –name name of the function (defaults to the name of the file)
- –source points to the location of the function file (usually .lua (version 5.1) or .wasm)
In this example we will use a Lua 5.1 function that takes webhook JSON as an input and converts it to a Slack message JSON to display an alert for us in chat.
Mailgun JSON (shortened version, usually it’s a bit bigger):
Below is a Lua function that converts Mailgun webhook into a Slack webhook:
This function transforms it into a JSON that can be accepted by Slack incomming webhook API:
To add this function to your account so we can use it later, type:
relay function create mailgun_to_slack.lua
The easiest way to start using Functions is with the
relay forward --bucket mailgun-to-slack --function mailgun_to_slack --type public https://hooks.slack.com/services/xxx
This command will create a new bucket, input and output. Function will be attached to the output that’s forwarding to
Alternatively, you can attach Functions to existing Inputs:
relay input update INPUT_ID -b BUCKET_NAME --function mailgun_to_slack
relay output update OUTPUT_ID -b BUCKET_NAME --function mailgun_to_slack
To stop using some particular function in your Input or Output, just update Function to “”:
relay input update INPUT_ID -b BUCKET_NAME --function ""
You can view your uploaded functions via
relay function ls command:
relay function ls
To update a function, you have to specify the name and source location:
relay function update mailgun_to_slack --source mailgun_to_slack.lua
To delete a function, first ensure that any Input or Output doesn’t use the function anymore. Once it’s done:
relay function rm mailgun_to_slack
Lua functions use
r object that provide access to request data (headers, query, method and body) and can then update any HTTP request details.
Available data to access:
r:RequestBody- request body.
r:RequestMethod- request method (PUT, POST, DELETE, etc.).
r:RequestPath- request path.
r:RequestRawQuery- request query, for example if the request was made with /api?category=electronics, then the query will be category=electronics.
r:RequestHeader- a table [foo]bar of headers.
An example of accessing request body and decoding it:
local json = require("json")
Available methods to update request:
r:SetRequestBody("string")- update request body
r:SetRequestMethod("string")- update request method
r:SetRequestRawQuery("foo=bar")- update request raw query
r:SetRequestPath("/extra/path")- set additional extra path
r:SetRequestHeader("key", "value")- set new header key/value pair
An example how to update request object:
-- set body
Available methods to set customized response:
r:SetResponseBody("response body")- set response body
r:SetResponseStatusCode(201)- set response status code
r:SetResponseHeader("key", "value")- set response header key/value pair
Customized responses only applicable if function is attached to the Input and not bucket Output.