Build
Functions
A function is your project's own server-side code, run by Rezin Core. Use one when something must happen on a server rather than in the browser or the phone: charging a card with a key the client must never see, tidying data on a timetable, reacting when a row is added.
Each run happens in a confined process of its own. A function has exactly one way to reach anything: fetch.
Write one
One JavaScript module that exports a default function. It is given a standard Request and returns a standard Response (or a string, or a plain object, which is sent as JSON).
export default async (request, context) => {
const { name = 'world' } = await request.json().catch(() => ({}));
console.log('called with', name);
return Response.json({ hello: name });
};
context holds:
| Field | What it is |
|---|---|
context.secrets | The secrets you assigned to this function, by name. Nothing else from the project's secrets. |
context.apiUrl | The address of this project's API. Call it with fetch and one of the project's keys, like any other caller. |
context.user | { id, organizationId } of the signed-in user who called, or null |
context.trigger | "http", "schedule", "webhook" or "test" |
context.project.ref | The project ID |
context.function.name, context.function.version | Which function and version is running |
console.log, info, warn and error go to the function's log in the console: 200 lines a run, kept seven days. The values of assigned secrets are blanked out of the log.
Node's built-in modules can be imported. Packages cannot: a function is one module of up to 256 KB.
Reach your project's data
A function is not handed a database connection. It uses the API, with a key you choose to give it. Store the project's secret key as a secret (for example API_KEY), assign it to the function, and:
export default async (request, context) => {
const r = await fetch(`${context.apiUrl}/v1/data/orders?status=eq.open`, {
headers: { apikey: context.secrets.API_KEY, 'x-organization-id': '0d9c8b7a-6f5e-4d3c-8b2a-1f0e9d8c7b6a' },
});
return Response.json({ open: (await r.json()).rows.length });
};
To act as the person who called instead, pass their token on: forward the request's authorization header with the publishable key, and row-level security applies to them as it would from the browser.
From inside a function the project's API offers data, sign-in and storage (/v1/data, /v1/auth, /v1/storage). It does not offer other functions, so a function cannot call a function, and it does not offer the management API.
Reach the internet
fetch to any public http or https address works. Addresses on a private network, on Rezin Core's own machines, and link-local addresses are refused, including when a public address redirects to one. A run may make 20 requests, and an answer may be up to 6 MB.
Deploy
In the console: Functions in the project. Type a name, edit, and choose Deploy as a new version. The newest twenty versions are kept; Make live on an older one undoes a mistake.
From a terminal, with a read-write personal access token:
rezin functions deploy send-welcome ./send-welcome.js
rezin functions set send-welcome secrets=API_KEY,MAIL_KEY timeoutSeconds=20
rezin functions invoke send-welcome '{"email":"[email protected]"}'
rezin functions logs send-welcome
From an AI assistant through the connector: deploy_function, update_function, invoke_function, get_function_logs, list_functions, get_function, delete_function.
Function names are lowercase letters, digits and dashes, like send-welcome.
Settings
| Setting | Meaning |
|---|---|
| Enabled | Off means calls answer 404 |
Signed-in accounts only (requireUser) | Refuse calls without a user's token (401) |
Time limit (timeoutSeconds) | 1 to 60 seconds; 10 unless you set it |
Run by itself every … minutes (everyMinutes) | A timetable; none by default |
| Secrets | The names of the project's secrets this function is given |
| Version | Which stored version is live |
Call one
POST https://api.rezincore.com/functions/v1/send-welcome
apikey: <one of the project's keys>
content-type: application/json
{"email":"[email protected]"}
curl -s https://api.rezincore.com/functions/v1/send-welcome \
-H "apikey: $KEY" -H "authorization: Bearer $TOKEN" \
-H "content-type: application/json" -d '{"email":"[email protected]"}'
Any of GET, POST, PUT, PATCH and DELETE, any path after the name, any query. The function sees the request as sent, without cookies. A signed-in user adds Authorization: Bearer <their token>.
The answer is the function's own status, headers and body, with three changes:
Set-Cookieand cross-origin headers are dropped;- a page type (
text/htmland the like) is sent astext/plain, so a function cannot serve a web page from the API's address; x-rezin-function-runnames the run.
| Status | Code | Meaning |
|---|---|---|
| 401 | bad_key, sign_in_required | No valid key, or the function needs a signed-in user |
| 404 | not_found | No such function in this project, or it is switched off |
| 429 | too_many_requests, busy | The project's limit per minute, or too many running at once |
| 500 | function_error | The code threw. The error is in the function's log, not in the answer. |
| 502 | function_memory, function_output, function_crashed | Stopped for using too much, or ended without answering |
| 504 | function_timeout | Stopped at its time limit |
On a timetable
Set "run by itself every … minutes", from 1 minute to a week. The function is called with POST and the body {"type":"schedule","at":"…"}, and context.trigger is "schedule".
A run that is missed while the service is restarting happens once when it is back, not once per missed slot.
When a table changes
On the Webhooks page choose one of the project's functions instead of an outside address. The function receives the same signed JSON message an outside address would, and the message never leaves Rezin Core. See Database webhooks.
Limits
| What | Limit |
|---|---|
| Functions in a project | 20 |
| Code in one function | 256 KB, one module |
| Versions kept | The newest 20 |
| Time for one run | 10 seconds unless you set it, 60 at most |
| Memory for one run | 128 MB |
| Request body | 1 MB |
| Answer | 6 MB |
| Outgoing requests in one run | 20 |
| Runs | 120 a minute, and 4 at once, for the project |
| Log | 200 lines a run, kept 7 days |
What is recorded
Every deploy, settings change and deletion is on the project's audit record with who did it, from where (console, command line or assistant) and, for code, its size and a fingerprint. Never the code. Each run is one line on the function's page: what started it, how it ended, how long it took. The request and the answer are not kept.
Rezin Core's staff cannot open a project's functions, their code, their logs or the names of their secrets, exactly as with the rest of a project.
What the isolation does and does not guarantee
What it does. Each run is a separate process started inside the operating system's sandbox with everything refused except what is listed:
- it can read the language runtime's own program files and nothing else on the disk;
- it cannot write anywhere;
- it cannot open any network connection, not even to the machine it is on;
- it cannot start another program or signal one;
- it is started with none of the server's settings.
A second, independent layer inside the runtime refuses the same file, process and add-on calls. Its requests for the outside world are carried out by the server, which checks every address after looking it up, connects only to the address it checked, and checks every redirect again. A run is stopped at its time limit, at twice its memory limit, and when it prints or returns more than is allowed. Before the first run after each start, the server proves that the sandbox alone stops a test program from reading real files, starting a program and connecting to a local port; if that proof fails, no function is run. All of this is exercised by the test suite with hostile code.
What it does not.
- The sandbox is a feature of the operating system and is only as strong as that system: a flaw in its kernel or in the language runtime could let code out, as with any provider that runs customers' code.
- Functions share one machine's processor with everything else on it. There is no hard processor quota beyond the time limit and the cap on how many run at once, so a project using all of its allowance can slow others down.
- A function can learn which runtime version it runs on.
- A function can send anything it is given (its request, its assigned secrets, data it reads through the API with the key you gave it) to any public address. Assign a function only the secrets it needs, and give it the publishable key rather than the secret key when that is enough.