Use Alibaba Cloud SDK for Python to call an API operation

Updated at:

This guide shows how to call the DescribeInstancesOverview operation of Tair (Redis OSS-compatible) using the Alibaba Cloud SDK for Python—from setting up your environment to running the request and reading the response.

Prerequisites

Before you begin, ensure that you have:

  • Python 3. Run python --version to check. Download from python.org if not installed.

  • A Resource Access Management (RAM) user with API access. If you haven't created one yet, complete the Create a RAM user step below.

  • Your AccessKey ID and AccessKey secret.

Review the API reference

Before calling any Tair API operation, review the list of operations by function to understand the available parameters and usage limits.

Create a RAM user

Skip this section if you already have a RAM user with the required permissions.

  1. Go to the Users page in the RAM console, then click Create User.

  2. Set Logon Name to redis-openapi-operator and set Access Mode to Using permanent AccessKey to access.

  3. Click OK and save the AccessKey ID and AccessKey secret that are displayed.

  4. On the Users page, find the user you just created and click Add Permissions in the Actions column.

  5. In the panel that appears, search for AliyunKvstore and select a policy: For custom permission configurations, see Identity management.

    • AliyunKvstoreReadOnlyAccess: read-only access to Tair instances

    • AliyunKvstoreFullAccess: read and write access to Tair instances

  6. Click Grant permissions.

Set up credentials

Store your AccessKey ID and AccessKey secret as environment variables so they stay out of your code.

Linux or macOS:

export ALIBABA_CLOUD_ACCESS_KEY_ID=<your-access-key-id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<your-access-key-secret>

Windows:

Create a file to define the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, then restart your system for the changes to take effect.

Install dependencies

Run the following commands in sequence to install the required packages:

pip install alibabacloud_credentials          # Alibaba Cloud credentials management
pip install alibabacloud_r_kvstore20150101    # Tair (Redis OSS-compatible) SDK client
pip install alibabacloud_tea_console          # Console output utilities for SDK responses

Call the API

Download sample code

The OpenAPI portal generates ready-to-run Python code for each operation.

  1. Open the DescribeInstancesOverview page in the OpenAPI portal.

  2. On the Parameters tab, set RegionId to the region where your instances are located (for example, cn-hangzhou).

  3. On the SDK Sample Code tab, select Python for Languages, then click Download Project.

  4. Extract the archive and go to the alibabacloud_sample directory.

Run the sample

python sample.py

A successful response returns HTTP 200 along with an Instances array. Each entry includes key fields:

FieldDescription
InstanceIdUnique instance identifier
InstanceStatusCurrent status (for example, Normal)
ArchitectureTypeArchitecture type: standard or rwsplit
InstanceTypeInstance type: Redis or tair_rdb
ConnectionDomainConnection endpoint for the instance
RegionIdRegion where the instance is deployed

Example response (two instances):

{
    "headers": {
        "date": "Tue, 04 Jul 2023 10:03:39 GMT",
        "content-type": "application/json;charset=utf-8",
        "content-length": "1665",
        "connection": "keep-alive",
        "keep-alive": "timeout=25",
        "vary": "Accept-Encoding",
        "access-control-allow-origin": "*",
        "access-control-expose-headers": "*",
        "x-acs-request-id": "F2F4B6CB-3C03-51A0-9300-F78BC94E****",
        "x-acs-trace-id": "4d9ceb1f6cb98086cd8333f0bb30****",
        "etag": "1YlaXvHZ3H5CjZic1Oeo****"
    },
    "statusCode": 200,
    "body": {
        "Instances": [
            {
                "ArchitectureType": "standard",
                "Capacity": 4096,
                "ChargeType": "PostPaid",
                "ConnectionDomain": "r-bp1u7sm6mp6h83****.redis.rds.aliyuncs.com",
                "CreateTime": "2023-02-07T05:49:56Z",
                "EngineVersion": "6.0",
                "InstanceClass": "redis.shard.large.ce",
                "InstanceId": "r-bp1u7sm6mp6h83****",
                "InstanceName": "",
                "InstanceStatus": "Normal",
                "InstanceType": "Redis",
                "NetworkType": "VPC",
                "RegionId": "cn-hangzhou",
                "ResourceGroupId": "rg-acfmz7u4zzr****",
                "VSwitchId": "vsw-bp1ah4djy7ghm3ump****",
                "VpcId": "vpc-bp1p5zk2l6ixtgx6v****",
                "ZoneId": "cn-hangzhou-h"
            },
            {
                "ArchitectureType": "rwsplit",
                "Capacity": 2048,
                "ChargeType": "PostPaid",
                "ConnectionDomain": "r-bp1qsslcssrjeb****.redis.rds.aliyuncs.com",
                "CreateTime": "2023-02-07T05:53:33Z",
                "EngineVersion": "6.0",
                "InstanceClass": "tair.rdb.with.proxy.2g",
                "InstanceId": "r-bp1qsslcssrjeb****",
                "InstanceName": "",
                "InstanceStatus": "Normal",
                "InstanceType": "tair_rdb",
                "NetworkType": "VPC",
                "RegionId": "cn-hangzhou",
                "ResourceGroupId": "rg-acfmz7u4zzr****",
                "VSwitchId": "vsw-bp1ah4djy7ghm3ump****",
                "VpcId": "vpc-bp1p5zk2l6ixtgx6v****",
                "ZoneId": "cn-hangzhou-h"
            }
        ],
        "RequestId": "F2F4B6CB-3C03-51A0-9300-F78BC94EE283",
        "TotalCount": 2
    }
}

If InstanceStatus is Normal for all listed instances, the call succeeded.

What's next