

# 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
<a name="_roles"></a>

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 `apikey` header |  `anon`  | Visitors who aren’t signed in | 
| The publishable key, and a user’s token in `Authorization: Bearer <token>`  |  `authenticated`  | A signed-in user. In a row-level security (RLS) policy, `auth.uid()` returns the user’s ID as `text`  | 
| An AWS Signature Version 4 (SigV4) signature |  `service_role`  | 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](backend-rls.md).

## The publishable key
<a name="_the_publishable_key"></a>

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`
<a name="_server_access_with_service_role"></a>

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](backend-local.md).

```ts
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**  
 **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](add-IAM-roles.md).

## Manage a deployed backend’s keys
<a name="_manage_a_deployed_backends_keys"></a>

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 | 
| --- | --- | 
|  `aws amplifygen3 backend publishable-key list`  | Lists each key’s ID, status, and the time it last changed, without its value. Marks the key that the live deployment uses as `[deployed]`. `--status ACTIVE` or `--status INACTIVE` filters the list | 
|  `aws amplifygen3 backend publishable-key get --id <key-id>`  | Prints one key’s value | 
|  `aws amplifygen3 backend publishable-key revoke --id <key-id>`  | Turns a key off permanently after you confirm. Add `--yes` to skip the prompt. When `CI=true` is set, `--yes` is required | 

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
<a name="_replace_a_key"></a>

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.

1. Run the following command to deploy with a new key:

   ```bash
   aws amplifygen3 deploy --env production --create-publishable-key
   ```

1. Run the following command to list the keys:

   ```bash
   aws amplifygen3 backend publishable-key list --env production
   ```

   You should see the new key marked `[deployed]` and the old key still `ACTIVE`.

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

1. When no client uses the old key, run the following command to revoke it:

   ```bash
   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
<a name="_revoke_the_deployed_key"></a>

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

1. Run the following command to revoke the key:

   ```bash
   aws amplifygen3 backend publishable-key revoke --env production --id <key-id> --allow-deployed-key --yes
   ```

1. Run the following command to deploy again and give your app a new key:

   ```bash
   aws amplifygen3 deploy --env production
   ```

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

The following table lists publishable key errors and how to fix them:


| Error | Cause | Fix | 
| --- | --- | --- | 
|  `401` `invalid_credential`, `No apikey was sent.`  | No `apikey` header, even with a valid user token | Send the publishable key as `apikey`  | 
|  `401` `invalid_credential`, `The apikey is not a valid key for this backend.`  | A revoked or mistyped key, or a key from another backend | Use the key and the backend URL from the same environment | 
|  `401` `invalid_credential`, `The bearer token is not valid for this backend.`  | The `Authorization` header holds something that isn’t a valid user token for this backend: an expired token, a token from another backend, or the publishable key | Send the publishable key as `apikey`, and send only a user’s token as `Bearer`. Get a new token from `GET /auth/token`. The SDK does this for you | 
|  `401` `invalid_credential`, `invalid credential`  | 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 | 
|  `PUBLISHABLE_KEY_LIMIT_EXCEEDED`  | 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](quotas-chapter.md)  | 