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:
amplify add api
Select REST
as the service type.
? Please select from one of the below mentioned services GraphQL❯ 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:
amplify 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).
import { Amplify, API } from 'aws-amplify';import awsconfig from './aws-exports';
Amplify.configure(awsconfig);
Manual Setup: Reference existing REST API
For manual configuration you need to provide your AWS Resource configuration and optionally configure authentication.
import { Amplify, API } from 'aws-amplify';
Amplify.configure({ // OPTIONAL - if your API requires authentication Auth: { identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab', // REQUIRED - Amazon Cognito Identity Pool ID region: 'XX-XXXX-X', // REQUIRED - Amazon Cognito Region userPoolId: 'XX-XXXX-X_abcd1234', // OPTIONAL - Amazon Cognito User Pool ID userPoolWebClientId: 'a1b2c3d4e5f6g7h8i9j0k1l2m3' // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string) }, API: { endpoints: [ { name: 'MyAPIGatewayAPI', endpoint: 'https://1234567890-abcdefgh.amazonaws.com' }, { name: 'MyCustomCloudFrontApi', endpoint: 'https://api.my-custom-cloudfront-domain.com' } ] }});
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:
API: { endpoints: [ { name: 'MyCustomLambda', endpoint: 'https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/yourFuncName/invocations', service: 'lambda', region: 'us-east-1' } ];}