

# Add server code to your app

Server code in your AWS Amplify app, such as a Next.js route handler, deploys with your app. It calls the Amplify backend with the backend client SDK, either with full access or as the signed-in user.

## Install the SDK
<a name="backend-server-code-install"></a>

Your server code calls the backend with the backend client SDK. Run the following command in your app’s folder to install it:

```bash
npm install @aws-amplifygen3/backend-client-js @aws-sdk/credential-providers
```

The SDK signs full-access requests with the AWS credentials that it gets from `@aws-sdk/credential-providers`. For more information, see the [client SDK README](https://github.com/aws-amplifygen3/amplifygen3/blob/main/packages/client-sdk/js/README.md) on GitHub.

## Call the backend with full access
<a name="backend-server-code-full-access"></a>

To read or change every row, such as for an admin page or a report across users, create the client with the backend URL, `AMPLIFY_BACKEND_URL`, and `awsCredentialsProvider`, with no publishable key. The client runs as `service_role`: it reads and changes every row, and your row-level security (RLS) policies don’t filter it. For the roles, see [API keys and roles](backend-keys.md).

```typescript
// app/api/stats/route.ts
import { createClient } from '@aws-amplifygen3/backend-client-js'
import { fromNodeProviderChain } from '@aws-sdk/credential-providers'

export async function GET() {
  const admin = createClient(process.env.AMPLIFY_BACKEND_URL!, {
    awsCredentialsProvider: fromNodeProviderChain(),
  })

  const { count, error } = await admin.from('notes').select('*', { count: 'exact', head: true })
  if (error) return Response.json({ error: error.message }, { status: 500 })
  return Response.json({ totalNotes: count })
}
```

Amplify sets `AMPLIFY_BACKEND_URL` in every deployment, and the SDK signs each request with the credentials of the environment’s IAM role. The role is the credential, so full access needs no key. It works in the default `scoped` permissions mode, and your only setup is installing `@aws-sdk/credential-providers`. For the permissions of that role, see [Permissions for your app](add-IAM-roles.md).

**Important**  
A full-access route returns data to anyone who calls it. Check who the caller is before the route returns or changes data, and keep full-access clients out of browser code.

To create, change or delete users from server code, call `createServerAdmin`, exported from `@aws-amplifygen3/auth-js/server`. The Install the SDK step doesn’t install that package. For more information, see [Manage users](backend-users.md).

## Act as the signed-in user
<a name="backend-server-code-as-user"></a>

To return only the rows that a user may see, create the client with your publishable key and the user’s token. The RLS policies that you configured apply to each request. For more information, see [Secure your data](backend-rls.md).

```typescript
import { createClient } from '@aws-amplifygen3/backend-client-js'

const asUser = createClient(process.env.AMPLIFY_BACKEND_URL!, process.env.AMPLIFY_BACKEND_PUBLISHABLE_KEY!, {
  webTokenProvider: () => token, // the user's token
})
const { data, error } = await asUser.from('notes').select('id, body')
```

Your server code gets the user’s token from the user’s session, or from an `Authorization: Bearer <token>` header that your browser code sends. To read the session in a route handler or loader, see [Server-side rendering](backend-auth-server-side.md).

## Add secrets and other values
<a name="backend-server-code-secrets"></a>

To give your server code an API key or another value for each environment, set it with `aws amplifygen3 var set`, and read it from `process.env`. For a secret, add `--secret`. The value stays out of your shell history, and the CLI masks it in output. Run the following command to set a secret for the `production` environment:

```bash
aws amplifygen3 var set THIRD_PARTY_KEY --secret --env production
```

For more information, see [Environment variables](environment-variables.md).

## Deploy your server code
<a name="backend-server-code-deploy"></a>

Your server code deploys with the rest of your app, each time you run `aws amplifygen3 deploy` or push to a connected repository. A new or changed variable takes effect at the next deployment of its environment.