Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated May 7, 2024

Set up Amplify REST API

Amplify Android v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Android to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

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 21 (Android 5.0) or above

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 an AWS Lambda function name:
`todo`
? Choose the 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 configure advanced settings?
`No`
? Do you want to edit the local lambda function now?
`No`
? Restrict API access
`Yes`
? Who should have access?
`Authenticated and Guest users`
? What kind of access do you want for Authenticated users?
`create, read, update, delete`
? What kind of access do you want for Guest users?
`create, read, update, delete`
Successfully added auth resource locally.
? Do you want to add another path?
`No`

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

amplify 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:

dependencies {
implementation 'com.amplifyframework:aws-api:ANDROID_V1_VERSION'
implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_V1_VERSION'
}

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:

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

Your class will look like this:

public class MyAmplifyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
// Add these lines to add the `AWSApiPlugin` and `AWSCognitoAuthPlugin`
Amplify.addPlugin(new AWSApiPlugin());
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify.");
} catch (AmplifyException error) {
Log.e("MyAmplifyApp", "Could not initialize Amplify.", error);
}
}
}
Amplify.addPlugin(AWSApiPlugin())
Amplify.addPlugin(AWSCognitoAuthPlugin())

Your class will look like this:

class MyAmplifyApp : Application() {
override fun onCreate() {
super.onCreate()
try {
// Add these lines to add the `AWSApiPlugin` and `AWSCognitoAuthPlugin`
Amplify.addPlugin(AWSApiPlugin())
Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.configure(applicationContext)
Log.i("MyAmplifyApp", "Initialized Amplify.")
} catch (error: AmplifyException) {
Log.e("MyAmplifyApp", "Could not initialize Amplify.", error)
}
}
}
RxAmplify.addPlugin(new AWSApiPlugin());
RxAmplify.addPlugin(new AWSCognitoAuthPlugin());

Your class will look like this:

public class MyAmplifyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
// Add these lines to add the `AWSApiPlugin` and `AWSCognitoAuthPlugin`
RxAmplify.addPlugin(new AWSApiPlugin());
RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
RxAmplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify.");
} catch (AmplifyException error) {
Log.e("MyAmplifyApp", "Could not initialize Amplify.", error);
}
}
}

Make a POST Request

Send a POST request with a JSON body.

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

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: