Page updated Jan 16, 2024

Set up Amplify REST API

The Amplify API category provides an interface for making requests to your backend. The Amplify CLI deploys REST APIs and handlers using Amazon API Gateway and AWS Lambda.

Goal

To setup and configure your application with Amplify API to make requests to your API Gateway and trigger the lambda function using authorization provided by Amplify Auth.

Prerequisites

Make sure you have completed the below steps:

Configure API

To start provisioning API resources in the backend, go to your project directory and execute the command:

amplify add api
1amplify add api

Enter the following when prompted:

? Please select from one of the below mentioned services: `REST` ? Provide a friendly name for your resource to be used as a label for this category in the project: `todoApi` ? Provide a path (e.g., /book/{isbn}): `/todo` ? Choose a Lambda source `Create a new Lambda function` ? Provide the AWS Lambda function name: `todoFunction` ? Choose the function runtime that you want to use: `NodeJS` ? Choose the function template that you want to use: `Serverless ExpressJS function (Integration with API Gateway)` ? Do you want to access other resources created in this project from your Lambda function? `No` ? Do you want to invoke this function on a recurring schedule? `No` ? Do you want to edit the local lambda function now? `No` ? Restrict API access `No` ? Do you want to add another path? `No`
1? Please select from one of the below mentioned services:
2 `REST`
3? Provide a friendly name for your resource to be used as a label for this category in the project:
4 `todoApi`
5? Provide a path (e.g., /book/{isbn}):
6 `/todo`
7? Choose a Lambda source
8 `Create a new Lambda function`
9? Provide the AWS Lambda function name:
10 `todoFunction`
11? Choose the function runtime that you want to use:
12 `NodeJS`
13? Choose the function template that you want to use:
14 `Serverless ExpressJS function (Integration with API Gateway)`
15? Do you want to access other resources created in this project from your Lambda function?
16 `No`
17? Do you want to invoke this function on a recurring schedule?
18 `No`
19? Do you want to edit the local lambda function now?
20 `No`
21? Restrict API access
22 `No`
23? Do you want to add another path?
24 `No`

To push your changes to the cloud, execute the command:

amplify push
1amplify push

Upon completion, amplifyconfiguration.json should be updated to reference provisioned backend storage resources. Note that this file should already be a part of your project if you followed the Project setup walkthrough.

Install Amplify Libraries

Use the package manager of your choice to install the amplify JS library. For example, with npm:

npm install aws-amplify
1npm install aws-amplify

If you are using yarn:

yarn add aws-amplify
1yarn add aws-amplify

Initialize Amplify API

To initialize the Amplify API category you need to configure Amplify with Amplify.configure().

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 index.js in React or main.ts in Angular.

import { Amplify } from 'aws-amplify'; import amplifyconfig from './amplifyconfiguration.json'; Amplify.configure(amplifyconfig);
1import { Amplify } from 'aws-amplify';
2import amplifyconfig from './amplifyconfiguration.json';
3
4Amplify.configure(amplifyconfig);

Make a POST Request

Send a POST request with a JSON body.

import { post } from 'aws-amplify/api'; async function postTodo() { try { const restOperation = post({ apiName: 'todoApi', path: '/todo', options: { body: { message: 'Mow the lawn' } } }); const { body } = await restOperation.response; const response = await body.json(); console.log('POST call succeeded'); console.log(response); } catch (e) { console.log('POST call failed: ', e); } }
1import { post } from 'aws-amplify/api';
2
3async function postTodo() {
4 try {
5 const restOperation = post({
6 apiName: 'todoApi',
7 path: '/todo',
8 options: {
9 body: {
10 message: 'Mow the lawn'
11 }
12 }
13 });
14
15 const { body } = await restOperation.response;
16 const response = await body.json();
17
18 console.log('POST call succeeded');
19 console.log(response);
20 } catch (e) {
21 console.log('POST call failed: ', e);
22 }
23}

To navigate to your backend, go to the API Gateway console and select the API. The name of the API corresponds to the friendly name of the resource to be used as a label you specified earlier in the CLI steps.

Next steps

Congratulations! You've made a call to your API Gateway and triggered your Lambda function. Check out the following links to see other Amplify API use cases: