# Calling functions (RPC)

Source: https://cubibase.com/docs/data-api/rpc

Also available: [Markdown version](https://cubibase.com/docs/data-api/rpc.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

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**

```http
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**

```json
{
  "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.

> **note**: Only POST is served. A function that writes rows is still a POST — the method selects the call, not the effect. Statements run with the api pool’s 8-second timeout.

## 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:

**A function that respects RLS**

```sql
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.

> **important**: EXECUTE on the function is not a grant on the tables it reads. The api role still needs SELECT (or write) privileges on those tables — expose the table in the Console, or the call fails with 403 forbidden. RLS then narrows the rows per user.

## 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 |

> **note**: Constraint failures inside the function surface with the same codes as table writes: conflict, foreign_key_violation, check_violation. RPC calls appear in the project request logs with the argument names and the user id.

## Additional resources

- [Functions](https://cubibase.com/docs/database/functions) Writing and granting functions in the Console.
- [Row-level security](https://cubibase.com/docs/database/row-level-security) The policies an INVOKER function runs under.
- [API routes](https://cubibase.com/docs/reference/routes) Every route on the project host.
