PreviewRustaBase OSS is in public preview. Build on managed PostgreSQL with APIs, authentication, storage, realtime, and server-side functions.Read the product direction

Functions and configuration

Edge Functions

Run server-side JavaScript inside your Project for custom endpoints, record hooks, scheduled work, and integrations with other services, without running a separate server.

Triggers

Every function has one trigger. You choose it in the Triggers tab and can change it at any time.

TriggerRuns whenTypical use
HTTP endpointA request arrives at /edge-hook/<name> on your Project address, with any HTTP methodCustom APIs, inbound webhooks, form handlers
Record hookA record in the chosen table is created, updated, or deletedNotifications, derived data, audit trails
Cron scheduleThe cron expression matches (minute, hour, day, month, weekday)Cleanup, reports, synchronization
Manual onlySomeone selects Run function now, or it is called through the admin SDKOne-off maintenance and testing

Create your first function

  1. Open your Project in the console and select Functions.
  2. Select New function and pick a template: Blank, Record hook, HTTP webhook, Scheduled job, Call an API, or SQL query.
  3. Give it a name. The name becomes part of the endpoint address, so use lowercase letters, numbers, and dashes.
  4. Write your code in the Code tab. The last expression is the function's result.
  5. Choose the trigger in the Triggers tab.
  6. Add environment variables and the execution timeout in the Settings tab.
  7. Select Save, then open the Run tab and select Run function now to test it.
  8. Check the Logs tab for output, duration, and errors.

What your code can use

NameWhat it gives you
$requestThe incoming HTTP request for endpoint triggers: method, headers, query, and body
$dataThe record that triggered a record hook, or the test payload from the Run tab
$db.query(sql, params)Runs a PostgreSQL read and returns the rows
$db.execute(sql, params)Runs a PostgreSQL write such as UPDATE or INSERT
$http.get / $http.postCalls another service and returns its status and data
$env.KEYReads an environment variable set in the Settings tab
$utils.now()The current time, plus other small helpers
console.logWrites a line to the function's execution log

Always pass values through parameters such as {:id} instead of joining them into SQL text. This keeps user input from changing your query.

Example: an HTTP endpoint

The returned object controls the response. Set status and body, and RustaBase sends it back as JSON.

// Trigger: HTTP endpoint, name: create-lead
const body = $request.body || {};
if (!body.email) {
  ({ status: 400, body: { error: "email is required" } });
} else {
  $db.execute(
    "INSERT INTO leads (email, source) VALUES ({:email}, {:source})",
    { email: body.email, source: body.source || "website" },
  );
  ({ status: 201, body: { ok: true } });
}
curl -X POST "https://my-app.rustabase.net/edge-hook/create-lead"               -H "Content-Type: application/json"               -H "X-Edge-Secret: YOUR_SHARED_SECRET"               -d '{"email":"[email protected]"}'

Example: a record hook

// Trigger: Record hook, table: orders, event: On create
const res = $http.post("https://hooks.example.com/new-order", {
  id: $data.id,
  total: $data.total,
}, { headers: { Authorization: "Bearer " + $env.NOTIFY_TOKEN } });

console.log("notified", res.status);
({ handled: true })

Call a function from your app

HTTP endpoints are normal web addresses, so any client can call them. From the SDKs, use fetch in JavaScript or the http package in Dart.

const res = await fetch("https://my-app.rustabase.net/edge-hook/create-lead", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ email: "[email protected]" }),
});
const result = await res.json();
import 'dart:convert';
import 'package:http/http.dart' as http;

final res = await http.post(
  Uri.parse('https://my-app.rustabase.net/edge-hook/create-lead'),
  headers: {'Content-Type': 'application/json'},
  body: jsonEncode({'email': '[email protected]'}),
);

Manage functions from a server

Superuser and admin tools can list, test, and read logs for functions through rb.admin.functions.

const fns = await rb.admin.functions.list();
const run = await rb.admin.functions.test("FUNCTION_ID", { email: "[email protected]" });
const logs = await rb.admin.functions.logs("FUNCTION_ID", { status: "error", limit: 20 });

Limits and good practice

  • The execution timeout defaults to 15 seconds and can be raised to 120 seconds. Longer runs are stopped and logged.
  • Protect public endpoints with a shared secret. Callers send it in the X-Edge-Secret header or the secret query parameter.
  • Keep tokens and passwords in environment variables, never in the code.
  • Pause a function with its status switch instead of deleting it when you want to stop it for a while.
  • Make record hooks and scheduled functions safe to run twice. Retries and manual runs can repeat work.
  • Use Duplicate to try a change safely before editing a function that is already live.

Schedule recurring work

Found something wrong on this page?

Fix it yourself. The link below opens this file in GitHub's editor and forks the repository for you if you need one, and your change becomes a pull request without leaving the browser.