Page updated Jan 16, 2024

Set up Amplify REST API

;

The API category provides a solution for making HTTP requests to REST and GraphQL endpoints. For building GraphQL APIs please visit the GraphQL Getting Started section of our documentation.

The REST API category can be used for creating signed requests against Amazon API Gateway when the API Gateway Authorization is set to AWS_IAM.

Ensure you have installed and configured the Amplify CLI and library.

Automated Setup: Create new REST API

Run the following command in your project's root folder:

1amplify add api

Select REST as the service type.

1? Please select from one of the below mentioned services
2 GraphQL
3❯ REST

The CLI will prompt several options to create your resources. With the provided options you can create:

  • REST endpoints that triggers Lambda functions
  • REST endpoints which enables CRUD operations on an Amazon DynamoDB table

During setup you can use existing Lambda functions and DynamoDB tables or create new ones by following the CLI prompts. After your resources have been created update your backend with the push command:

1amplify push

A configuration file called aws-exports.js will be copied to your configured source directory, for example ./src.

Configure your application

Import and load the configuration file in your app. It's recommended you add the Amplify configuration step to your app's root entry point. For example, App.js (Expo) or index.js (React Native CLI).

1import { Amplify, API } from 'aws-amplify';
2import awsconfig from './aws-exports';
3
4Amplify.configure(awsconfig);

Manual Setup: Reference existing REST API

For manual configuration you need to provide your AWS Resource configuration and optionally configure authentication.

1import { Amplify, API } from 'aws-amplify';
2
3Amplify.configure({
4 // OPTIONAL - if your API requires authentication
5 Auth: {
6 identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab', // REQUIRED - Amazon Cognito Identity Pool ID
7 region: 'XX-XXXX-X', // REQUIRED - Amazon Cognito Region
8 userPoolId: 'XX-XXXX-X_abcd1234', // OPTIONAL - Amazon Cognito User Pool ID
9 userPoolWebClientId: 'a1b2c3d4e5f6g7h8i9j0k1l2m3' // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
10 },
11 API: {
12 endpoints: [
13 {
14 name: 'MyAPIGatewayAPI',
15 endpoint: 'https://1234567890-abcdefgh.amazonaws.com'
16 },
17 {
18 name: 'MyCustomCloudFrontApi',
19 endpoint: 'https://api.my-custom-cloudfront-domain.com'
20 }
21 ]
22 }
23});

AWS Regional Endpoints

You can utilize regional endpoints by passing in the service and region information to the configuration. See AWS Regions and Endpoints. The example below defines a Lambda invocation in the us-east-1 region:

1API: {
2 endpoints: [
3 {
4 name: 'MyCustomLambda',
5 endpoint:
6 'https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/yourFuncName/invocations',
7 service: 'lambda',
8 region: 'us-east-1'
9 }
10 ];
11}

The above example IS NOT A RECOMMENDED ARCHITECTURE and we highly recommend you leverage AWS AppSync or API Gateway as the endpoint to invoke your Lambda functions.

Configuring Amazon Cognito Regional Endpoints: To call regional service endpoints, your Amazon Cognito role needs to be configured with appropriate access for the related service. See AWS Cognito Documentation for more details.