Webhooks
A webhook lets a query notify another system over HTTP when it runs. Kvery
sends an HTTP request (usually a POST) to a URL you choose — on success, on
error, or on a specific status code — without any backend code on your side.
Webhooks are driven entirely by directives in your SQL.
Write the directive at the end of its statement, before the ;: a directive after
the ; becomes a statement of its own and causes a database error. A statement can
carry one webhook directive; put a second webhook on another statement of the run.
Firing on success
Use ON SUCCESS POST to notify a system after the query completes successfully:
INSERT INTO orders (customer_id, total)
VALUES ('$customer_id', '$total')
ON SUCCESS POST '{"text": "New order created"}' TO 'https://hooks.example.com/order-created';
When the run succeeds, Kvery sends a POST to the URL with the payload you
specify.
Firing on error
Use ON ERROR POST to send an alert when the query fails — perfect for
monitoring:
SELECT * FROM critical_report
ON ERROR POST '{"text": "The critical report failed"}' TO 'https://hooks.example.com/report-failed';
Firing on a status code
Use ON '<code>' to fire only when the run answers with that HTTP status code:
SELECT * FROM orders WHERE id = '$id'
ON ERROR RETURN 'Order not found' SET HTTPCODE '404'
ON '404' POST '{"text": "An order was not found"}' TO 'https://hooks.example.com/order-missing';
Using both together
A query can notify different endpoints for success and failure. Each webhook goes
on its own statement, and a webhook directive takes effect only when its statement
is reached, so put the ON ERROR webhook on the statement that can fail:
UPDATE invoices SET paid = 1 WHERE id = '$id'
ON ERROR POST '{"text": "Invoice update failed"}' TO 'https://hooks.example.com/invoice-error';
SELECT id, paid FROM invoices WHERE id = '$id'
ON SUCCESS POST '{"text": "Invoice paid"}' TO 'https://hooks.example.com/invoice-paid';
When webhooks fire
Kvery sends the webhooks after the response has been sent to the caller, according to the final HTTP status of the whole run:
ON SUCCESSfires on a2xxstatus,ON ERRORfires on a4xxor5xxstatus,ON '<code>'fires on exactly that status.
Only http:// and https:// URLs are called, also after redirects, and only
addresses on the public internet: a URL (or a redirect) that points to an address
that is not public (loopback, private, link-local, carrier-grade NAT, reserved or
multicast, such as localhost or 10.0.0.5) is not called, and neither is a host
name that cannot be resolved. At most 20 redirects are followed, and the
certificate of an https address is not verified. A webhook request, with its
redirects, may take at most 100 seconds, and the receiving server has to accept the
connection within a second. A redirect keeps the method, sends the body again only
for 307 and 308, and leaves out the Authorization and Cookie headers when it
leads to another scheme, host or port.
A webhook that fails does not change the answer of the run, which has already been sent; the webhook's request and its result are recorded in the query's log (as far as the log level of the query records the run).
The payload
The payload is the text from the apostrophe after the method up to the ' TO '
in front of the URL:
- A JSON object or array is sent as JSON.
- Any other text is wrapped as
{"text": "…"}. - The body is sent with the
Content-Type: application/jsonheader. - A payload of
@erroris replaced with the statement's response — for a failed statement, its error message. - For a URL that contains
slack(a Slack webhook or a Slack-compatible endpoint), Kvery adds a bot name and icon unless the payload setsusernameandicon_urlitself.
A GET webhook sends no body. Point the URL at any endpoint that accepts an inbound
webhook (an automation tool, a chat notifier, your own API).
Choosing the HTTP method
POST is the typical choice, but the method right after ON SUCCESS / ON ERROR
/ ON '<code>' can be any of GET, POST, PUT, PATCH or DELETE — use
whatever the receiving endpoint expects:
UPDATE invoices SET paid = 1 WHERE id = '$id'
ON SUCCESS PUT '{"paid": true}' TO 'https://hooks.example.com/invoice/$id';
Common patterns
- Chained workflows — trigger a downstream job after a write completes.
- Alerting —
ON ERROR POSTto a monitoring or chat endpoint. - Audit/notify —
ON SUCCESS POSTto record that a sensitive query ran.
Tips
- Combine webhooks with
SET HTTPCODEandON ERROR RETURNso the caller of your API and the webhook receiver both get clean, intentional responses. - Make sure the target URL is reachable from the internet and responds quickly.
- To call an endpoint while the query runs and use its answer, use an
HTTP request directive such as
GET('…')instead. - See Posting data for the write queries these webhooks usually accompany.