

# Call database functions

Your AWS Amplify app calls the functions in your `public` schema through the Amplify backend data API, with `rpc` from the client SDK or over HTTP. The data API serves each function at `/rest/rpc/<name>`. A function can compute a result from rows that the caller can’t read, such as which booking slots are taken.

## Create a function
<a name="_create_a_function"></a>

Create the function in a migration, and grant `execute` on it to each role that calls it:

```sql
create function public.notes_since(since timestamptz) returns setof public.notes
  language sql stable set search_path = ''
  as $$ select * from public.notes where created_at >= since $$;

-- A new function has no execute grant for anon or authenticated.
grant execute on function public.notes_since(timestamptz) to authenticated;
```

This function runs as the caller, so it returns the rows allowed by the row-level security (RLS) policies that you configured on `notes`. For more information about grants and policies, see [Secure your data](backend-rls.md).

A `GET` call, and a `POST` call to a function declared `stable` or `immutable`, run in a read-only transaction, so a function that writes fails there. Keep a function that writes `volatile`, the default, and call it with `POST`.

Each migration reloads the schema of the data API, so the data API serves the function as soon as the migration runs. If you create a function with `aws amplifygen3 db query`, the data API returns `PGRST202` for it until you reload the schema. Run the following command to reload it. Replace `production` with the name of your environment.

```bash
aws amplifygen3 db query "notify pgrst, 'reload schema'" --env production
```

## Call a function from the client SDK
<a name="_call_a_function_from_the_client_sdk"></a>

Pass `rpc` the function name and its arguments, as an object keyed by argument name:

```ts
const { data, error } = await backend.rpc('notes_since', { since: '2026-01-01T00:00:00Z' })
```

To call a function that only reads with `GET`, pass `{ get: true }`. The SDK then sends the arguments in the query string, as in `GET /rest/rpc/notes_since?since=2026-01-01T00:00:00Z` over HTTP:

```ts
const { data, error } = await backend.rpc('notes_since', { since: '2026-01-01T00:00:00Z' }, { get: true })
```

When the function returns rows, select, filter, order and page them as you do with a table:

```ts
const { data, error } = await backend
  .rpc('notes_since', { since: '2026-01-01T00:00:00Z' })
  .select('id, body')
  .order('created_at', { ascending: false })
  .range(0, 49) // the data API returns at most 1,000 rows per call, as for any read
```

## Call a function over HTTP
<a name="_call_a_function_over_http"></a>

Send a `POST` request to `/rest/rpc/<name>` with the arguments as a JSON object. Filters and other query parameters go in the URL. Run the following command to call `notes_since` as a signed-in user:

```bash
# API, KEY and JWT: the backend URL, the publishable key and a user's token
curl -s -X POST "$API/rest/rpc/notes_since?select=id,body&order=created_at.desc&limit=50" \
  -H "apikey: $KEY" -H "Authorization: Bearer $JWT" \
  -H 'content-type: application/json' -d '{"since":"2026-01-01T00:00:00Z"}'
```

A function that only reads can also take a `GET`, with the arguments as query parameters:

```bash
curl -s "$API/rest/rpc/notes_since?since=2026-01-01T00:00:00Z" -H "apikey: $KEY" -H "Authorization: Bearer $JWT"
```

A call without a token runs as `anon`, so a missing grant gets `401` instead of `403`. For more information about setting `API`, `KEY` and `JWT`, see [Call the data API over HTTP](backend-data-api-http.md). For more information about the role that each request runs as, see [API keys and roles](backend-keys.md).

## Place security definer functions
<a name="_place_security_definer_functions"></a>

A `security definer` function runs as its owner and bypasses RLS. Never create one to get past `permission denied`. Fix the grant or the policy instead.

When you need one, create it in the schema that matches what calls it:
+  **Called only by your RLS policies** – Create it in a schema of your own, such as `private`, so that the data API doesn’t serve it. Grant `usage` on the schema and `execute` on the function to each role that those policies apply to.
+  **Called by your app** – Create it in `public`. Any role that you grant `execute` can call it at `/rest/rpc/<name>`, so grant only the role that needs it.

Give the function `set search_path = ''`, and qualify every name with its schema. If the function doesn’t set `search_path`, `aws amplifygen3 db lint --schema public,private --env <env>` warns `function_search_path_mutable`. Return only what the caller may see: check `(select auth.uid())` in its body, or return only columns that name no user. In the following example, the RLS policies that you configured on `bookings` are owner-only: each user can read only their own rows. The `taken_slots` function returns the `slot_id` of every booking:

```sql
create function public.taken_slots() returns table (slot_id bigint)
  language sql stable security definer set search_path = ''
  as $$ select slot_id from public.bookings $$;

grant execute on function public.taken_slots() to authenticated; -- your app calls backend.rpc('taken_slots')
```

## Errors
<a name="_errors"></a>


| Error | Cause | Fix | 
| --- | --- | --- | 
|  `42501`, `permission denied for function`  | The caller’s role has no `execute` grant. The status is `401` without a user’s token and `403` with one | Grant `execute` to that role in a new migration | 
|  `PGRST202`  | No such function in `public`, or one created with `db query` before a schema reload | Check the name and schema, or reload the schema | 
|  `PGRST101` (`405`) | A method other than `GET`, `HEAD`, `POST`, or `OPTIONS` on `/rest/rpc/<name>`  | Call the function with `POST`, or with `GET` for a function that only reads | 
|  `function_search_path_mutable`, from `aws amplifygen3 db lint --schema public,private --env <env>`  | The function has no `set search_path = ''`  | Add it | 

For more information about other error codes, see [Data API errors](backend-data-api-errors.md).