HTTP from SQL
A Telegram notification, a webhook to your server, a call to a third-party API
is an ordinary database function that calls net.http_post. The functions are
the same as in the pg_net extension in Supabase, so code from there moves over
unchanged, except for secrets.
| Function | What it does |
|---|---|
net.http_post(url, body, params, headers, timeout_milliseconds) | POST with a JSON body; Content-Type: application/json by default |
net.http_get(url, params, headers, timeout_milliseconds) | GET |
net.http_delete(url, params, headers, timeout_milliseconds) | DELETE |
Every argument except url is optional. The function returns the request id
(bigint) and returns immediately: the request goes out after the transaction
commits.
The net schema appears in the database together with the Data API. If the
database already has its own net schema or the pg_net extension, Layero
leaves it alone.
A Telegram notification
First set a secret — the bot token from @BotFather. The value goes to Layero and never lands in the database:
printf %s "$BOT_TOKEN" | npx layero@latest data secrets set TELEGRAM_BOT_TOKEN --db <database>
The same in the panel: "Data API" → "Access" → "Secrets".
In the URL, headers and body of a request a secret is written as
{{secrets.NAME}} — Layero substitutes the value when sending.
Option 1. One function writes and notifies
The site calls one RPC method. The write and the notification share a transaction: nothing written — nothing sent.
CREATE FUNCTION api.submit_report(site_key text, message text) RETURNS jsonb
LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $$
DECLARE
s public.sites%ROWTYPE;
id bigint;
BEGIN
SELECT * INTO s FROM public.sites WHERE sites.key = submit_report.site_key;
IF NOT FOUND THEN RAISE EXCEPTION 'unknown site key'; END IF;
INSERT INTO public.reports (site_id, message) VALUES (s.id, submit_report.message)
RETURNING reports.id INTO id;
PERFORM net.http_post(
url := 'https://api.telegram.org/bot{{secrets.TELEGRAM_BOT_TOKEN}}/sendMessage',
body := jsonb_build_object('chat_id', s.telegram_chat_id,
'text', 'New report: ' || left(submit_report.message, 500))
);
RETURN jsonb_build_object('id', id);
END $$;
GRANT EXECUTE ON FUNCTION api.submit_report(text, text) TO anon;
Option 2. A trigger — when the table is written several ways
If the site writes through the Data API, your server through the connection string and migrations as well, hang the notification on a trigger: it can't be bypassed.
CREATE SCHEMA IF NOT EXISTS private;
CREATE FUNCTION private.notify_new_report() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $$
BEGIN
PERFORM net.http_post(
url := 'https://api.telegram.org/bot{{secrets.TELEGRAM_BOT_TOKEN}}/sendMessage',
body := jsonb_build_object('chat_id', 123456789, 'text', 'New report: ' || new.message)
);
RETURN new;
END $$;
CREATE TRIGGER reports_notify AFTER INSERT ON public.reports
FOR EACH ROW EXECUTE FUNCTION private.notify_new_report();
⚠️ Don't make a separate "send a notification" RPC that the site calls after the write. Anyone with your public key can call it with any text, and between the two calls the write and the notification drift apart.
A webhook to your server
PERFORM net.http_post(
url := 'https://example.com/hooks/order',
headers := jsonb_build_object('Content-Type', 'application/json',
'Authorization', 'Bearer {{secrets.HOOK_TOKEN}}'),
body := jsonb_build_object('type', 'INSERT', 'table', 'orders', 'record', to_jsonb(new))
);
What happens after the call
- The request lands in the
net.http_request_queuequeue — in the same transaction as your write. Rolled back — the request won't go out. - After the commit Layero sends it. Internal addresses are refused: the request only goes to the internet.
- Any server response is final, as in
pg_net: 3xx, 4xx and 5xx are stored with their code, and your code decides bystatus_code. Layero does not follow redirects. Only network failures and timeouts, where there is no response at all, are retried: up to 5 attempts with pauses of 2, 4, 8 and 16 minutes. So after a network failure a notification may arrive several minutes late. Setup errors (missing secret, bad address) are not retried: a person fixes those, not time. - The outcome goes to
net._http_responseunder the same id: status code, headers, body (up to 64 KB, secret values cut out),timed_out,error_msg. Responses are kept for 6 hours, as inpg_net. - A database's queue holds at most 1000 unsent requests. Beyond that
net.http_*fails right away — most often it is a function calling the request in a loop.
SELECT id, status_code, error_msg, left(content, 200)
FROM net._http_response ORDER BY id DESC LIMIT 10;
The timeout is timeout_milliseconds: 5000 ms by default, no more than 15 seconds. Requests
count toward the database's daily request limit.
Differences from pg_net in Supabase
Supabase pg_net | Layero | |
|---|---|---|
net.http_* signatures | — | the same |
| Sending | after commit | after commit |
| Retries | no | network failure and timeout only, up to 5 attempts; any server response is final |
| Secrets | vault.decrypted_secrets right in SQL | {{secrets.NAME}} — the value never lands in the database |
| Internal addresses | reachable | refused |
| Database Webhooks in the panel | yes | no: a webhook is the trigger from option 2 |
If code from Supabase reads a token from vault.decrypted_secrets, replace that
expression with {{secrets.NAME}} in the URL or header string.
Incoming webhooks — there are none
HTTP from SQL works one way: from the database out. The Data API has no address that accepts someone else's request without a key and runs your function. In Supabase such requests are handled by Edge Functions; we do not have them and do not run your code on our side.
What to use instead, from best to worst:
-
The service can add headers — your backend, an integration platform. Let it call the function itself:
POST https://data.layero.ru/<database-address>/rest/v1/rpc/<function>with the key in theapikeyheader. Keep the secret key on the server only. -
The service only sends a webhook (Telegram, YooKassa, GitHub). Run a small server app on Layero in Node or Python. It receives the webhook, checks the service's signature or secret and calls the database function with the secret key. It is an ordinary Layero project; nothing extra is needed.
-
The service has a "give me what's new" method — e.g.
getUpdatesof a Telegram bot. A database function polls it withnet.http_getand reads the answer fromnet._http_response. This works, but keep polling in bounds:- poll only while you wait for an answer (the "connect the bot" dialog is open), and cap the wait, e.g. 15 minutes;
- no more often than every 2 seconds;
- pass
offsetso the same updates are not read again; - every function call from the browser counts towards the database's daily request limit, and responses are kept for 6 hours and take its space.
In Telegram polling and a webhook exclude each other: while the bot has a webhook set,
getUpdatesreturns an error.