View a markdown version of this page

Data API - AWS Amplify

Data API

Each Amplify backend includes a data API, an HTTPS API that reads and writes the tables in your database and calls its database functions. AWS Amplify serves the data API from your database schema. When a migration adds a table or a function to the public schema, the data API serves it. Your app calls the data API with the client SDK in JavaScript and TypeScript, or with HTTP requests from other languages.

About the data API

The data API has the following characteristics:

  • PostgREST-compatible queries – Filters, ordering, paging, embedded related tables and function calls use PostgREST query syntax. The client SDK for JavaScript and TypeScript builds each query from method calls.

  • Tables and functions in the public schema – To keep a function out of the data API, create it in another schema, such as private, in a migration.

  • The /rest path on the backend URL – The data API is at the /rest path of the backend URL. Each table has its own path under /rest, such as /rest/notes for a table named notes. The path has no version, so write /rest/notes, not /rest/v1/notes. A versioned path gets 404 with PGRST125. Functions are at /rest/rpc/<name>, and auth is at /auth. Each deployment sets the backend URL as AMPLIFY_BACKEND_URL, and aws amplifygen3 backend info --env preview prints it as BackendUrl. Locally, aws amplifygen3 local run writes the URL to local.endpoint in amplify.local.json. Read it from there, because the port can change.

  • Read limits – One read returns at most 1,000 rows. Row counts must be exact, and the data API refuses planned and estimated counts. For more information, see Read data.

  • Private until a migration grants access – Amplify turns on row-level security (RLS) for each new table and adds no grants to it. To let your app read and write a table, add a grant and RLS policies for it in a migration. To let your app call a new function, grant access to it in a migration. For more information about grants and policies, see Secure your data.

  • Requests that run as your users, or with full access – Your website sends the publishable key with each request. A request runs as a visitor, or as the signed-in user when your website also sends the user’s token. Server code can sign its requests with AWS credentials, using AWS Signature Version 4 (SigV4). A signed request has full access to every table. Keep AWS credentials in server code only. In policies and functions, auth.uid() returns the user’s ID as text, auth.role() and auth.email() return the role and email address, and auth.jwt() returns the token’s claims as jsonb. For more information about keys and roles, see API keys and roles.

  • Retries while the database restarts – The database restarts during a failover or maintenance, and calls get a 503 status and a Retry-After header. The client SDK sends the call again after the time in that header. When you call over HTTP, wait for the same time, and send the request again from your code.

  • No realtime subscriptions or GraphQL – The backend serves the data API at /rest and auth at /auth, and has no realtime subscriptions and no GraphQL endpoint. To show changes as they happen, have your app poll the table.

Work with the data API

Use the following pages to work with the data API:

  • Read data – Select, filter, count, order and page rows, and embed related tables, with the client SDK or over HTTP.

  • Write data – Insert, update, upsert and delete rows, and get the changed rows back.

  • Call the data API over HTTP – Call the data API from another language or tool, as a visitor, as a signed-in user or with full access.

  • Call database functions – Call a function in the public schema with the client SDK or over HTTP, and grant access to it.

  • API keys and roles – Send the publishable key, see which role each request runs as, and rotate the key.

  • Generate TypeScript types – Generate types from your schema, and use them with the client SDK.

  • Data API errors – Look up an error code’s cause and fix, including empty results when the RLS policies that you configured match no rows.