Skip to content
v2.7

What the code receives

The exported function, the request and response, the OWBASE_FUNCTION_* variables and the x-owbase-* headers.

The entrypoint exports an async function. On each execution, it receives an object with the request (req), the response (res) and the log (log, error), and it must return a response.

req. What
method, path, query, queryString HTTP method, path, query as an object and as text
headers request headers, in lowercase
bodyText, bodyJson body as text, or parsed from JSON (throws if the body isn’t JSON)
host, scheme, url where the request came from
res. Returns
json(object, status?, headers?) JSON
text(text, status?, headers?) text
empty() 204, no body
redirect(url, status?) redirect (301 by default)

log(...) and error(...) go to the execution’s Logs and Errors (if Keep logs and errors of executions is turned on in Settings).

Variable Value
OWBASE_FUNCTION_API_ENDPOINT https://cloud.owbase.dev/v1: use it in the SDK’s setEndpoint
OWBASE_FUNCTION_PROJECT_ID project ID
OWBASE_FUNCTION_ID, OWBASE_FUNCTION_NAME function ID and name
OWBASE_FUNCTION_DEPLOYMENT ID of the live deployment
OWBASE_FUNCTION_RUNTIME_NAME, OWBASE_FUNCTION_RUNTIME_VERSION runtime (Node, 22)
OWBASE_FUNCTION_CPUS, OWBASE_FUNCTION_MEMORY CPU and memory from the specification
OWBASE_VERSION Owbase server version

Your own variables are added to these; if a name repeats, Owbase’s wins.

Header Value
x-owbase-key API key for this execution, with the scopes from Settings → Execution key scopes. It is valid only while the execution lasts
x-owbase-trigger http, schedule or event
x-owbase-event the event that triggered it (when trigger is event)
x-owbase-user-id, x-owbase-user-jwt who called, if a user was signed in
x-owbase-execution-id execution ID
x-owbase-client-ip caller’s IP
x-owbase-country-code, x-owbase-continent-code, x-owbase-continent-eu IP location, when known

x-owbase-* headers that the client tries to send are discarded: the code only sees Owbase’s.

A function that writes orders to the database with the execution key (give it the rows.write scope in Settings):

src/main.js
import { Client, ID, TablesDB } from '@owbase/node';
// POST /orders saves an order with the execution key (grant the rows.write scope under Settings → Scopes of the execution key).
export default async ({ req, res, log, error }) => {
const client = new Client()
.setEndpoint(process.env.OWBASE_FUNCTION_API_ENDPOINT)
.setProject(process.env.OWBASE_FUNCTION_PROJECT_ID)
.setKey(req.headers['x-owbase-key'] ?? '');
if (req.method === 'GET') return res.json({ ok: true, function: process.env.OWBASE_FUNCTION_ID });
if (req.method !== 'POST' || req.path !== '/orders') return res.json({ error: 'route not found' }, 404);
let order;
try {
order = JSON.parse(req.bodyText || '{}');
} catch {
return res.json({ error: 'the body must be JSON' }, 400);
}
if (typeof order.total !== 'number') return res.json({ error: 'provide total (number)' }, 400);
try {
const row = await new TablesDB(client).createRow({
databaseId: 'store',
tableId: 'orders',
rowId: ID.unique(),
data: { customer: req.headers['x-owbase-user-id'] || 'anonymous', total: order.total },
});
log(`order ${row.$id} saved (trigger: ${req.headers['x-owbase-trigger']})`);
return res.json({ id: row.$id }, 201);
} catch (e) {
error(`failed to save the order: ${e.message}`);
return res.json({ error: 'could not save the order' }, 500);
}
};
package.json
{
"name": "orders",
"private": true,
"type": "module",
"dependencies": {
"@owbase/node": "^1.0.1"
}
}

To call the database as the user who made the request (respecting their permissions) instead of using the key, use setJWT(req.headers['x-owbase-user-jwt']) in place of setKey.

Was this page helpful?