Also available: Markdown version · llms.txt · full site content
Data API
Calling functions (RPC)
A function you grant to the Data API role is callable at POST /rest/v1/rpc/{name}. Same host, same publishable key, same end-user JWT. Arguments are a JSON object; the result comes back in data.
The call
Write the function in the Console (Database, Functions), then switch on Grant Data API EXECUTE. The function is then served on the project host:
POST https://example-project.api.cubibase.com/rest/v1/rpc/customer_count
apikey: cb_pub_EXAMPLE_ONLY
Authorization: Bearer <USER_JWT>
Content-Type: application/json
{
"plan_name": "pro"
}{
"data": 2
}The body is a JSON object keyed by parameter name. Values are bound as parameters and cast to the declared types, so "42" works for an integer and a JSON object feeds a jsonb parameter. Omit a key to use the parameter default; send {} (or no body) for a function without arguments.
What data contains
| Return type | data | Example |
|---|---|---|
Scalar (integer, text, jsonb, …) | The value itself | { "data": 2 } |
SETOF a scalar | An array of values | { "data": ["a-note", "b-note"] } |
SETOF a table or composite | An array of row objects | { "data": [{ "id": "…", "body": "…" }] } |
| Composite / OUT parameters | One row object | { "data": { "total": 12, "mine": 3 } } |
void | null | { "data": null } |
Results are JSON: bigint and numeric arrive as JSON numbers, timestamps as ISO strings. A set-returning function is capped at 10000 rows per call; page inside the function with arguments when you need more.
Identity and row-level security
The call runs as the api role with cubibase.user_id set to the JWT subject, exactly like a table request. A SECURITY INVOKER function sees only the rows the caller’s policies allow:
CREATE FUNCTION public.my_note_bodies()
RETURNS SETOF text
LANGUAGE sql
STABLE
SECURITY INVOKER
SET search_path = public, pg_catalog
AS $$
SELECT body FROM public.notes ORDER BY body;
$$;Two users calling my_note_bodies get two different arrays. SECURITY DEFINER runs as the function owner instead and can bypass the caller’s policies — grant it deliberately and validate arguments inside the body.
Overloads and defaults
- Parameters are matched by name. Keys you send must exist on the function; required parameters must be present or have defaults.
- When several overloads accept the same keys, an exact match wins. A tie answers 300 ambiguous_function with the candidate signatures in details.
- Functions with unnamed parameters cannot be called with a JSON object; name the parameters in the Console.
- Defaults are applied by PostgreSQL, so the function body sees the same values as a direct SQL call.
Errors
| Status | Code | Meaning |
|---|---|---|
401 | missing_api_key / missing_end_user_token | Same credential rules as table routes |
404 | not_found | No such function, no EXECUTE grant, or no overload matches the argument names |
400 | invalid_body | Body is not an object, or a value does not fit the parameter type |
403 | forbidden | A policy or a missing table grant blocked the function body |
300 | ambiguous_function | Several overloads match; details lists the signatures |
408 | statement_timeout | The function ran past the statement timeout |
413 | result_too_large | A set-returning function produced more than 10000 rows |