Webhooks
Webhooks are used to connect inbound HTTP requests with flows. Webhooks conveniently facilitates three common use cases:
- Consume incoming HTTP requests (webhooks) from external services and systems
- Create API endpoints, powered by flows
- Trigger flows with external HTTP requests
In Sirveo, a webhook is a simple configuration that associates a unique URL to a target flow, with optional authentication.
Configuring webhooks
Before creating a webhook, you should have an existing graph or task flow to associate with it.
-
In the UI, navigate to Webhooks, under Flows in the menu
-
Click on the New Webhook button
-
Configure the webhook, and select save
The webhook identifier is a unique name, used to create the webhook’s URL. By default, it is a random value, which can be changed to something more descriptive.
Existing webhooks appear like this in the webhook list:
The UI shows the webhook’s associated URLs. The inbound URL is shown if the server’s inbound port and URL is configured.
In this case, the webhook is live, because the associated flow is active.
Webhook URLs
For a webhook with an identifier of flow-one
, with an active flow, the following URLs are available on Sirveo’s HTTP server(s):
Since the server is typically deployed in a container and/or behind a reverse proxy, the UI relies on the server’s URL configuration to determine the actual webhook URLs:
See network isolation for details on separating admin & inbound HTTP traffic.
Output mode
The Output Mode setting of webhooks determines the behaviour of HTTP response bodies.
By default, webhooks use output mode “None”, which is the safe default. In this case, HTTP responses only contains a status code, without a response body.
If a webhook’s output mode is changed to “Flow Output”, a webhook’s HTTP response body contains the output of it’s target flow, as JSON data. Flows may produce any output data that can serialize to JSON. This makes webhooks a powerful feature that can be used to create HTTP API’s.
Understanding Flow Output
By default, the flow engine assumes that a flow’s output data is the output of the last node which executed in that flow.
As of Sirveo 0.3.0, the output node can be used to change the default flow output behaviour. When an output node runs at any stage of a flow, it’s output data becomes the flow’s output data. Multiple output nodes may be used in the same flow. Each output node will replace the flow’s output data.
Authentication token
When an authentication token is configured for a webhook, the server requires that incoming HTTP requests provide that token. If configured, tokens must contain at least 16 characters.
Given a webhook with an authentication token of 117881a82594c4ac5446b18a71500f5d
, HTTP clients must provide the
authentication token value as a request header:
Or as a query parameter in the URL:
If an HTTP client does not provide a token, or provides a non-matching token, the server responds with a 401 status, and a JSON body:
Flow input data
The server provides data from and about incoming HTTP requests to a webhook’s associated flow, as flow input data.
To learn about how the server turns HTTP requests into flow input, we can easily configure a flow to show us.
-
Make a new graph flow, named
HTTP echo
-
Keep the default passthrough node
-
Activate the flow (top-right button in flow editor)
-
Create a new webhook.
- As identifier use
http-echo
- Select the new graph flow
- As output mode, use “Last”, since we want the output
- As identifier use
-
Save the webhook
To clarify what we’re implementing here:
- An HTTP client (your browser, shortly) opens the webhook URL
- The server prepares input data for the flow, from the incoming HTTP request
- The graph flow will output its input data, as-is, because of the passthrough node
- The webhook’s “Last” output mode includes the flow output in the HTTP response body, in this case to the browser
Let’s see this in action; Click on the webhook’s URL, which will open in a new browser tab. You will see some JSON data that looks similar to this:
Here we can see the metadata which the server adds to the flow input:
- client IP
- request method
- webhook ID and key (identifier)
And data from the HTTP request itself:
- all headers, as received by the server
- query parameters (none in this case)
- request body (empty in this case)
Let’s see what happens when I;
- add an authentication token to the webhook
- call the webhook URL from a non-browser client, like CURL or Postman
- add a query parameter, and some JSON data in the request body
HTTP response status
The server may reject certain HTTP requests to webhooks, or encounter errors before or during flow execution. Different scenarios result in different HTTP response status code:
status | when |
---|---|
404 | Webhook identifier does not exist |
404 | Target flow not active |
401 | Authentication token required, but missing/invalid |
500 | Server is in restricted mode |
400 | Request body contains invalid JSON data |
422* | Flow definition is not valid |
422** | Flow engine rejected input data |
400*** | The flow executed, but encountered a runtime error |
200 | The flow executed without runtime errors |
* Can happen, for example, when a flow definition is incorrectly modified directly in the database.
** Should not happen.
*** A response body error message will contain {"error":"Execution error"}
, without additional detail. This is by design, since error messages may contain potentially sensitive information.
Custom HTTP response status
If a webhook executes a flow, that flow can use the Status node to optionally configure the HTTP response code returned by webhooks to clients.
If configured by a Status node, valid status codes are in the 200-299, 400-499 and 500-599 ranges.
Webhook constraints
- Webhooks supports task and graph flows (use links for web flows)
- Webhooks will not run flows in restricted mode
- Request bodies are ignored for HTTP request methods other than
POST
,PUT
, andPATCH
- HTTP requests with request bodies must use the
application/json
content type header