API keys and roles
Every request that your AWS Amplify app sends to the Amplify backend data API runs as one of three PostgreSQL roles: anon, authenticated or service_role.
Roles
The data API sets the role from the credentials in the request:
| Credentials in the request | Role | Use it for |
|---|---|---|
|
The publishable key, in the |
|
Visitors who aren’t signed in |
|
The publishable key, and a user’s token in |
|
A signed-in user. In a row-level security (RLS) policy, |
|
An AWS Signature Version 4 (SigV4) signature |
|
Server code only. This role bypasses RLS |
A client that you create with the publishable key runs as anon until a user signs in. After that, the client SDK adds the user’s token to each request. You set what anon and authenticated can read and change with the grants and RLS policies in your migrations. For more information about grants and RLS policies, see Secure your data.
The publishable key
You can put the publishable key in browser and mobile code. Anyone who opens your website can read the key. Each backend branch has its own key, and the key doesn’t expire.
Amplify sets the key in every deployment as the environment variable AMPLIFY_BACKEND_PUBLISHABLE_KEY. To see its value, run aws amplifygen3 deployment get --output json and read EnvironmentVariables.
Send the key as the apikey header on every request to /rest and /auth, whether or not a user is signed in. The client SDK sends it for you. Put only a user’s token in the Authorization header. If you send the key in the Authorization header, the backend returns 401.
Server access with service_role
The backend has no secret key. Server code gets service_role by signing each request with AWS credentials. A signed request doesn’t need the publishable key. To sign requests to a deployed backend, use an identity in the backend’s AWS account that has the amplifygen3:Connect permission. For more information about the key and signed requests on the local backend, see Develop locally.
import { createClient } from '@aws-amplifygen3/backend-client-js' import { fromNodeProviderChain } from '@aws-sdk/credential-providers' // Server code only: this client bypasses RLS. const admin = createClient(process.env.AMPLIFY_BACKEND_URL!, { awsCredentialsProvider: fromNodeProviderChain(), })
When your code runs outside AWS, such as a script on your computer, set AWS_REGION to your app’s AWS Region. A deployed backend URL doesn’t include an AWS Region, so without AWS_REGION or AWS_DEFAULT_REGION, each SDK call fails with Cannot determine the SigV4 signing region.
Warning: Anyone who gets AWS credentials with the amplifygen3:Connect permission can read and change every row. Keep the code that signs requests on the server, and never put AWS credentials in browser code, in a NEXT_PUBLIC_* or VITE_* variable, or in a commit.
For more information about the IAM permission, see Permissions for your app.
Manage a deployed backend’s keys
Amplify creates a publishable key for a backend branch in the branch’s first deployment, and sets the same key in later deployments while the key is active. If you revoke the key, Amplify creates a new one in the next deployment. Use the following commands to see and revoke keys:
| Command | What it does |
|---|---|
|
|
Lists each key’s ID, status, and the time it last changed, without its value. Marks the key that the live deployment uses as |
|
|
Prints one key’s value |
|
|
Turns a key off permanently after you confirm. Add |
Each command takes --env (default preview) and --app, or --branch-arn in place of both. When the environment has more than one branch, the command asks you to choose one. When CI=true is set, the command exits with code 252 instead, so pass --branch-arn and --id.
When list can’t tell which key the live deployment uses, it prints a warning, and --output json shows "Deployed": null. Your live app might use a key marked null. The revoke command warns and revokes that key without the deployed-key check.
Replace a key
When you deploy with --create-publishable-key, Amplify creates a new key and sets it in that deployment and every later one. The old key stays active until you revoke it, so a mobile app that you release separately keeps working until you release a version with the new key.
-
Run the following command to deploy with a new key:
aws amplifygen3 deploy --env production --create-publishable-key -
Run the following command to list the keys:
aws amplifygen3 backend publishable-key list --env productionYou should see the new key marked
[deployed]and the old key stillACTIVE. -
Give the new key to every other client that calls the backend. To print it, run
aws amplifygen3 backend publishable-key get --env production --id <new-key-id>. -
When no client uses the old key, run the following command to revoke it:
aws amplifygen3 backend publishable-key revoke --env production --id <old-key-id>
A revoked key can keep working for up to 5 minutes after you revoke it.
An instant rollback brings back the publishable key that the older deployment was built with. If you revoked that key, your app’s backend requests get 401
invalid_credential until you deploy again.
Revoke the deployed key
Warning: Revoking the key that your live deployment uses breaks your deployed app. The backend returns 401 to the app’s requests until you deploy again and Amplify sets a new key. You can’t undo a revocation.
To revoke the deployed key, add --allow-deployed-key. Without it, the command revokes nothing, even with --yes. To revoke it and restore your app:
-
Run the following command to revoke the key:
aws amplifygen3 backend publishable-key revoke --env production --id <key-id> --allow-deployed-key --yes -
Run the following command to deploy again and give your app a new key:
aws amplifygen3 deploy --env production
Errors
The following table lists publishable key errors and how to fix them:
| Error | Cause | Fix |
|---|---|---|
|
|
No |
Send the publishable key as |
|
|
A revoked or mistyped key, or a key from another backend |
Use the key and the backend URL from the same environment |
|
|
The |
Send the publishable key as |
|
|
A user token whose session ended with sign-out, a ban, or the user’s removal. A deployed backend refuses it within 30 seconds. The message names no credential |
Sign the user in again |
|
|
The branch has 5 active keys, its limit. Revoked keys don’t count |
Revoke an unused key, and deploy again. For more information about the limit, see Quotas |