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

  • An Android application targeting Android API level 24 (Android 7.0) or above

Configure API

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

1amplify add api

Enter the following when prompted:

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 `api`
5? Provide a path (e.g., /book/{isbn}):
6 `/todo`
7? Choose a Lambda source
8 `Create a new Lambda function`
9? Provide an AWS Lambda function name:
10 `todo`
11? Choose the 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 configure advanced settings?
16 `No`
17? Do you want to edit the local lambda function now?
18 `No`
19? Restrict API access
20 `Yes`
21? Who should have access?
22 `Authenticated and Guest users`
23? What kind of access do you want for Authenticated users?
24 `create, read, update, delete`
25? What kind of access do you want for Guest users?
26 `create, read, update, delete`
27Successfully added auth resource locally.
28? Do you want to add another path?
29 `No`

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

1amplify push

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

The current version of the Amplify CLI will create classes intended for use with the AWS SDK for Android in your project. If you see compilation errors relating to com.amazonaws.mobileconnectors.apigateway.annotation, please delete the directory with the name of your API in Android Studio.

Install Amplify Libraries

Expand Gradle Scripts, open build.gradle (Module :app). You will already have configured Amplify by following the steps in the Project Setup walkthrough.

Add API by adding these libraries into the dependencies block:

1dependencies {
2 implementation 'com.amplifyframework:aws-api:ANDROID_VERSION'
3 implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_VERSION'
4
5}

Click Sync Now.

Initialize Amplify API

To initialize the Amplify Auth and API categories you call Amplify.addPlugin() method for each category. To complete initialization call Amplify.configure().

Add the following code to your onCreate() method in your application class:

1Amplify.addPlugin(new AWSApiPlugin());
2Amplify.addPlugin(new AWSCognitoAuthPlugin());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 // Add these lines to add the `AWSApiPlugin` and `AWSCognitoAuthPlugin`
8 Amplify.addPlugin(new AWSApiPlugin());
9 Amplify.addPlugin(new AWSCognitoAuthPlugin());
10 Amplify.configure(getApplicationContext());
11
12 Log.i("MyAmplifyApp", "Initialized Amplify.");
13 } catch (AmplifyException error) {
14 Log.e("MyAmplifyApp", "Could not initialize Amplify.", error);
15 }
16 }
17}
1Amplify.addPlugin(AWSApiPlugin())
2Amplify.addPlugin(AWSCognitoAuthPlugin())

Your class will look like this:

1class MyAmplifyApp : Application() {
2 override fun onCreate() {
3 super.onCreate()
4
5 try {
6 // Add these lines to add the `AWSApiPlugin` and `AWSCognitoAuthPlugin`
7 Amplify.addPlugin(AWSApiPlugin())
8 Amplify.addPlugin(AWSCognitoAuthPlugin())
9 Amplify.configure(applicationContext)
10
11 Log.i("MyAmplifyApp", "Initialized Amplify.")
12 } catch (error: AmplifyException) {
13 Log.e("MyAmplifyApp", "Could not initialize Amplify.", error)
14 }
15 }
16}
1RxAmplify.addPlugin(new AWSApiPlugin());
2RxAmplify.addPlugin(new AWSCognitoAuthPlugin());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 // Add these lines to add the `AWSApiPlugin` and `AWSCognitoAuthPlugin`
8 RxAmplify.addPlugin(new AWSApiPlugin());
9 RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
10 RxAmplify.configure(getApplicationContext());
11
12 Log.i("MyAmplifyApp", "Initialized Amplify.");
13 } catch (AmplifyException error) {
14 Log.e("MyAmplifyApp", "Could not initialize Amplify.", error);
15 }
16 }
17}

Make a POST Request

Send a POST request with a JSON body.

1RestOptions options = RestOptions.builder()
2 .addPath("/todo")
3 .addBody("{\"name\":\"Mow the lawn\"}".getBytes())
4 .build();
5
6Amplify.API.post(options,
7 response -> Log.i("MyAmplifyApp", "POST succeeded: " + response),
8 error -> Log.e("MyAmplifyApp", "POST failed.", error)
9);
1val options = RestOptions.builder()
2 .addPath("/todo")
3 .addBody("{\"name\":\"Mow the lawn\"}".toByteArray())
4 .build()
5
6Amplify.API.post(options,
7 { Log.i("MyAmplifyApp", "POST succeeded: $it") },
8 { Log.e("MyAmplifyApp", "POST failed", it) }
9)
1val request = RestOptions.builder()
2 .addPath("/todo")
3 .addBody("{\"name\":\"Mow the lawn\"}".toByteArray())
4 .build()
5try {
6 val response = Amplify.API.post(request)
7 Log.i("MyAmplifyApp", "POST succeeded: $response")
8} catch (error: ApiException) {
9 Log.e("MyAmplifyApp", "POST failed", error)
10}
1RestOptions options = RestOptions.builder()
2 .addPath("/todo")
3 .addBody("{\"name\":\"Mow the lawn\"}".getBytes())
4 .build();
5
6RxAmplify.API.post(options)
7 .subscribe(
8 response -> Log.i("MyAmplifyApp", "POST succeeded: " + response),
9 error -> Log.e("MyAmplifyApp", "POST failed.", error)
10 );

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: