Connect your app code to API
In this guide, you will connect your application code to the backend API using the Amplify Libraries. Before you begin, you will need:
- Your cloud sandbox with an Amplify Data resource up and running (
npx ampx sandbox
) - A frontend application set up with the Amplify library installed
- npm installed
Configure the Amplify Library
When you deploy you're iterating on your backend (npx ampx sandbox
), an amplify_outputs.json file is generated for you. This file contains your API's endpoint information and auth configurations. Add the following code to your app's entrypoint to initialize and configure the Amplify client library:
import { Amplify } from 'aws-amplify';import outputs from '../amplify_outputs.json';
Amplify.configure(outputs);
Generate the Amplify Data client
Once the Amplify library is configured, you can generate a "Data client" for your frontend code to make fully-typed API requests to your backend.
To generate a new Data client, use the following code:
import { generateClient } from 'aws-amplify/data';import type { Schema } from '../amplify/data/resource'; // Path to your backend resource definition
const client = generateClient<Schema>();
// Now you should be able to make CRUDL operations with the// Data clientconst fetchTodos = async () => { const { data: todos, errors } = await client.models.Todo.list();};
import { generateClient } from 'aws-amplify/data';
/** * @type {import('aws-amplify/data').Client<import('../amplify/data/resource').Schema>} */const client = generateClient();
// Now you should be able to make CRUDL operations with the// Data clientconst fetchTodos = async () => { const { data: todos, errors } = await client.models.Todo.list();};
Configure authorization mode
The Authorization Mode determines how a request should be authorized with the backend. By default, Amplify Data uses the "userPool" authorization which uses the signed-in user credentials to sign an API request. If you use a allow.publicApiKey()
authorization rules for your data models, you need to use "apiKey" as an authorization mode. Review Customize your auth rules to learn more about which authorization modes to choose for which type of request. A Default Authorization Mode is provided as part of the amplify_outputs.json that is generated upon a successful deployment.
You can generate different Data clients with different authorization modes or pass in the authorization mode at the request time.
Set authorization mode on a per-client basis
To apply the same authorization mode on all requests from a Data client, specify the authMode
parameter on the generateClient
function.
Use "API Key" as your authorization mode when if defined the allow.publicApiKey()
authorization rule.
import { generateClient } from 'aws-amplify/data';import type { Schema } from '../amplify/data/resource'; // Path to your backend resource definition
const client = generateClient<Schema>({ authMode: 'apiKey',});
Use "userPool" as your authorization mode when using Amazon Cognito user pool-based authorization rules, such as allow.authenticated()
, allow.owner()
, allow.ownerDefinedIn()
, allow.groupsDefinedIn()
, or allow.groups()
.
import { generateClient } from 'aws-amplify/data';import type { Schema } from '../amplify/data/resource'; // Path to your backend resource definition
const client = generateClient<Schema>({ authMode: 'userPool',});
Use "identityPool" as your authorization mode when using Amazon Cognito identity pool-based authorization rules, such as allow.guest()
or allow.authenticated('identityPool')
.
import { generateClient } from 'aws-amplify/data';import type { Schema } from '../amplify/data/resource'; // Path to your backend resource definition
const client = generateClient<Schema>({ authMode: 'identityPool',});
Use "oidc" as your authorization mode when connecting applications to a trusted identity provider. Private, owner, and group authorization can be configured with an OIDC authorization mode. Review the OIDC authorization docs to learn more.
import { generateClient } from 'aws-amplify/data';import type { Schema } from '../amplify/data/resource'; // Path to your backend resource definition
const client = generateClient<Schema>({ authMode: 'oidc',});
Set authorization mode on the request-level
You can also specify the authorization mode on each individual API request. This is useful if your application typically only uses one authorization mode with a small number of exceptions.
const { data: todos, errors } = await client.models.Todo.list({ authMode: 'apiKey',});
const { data: todos, errors } = await client.models.Todo.list({ authMode: 'userPool',});
const { data: todos, errors } = await client.models.Todo.list({ authMode: 'identityPool',});
const { data: todos, errors } = await client.models.Todo.list({ authMode: 'oidc',});
Set custom request headers
When working with the Amplify Data endpoint, you may need to set request headers for authorization purposes or to pass additional metadata from your frontend to the backend API.
This is done by specifying a headers
parameter into the configuration. You can define headers either on a per Data client-level or on a per-request level:
import type { Schema } from '../amplify/data/resource';import { generateClient } from 'aws-amplify/data';
const client = generateClient<Schema>({ headers: { 'My-Custom-Header': 'my value', },});
// same way for all CRUDL: .create, .get, .update, .delete, .list, .observeQueryconst { data: blog, errors } = await client.models.Blog.get( { id: 'myBlogId' }, { headers: { 'My-Custom-Header': 'my value', }, });
The examples above show you how to set static headers but you can also programmatically set headers by specifying an async function for headers
:
import type { Schema } from '../amplify/data/resource';import { generateClient } from 'aws-amplify/data';
const client = generateClient<Schema>({ headers: async (requestOptions) => { console.log(requestOptions); /* The request options allow you to customize your headers based on the request options such as http method, headers, request URI, and query string. These options are typically used to create a request signature. { method: '...', headers: { }, uri: '/', queryString: "" } */ return { 'My-Custom-Header': 'my value', }; },});
// same way for all CRUDL: .create, .get, .update, .delete, .list, .observeQueryconst res = await client.models.Blog.get( { id: 'myBlogId' }, { headers: async (requestOptions) => { console.log(requestOptions); /* The request options allow you to customize your headers based on the request options such as http method, headers, request URI, and query string. These options are typically used to create a request signature. { method: '...', headers: { }, uri: '/', queryString: "" } */ return { 'My-Custom-Header': 'my value', }; }, });