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
Amplify Flutter requires a minimum target platform for iOS (13.0), Android (API level 24), and macOS (10.15). Additional setup is required for some target platforms. Please see the platform setup guide for more details on platform specific setup.
Configure API
To start provisioning API resources in the backend, go to your project directory and execute the command:
amplify 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: `api`? Provide a path (e.g., /book/{isbn}): `/todo`? Choose a Lambda source `Create a new Lambda function`? Provide a friendly name for your resource to be used as a label for this category in the project: `todo`? Provide the AWS Lambda function name: `todo`? 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`
To push your changes to the cloud, execute the command:
amplify push
Upon completion, amplifyconfiguration.dart
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
Add the following dependencies to your pubspec.yaml
file and install dependencies when asked. Please keep in mind that Auth plugin is needed for IAM authorization mode, which is default for REST API:
dependencies: flutter: sdk: flutter amplify_flutter: ^2.0.0 amplify_api: ^2.0.0 amplify_auth_cognito: ^2.0.0
Initialize Amplify API
To initialize the Amplify API category you call Amplify.addPlugin()
method. To complete initialization call Amplify.configure()
.
Your code should look like this:
import 'package:amplify_flutter/amplify_flutter.dart';import 'package:amplify_api/amplify_api.dart';import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';import 'package:flutter/material.dart';
import 'amplifyconfiguration.dart';
void main() { runApp(MyApp());}
class MyApp extends StatefulWidget { _MyAppState createState() => _MyAppState();}
class _MyAppState extends State<MyApp> { void initState() { super.initState(); _configureAmplify(); }
Future<void> _configureAmplify() async { // Add the following line to add API plugin to your app. // Auth plugin needed for IAM authorization mode, which is default for REST API. final auth = AmplifyAuthCognito(); final api = AmplifyAPI(); await Amplify.addPlugins([api, auth]);
try { await Amplify.configure(amplifyconfig); } on AmplifyAlreadyConfiguredException { safePrint( 'Tried to reconfigure Amplify; this can occur when your app restarts on Android.'); } }
Future<void> onTestApi() async { // Edit this function with next steps. }
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Padding( padding: const EdgeInsets.all(20.0), child: ElevatedButton( onPressed: onTestApi, child: const Text('Rest API'), ), ), ), ); }}
Make a POST Request
Send a POST request with a JSON body.
Future<void> postTodo() async { try { final restOperation = Amplify.API.post( 'todo', body: HttpPayload.json({'name': 'Mow the lawn'}), ); final response = await restOperation.response; print('POST call succeeded'); print(response.decodeBody()); } on ApiException catch (e) { print('POST call failed: $e'); }}
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: