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:

RPC · request
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"
}
200 · response
{
  "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 typedataExample
Scalar (integer, text, jsonb, …)The value itself{ "data": 2 }
SETOF a scalarAn array of values{ "data": ["a-note", "b-note"] }
SETOF a table or compositeAn array of row objects{ "data": [{ "id": "…", "body": "…" }] }
Composite / OUT parametersOne row object{ "data": { "total": 12, "mine": 3 } }
voidnull{ "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:

A function that respects RLS
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

StatusCodeMeaning
401missing_api_key / missing_end_user_tokenSame credential rules as table routes
404not_foundNo such function, no EXECUTE grant, or no overload matches the argument names
400invalid_bodyBody is not an object, or a value does not fit the parameter type
403forbiddenA policy or a missing table grant blocked the function body
300ambiguous_functionSeveral overloads match; details lists the signatures
408statement_timeoutThe function ran past the statement timeout
413result_too_largeA set-returning function produced more than 10000 rows

Additional resources