

# Call the data API over HTTP

You can call the Amplify backend data API of your AWS Amplify app with HTTP requests from other languages and tools, such as Python, Go or `curl`. Each table is at `<backend URL>/rest/<table>`, and each database function is at `<backend URL>/rest/rpc/<function>`. In JavaScript and TypeScript code, use the client SDK instead. The SDK keeps the user’s session and gets a new token when the old one expires. For more information, see the [client SDK README](https://github.com/aws-amplifygen3/amplifygen3/blob/main/packages/client-sdk/js/README.md) on GitHub.

## Get the backend URL and publishable key
<a name="_get_the_backend_url_and_publishable_key"></a>

Run the following commands to read the backend URL and the publishable key from the active deployment of an environment. Replace `preview` with the name of your environment.

```bash
API=$(aws amplifygen3 backend info --env preview --query BackendUrl --output text)
KEY=$(aws amplifygen3 backend info --env preview --query PublishableKey --output text)
```

For more information about the local backend’s URL and key, see [Develop locally](backend-local.md).

## Call as a visitor
<a name="_call_as_a_visitor"></a>

Send the publishable key in the `apikey` header of every request. A request with only the key runs as the `anon` role. Run the following command to read the `products` table as a visitor:

```bash
curl -s "$API/rest/products?select=id,name" -H "apikey: $KEY"
```

For more information about roles, see [API keys and roles](backend-keys.md).

## Call as a signed-in user
<a name="call-as-a-signed-in-user"></a>

Get a token for the user’s session, and send it with each request:

1. Run the following command to sign the user in and save the session cookie that the backend returns in the file `jar`:

   ```bash
   curl -s -c jar -X POST "$API/auth/sign-in/email" -H "apikey: $KEY" -H 'content-type: application/json' \
     -d '{"email":"a@example.com","password":"correct horse battery"}'
   ```

1. Run the following command to get a token for the session:

   ```bash
   JWT=$(curl -s -b jar -H "apikey: $KEY" "$API/auth/token" | jq -r '.token')
   ```

1. Run the following command to send the token in the `Authorization` header, together with the publishable key:

   ```bash
   curl -s "$API/rest/notes?select=id,body&order=created_at.desc&limit=20" \
     -H "apikey: $KEY" -H "Authorization: Bearer $JWT"
   ```

A token expires after 15 minutes. Reuse the same token for your requests, and get a new one before it expires. When a request gets `401`, call `GET /auth/token` once with the session cookie, and send the request again.

## Call with full access from server code
<a name="_call_with_full_access_from_server_code"></a>

Server code can sign a request with AWS credentials, using AWS Signature Version 4 (SigV4). A signed request needs no publishable key. It runs as `service_role` and can read and change every row. Sign requests only in code that runs on your server, never in browser or mobile app code. The identity that signs needs the `amplifygen3:Connect` permission in the backend’s AWS account. A signed request body can be up to 1 MiB. A signed body over 64 KiB gets `503` `SERVICE_UNAVAILABLE` with `Retry-After` while the backend is busy with other large signed requests. Send it again after the wait.

A signed request uses the signing name `amplifygen3`. A deployed backend URL doesn’t include an AWS Region, so the request signs for the AWS Region in `AWS_REGION` or `AWS_DEFAULT_REGION`. When your code runs outside AWS, such as a script on your computer, set `AWS_REGION` to your app’s AWS Region. Without either variable, each SDK call fails with `Cannot determine the SigV4 signing region`.

For more information about signed calls with the SDK, see [Call the backend with full access](backend-server-code.md#backend-server-code-full-access).

## Read, write and call functions
<a name="_read_write_and_call_functions"></a>

Each operation uses its own HTTP method. Filters, sorting and paging are query parameters, such as `id=eq.1`, `order=created_at.desc` and `limit=50`. Run the following commands to read, insert, update and delete rows, and to call a function. They use the headers from [Call as a signed-in user](#call-as-a-signed-in-user):

```bash
H=(-H "apikey: $KEY" -H "Authorization: Bearer $JWT" -H 'content-type: application/json')

ID=$(curl -s -X POST "$API/rest/notes" "${H[@]}" -H 'Prefer: return=representation' \
  -d '{"body":"hello"}' | jq -r '.[0].id')  # insert
curl -s "$API/rest/notes?select=id,body&id=eq.$ID" "${H[@]}"                                  # read
curl -s -X PATCH "$API/rest/notes?id=eq.$ID" "${H[@]}" -H 'Prefer: return=representation' \
  -d '{"body":"edited"}'  # update
curl -s -X DELETE "$API/rest/notes?id=eq.$ID" "${H[@]}" -H 'Prefer: return=representation'           # delete
curl -s -X POST "$API/rest/rpc/notes_since" "${H[@]}" -d '{"since":"2026-01-01"}'                # call a function
```

The insert often returns an `id` other than 1, because identity values aren’t reused, so the commands keep the `id` that it returns.

For more information about filters and related tables, see [Read data](backend-data-api-query.md). For more information about upserts and changed rows, see [Write data](backend-data-api-write.md). For more information about functions, see [Call database functions](backend-data-api-rpc.md).

## Call from browser code
<a name="_call_from_browser_code"></a>

If you call the data API with `fetch` from browser code, pass `credentials: 'include'` on your `/auth` calls, such as sign-in and `GET /auth/token`, so that the browser sends and keeps the session cookie. On `/rest` calls, send the user’s token in the `Authorization` header. Amplify allows calls from the Amplify URLs and custom domains of your environment. To call from another origin, add the origin to `trustedOrigins`. For more information about trusted origins, see [Auth configuration](backend-auth-config.md).

## Headers
<a name="_headers"></a>


| Header | Direction | Use | 
| --- | --- | --- | 
|  `apikey`  | Request | The publishable key. Send it on every request to `/rest` and `/auth`, except a signed request | 
|  `Authorization`  | Request |  `Bearer <token>` for a signed-in user. Don’t put the publishable key here | 
|  `content-type`  | Request |  `application/json` for a request body | 
|  `Prefer`  | Request |  `return=representation` returns the changed rows. `count=exact` returns the row count. `resolution=merge-duplicates`, with `on_conflict=<column>`, makes an insert an upsert | 
|  `Range`  | Request | Not used. The deployed data API ignores it. To read a page of rows, use the `limit` and `offset` query parameters | 
|  `Content-Range`  | Response | The range of rows returned, and the total when you send `count=exact`  | 

## Limits and errors
<a name="_limits_and_errors"></a>

A read returns at most 1,000 rows. To read more, page with `limit`, `offset` and `order`. A request body can be up to 10 MiB on `/rest` and 1 MiB on `/auth`. A larger body gets `413` `PAYLOAD_TOO_LARGE`. For more information about error codes and their fixes, see [Data API errors](backend-data-api-errors.md).