Call APIs from the command line

Updated at:

OpenAPI Explorer provides the built-in Cloud Shell. Built on the Alibaba Cloud CLI, Cloud Shell is a web-based command-line interface (CLI) tool that requires no installation or AccessKey configuration and can be used immediately after you log on.

Alibaba Cloud APIs are either RPC or RESTful. Most products (ECS, ApsaraDB RDS, load balancer) use RPC APIs.

Identify the API type by these characteristics:

  • RPC APIs include an Action parameter, while RESTful APIs require a PathPattern parameter.

  • Typically, all API operations for a single cloud product are of the same type.

Start Cloud Shell

  1. Log on to OpenAPI Explorer.

  2. In the top navigation bar, click Select Cloud Product and select a cloud product from the panel.

  3. Navigate to the product homepage. In the top navigation bar, click API debugging and select the CLI Example tab.

  4. On the CLI Example tab, click the Cloud Shell icon image.png to start Cloud Shell.

    Alternatively, you can access Alibaba Cloud Shell directly.

Call RPC APIs in Cloud Shell

To call an RPC API in Cloud Shell, use the following format:

aliyun <ProductCode> <ActionName> [--parameter1 value1 --parameter2 value2]

Parameter

Description

Example

ProductCode

The code of the cloud product to call. For example, the product code for Elastic Compute Service is ecs, and the product code for Server Load Balancer is slb.

Run the aliyun --help command to view product codes.

Execute the following command in Cloud Shell to view the configuration information of a specific ECS instance.

Note

Before you run the command, replace the instance ID.

aliyun ecs DescribeInstanceAttribute --InstanceId i-bp198exxxxxx | jq 

ActionName

The API to call. For example, use the DescribeInstanceAttribute API of ECS to view the details of an ECS instance.

parameter

The request parameters to pass. For more information, see the API documentation for each cloud product.

Call RESTful APIs in Cloud Shell

Some Alibaba Cloud products, such as Container Service, use RESTful APIs. The method for calling a RESTful API differs from that of an RPC API. The basic structure for calling a RESTful API using the Cloud Assistant CLI is as follows:

  • GET request

    aliyun <ProductCode> GET /<Resource>

    Example

    aliyun cs GET /clusters
  • POST request

    aliyun <ProductCode> POST /<Resource> --body "$(cat input.json)"

    Example

    aliyun cs POST /clusters/<cluster-id> --header "Content-Type=application/json" --body "$(cat attach.json)"
  • DELETE request

    aliyun <ProductCode> DELETE /<Resource>

    Example

    aliyun cs DELETE /clusters/<cluster-id>