Page updated Jan 16, 2024

Fetch data

To invoke an endpoint, you need to set input object with required apiName option and optional headers, queryParams, and body options. API status code response > 299 are thrown as an ApiError instance. The error instance provides name and message properties parsed from the response.

GET requests

1import { get } from 'aws-amplify/api';
2
3async function getTodo() {
4 try {
5 const restOperation = get({
6 apiName: 'todo-api',
7 path: '/todo'
8 });
9 const response = await restOperation.response;
10 console.log('GET call succeeded: ', response);
11 } catch (e) {
12 console.log('GET call failed: ', JSON.parse(e.response.body));
13 }
14}

Accessing query parameters & body in Lambda proxy function

To learn more about Lambda Proxy Integration, please visit Amazon API Gateway Developer Guide.

If you are using a REST API which is generated with Amplify CLI, your backend is created with Lambda Proxy Integration, and you can access your query parameters & body within your Lambda function via the event object:

1exports.handler = function(event, context, callback) {
2 console.log(event.queryStringParameters);
3 console.log('body: ', event.body);
4};

Alternatively, you can update your backend file which is located at amplify/backend/function/[your-lambda-function]/app.js with the middleware:

1const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');
2app.use(awsServerlessExpressMiddleware.eventContext());

Accessing Query Parameters with Serverless Express

In your request handler use req.apiGateway.event or req.query:

1app.get('/items', function(req, res) {
2 const query = req.query;
3 // or
4 // const query = req.apiGateway.event.queryStringParameters
5 res.json({
6 event: req.apiGateway.event, // to view all event data
7 query: query
8 });
9});

Then you can use query parameters in your path as follows:

1async function getTodo() {
2 try {
3 const restOperation = get({
4 apiName: 'todo-api',
5 path: '/todo',
6 options: {
7 queryParams: {
8 id: '123'
9 }
10 }
11 });
12 const response = await restOperation.response;
13 console.log('GET call succeeded: ', response);
14 } catch (e) {
15 console.log('GET call failed: ', JSON.parse(e.response.body));
16 }
17}

Accessing response payload

You can consume the response payload by accessing the body property of the response object. Depending on the use case and the content type of the body, you can consume they payload in string, blob or JSON.

1// ...
2const { body } = await restOperation.response;
3// consume as a string:
4const str = await body.text();
5// OR consume as a blob:
6const blob = await body.blob();
7// OR consume as a JSON:
8const json = await body.json();

You can not consume the response payload more than once.

Access HTTP response from errors

The REST API handler may throw an ApiError error instance. If the error is caused by an HTTP response with a non-2xx status code, the error instance will provide a response property. The response property contains following properties:

  • statusCode: HTTP status code
  • headers: HTTP response headers
  • body: HTTP response body as a string

The following example shows how to access the HTTP response from an ApiError instance, so that you can handle the error response from your REST API endpoint:

1import { ApiError, get } from 'aws-amplify/api';
2
3try {
4 const restOperation = get({
5 apiName: 'todo-api',
6 path: '/todo'
7 });
8 await restOperation.response;
9} catch (error) {
10 if (error instanceof ApiError) {
11 if (error.response) {
12 const {
13 statusCode,
14 headers,
15 body
16 } = error.response;
17 console.error(`Received ${statusCode} error response with payload: ${body}`);
18 }
19 // Handle API errors not caused by HTTP response.
20 }
21 // Handle other errors.
22}