This topic describes how to call theListInstances API operation to query Simple Application Server instances in a specific region.
Step 1: Review the API documentation
Before you call an API operation, review its documentation to understand the required parameters and permissions. For more information, see API overview.
Step 2: Create a RAM user and grant permissions
An Alibaba Cloud account has full access to all API operations. For security, create and use a RAM user for API calls and routine O&M.
In this step, you create a RAM user with read-only permissions on Simple Application Server resources.
If you have already created a RAM user and granted the required permissions, you can skip this step.
-
Log on to the RAM console by using your Alibaba Cloud account.
-
Create a RAM user.
-
In the left-side navigation pane, choose .
-
On the User page, click Create User.
-
On the Create User page, set the Logon Name and Display Name, and select Console Password Logon for Access Mode.
-
Click OK.
After the RAM user is created, save the logon name and password. You will need these credentials to log on to the Alibaba Cloud OpenAPI Explorer as this RAM user.
-
-
Grant permissions to the RAM user.
NoteBy default, a new RAM user has no permissions to perform operations on Simple Application Server. You must attach a system policy or a custom policy to the RAM user. This topic uses the AliyunSWASFullAccess policy as an example. The AliyunSWASFullAccess policy grants full permissions to manage Simple Application Server resources. If you need to create a custom policy, see RAM authorization.
-
On the User page, find the target RAM user and click Add Permissions in the Actions column.
-
In the Add Permissions panel, search for the keyword
swasand select the AliyunSWASFullAccess policy from the results. -
Click OK to grant the permissions.
Step 3: Call the API operation
The OpenAPI platform provides SDKs for multiple programming languages, such as Java, Go, Python, TypeScript, PHP, and C++. You can integrate an SDK to call API operations directly through its methods. The SDKs handle signing logic, timeouts, and retries, and return structured response objects, which simplifies development.
Get the SDK sample code
The following steps show how to get the sample code for the Java SDK. You can use a similar process for other languages.
-
Log on to the Alibaba Cloud OpenAPI Explorer as the RAM user.
Select Simple Application Server as the cloud product.
In the top navigation bar, choose Tools > API Debugging.
In the left-side navigation pane, find and click the ListInstances operation. Set Service IP Address to China (Hangzhou). This configures the API call to query all Simple Application Server instances in the China (Hangzhou) region.
NoteTo filter instances based on other criteria, go to the Parameter Configuration tab on the left and enter the required parameters.
On the SDK Sample tab, select the SDK version and language. We recommend that you use V2.0. For information about the differences between V1.0 and V2.0, see Differences between V1.0 and V2.0 SDKs.
Click Download Project to download the complete SDK project. After the download is complete, decompress the file.
Run the SDK sample code
The following steps show how to run the Java SDK sample code.
In IntelliJ IDEA, choose File > Open, select the decompressed project folder, and wait for Maven to automatically install the dependencies.
Before you make a call, you must obtain an AccessKey pair. We recommend that you use the AccessKey pair of a RAM user. For more information, see Create an AccessKey pair for a RAM user.
ImportantAfter obtaining the RAM user's AccessKey pair, configure it as an environment variable. For step-by-step instructions, see Configure environment variables in Linux, macOS, and Windows.
Run the sample code.
Double-click the Sample file, verify there are no errors, and then run the code.
Check the results. In the console, press
Ctrl+Fand search forstatusCode. If you see"statusCode": 200, the call was successful.
Troubleshooting
code: 404, Specified access key is not found or invalid.
This error indicates that the environment variable for the AccessKey pair has not been loaded. Restart your IDE or operating system and try again.