All Products
Search
Document Center

Alibaba Cloud SDK:Initialize an SDK client and send API requests

Last Updated:Jul 09, 2024

This topic describes how to initialize an SDK client and send API requests in Alibaba Cloud SDK V1.0 for Python.

Initialize an SDK client

In Alibaba Cloud SDK V1.0 for Python, SDKs for all Alibaba Cloud services share the same client. The client generates a client object by using the method provided in the Core SDK to process all requests.

Note

In this example, a credential is created by reading the values of environment variables. Before you run the sample code, you must configure the following environment variables: ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. For more information, see Configure environment variables in Linux, macOS, and Windows.

pip install aliyun-python-sdk-core
import os

from aliyunsdkcore.client import AcsClient

# Initialize an SDK client.
client = AcsClient(
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),  # Obtain the AccessKey ID of the Resource Access Management (RAM) user from the environment variable.
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),  # Obtain the AccessKey secret of the RAM user from the environment variable.
    '<region_id>'  # The region ID.
)

Initiate API requests by using an SDK client

In Alibaba Cloud SDK V1.0 for Python, all API requests are processed based on the Core SDK. SDKs for Alibaba Cloud services provide the request and response objects for API operations. Developers can send API requests with ease based on the request and response objects. This simplifies service calls and improves development efficiency. The following example shows how to send a request for calling the DescribeRegions operation of Elastic Compute Service (ECS). For more information about the request and response parameters, see the API reference of ECS.

import os

from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest

# Initialize an SDK client.
client = AcsClient(
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),  # Obtain the AccessKey ID of the RAM user from the environment variable.
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),  # Obtain the AccessKey secret of the RAM user from the environment variable.
    '<region_id>'  # The region ID.
)

# Create an API request and configure parameters.
request = DescribeRegionsRequest()

#Send the request.
response = client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))

References

For more information about how to initialize an SDK client, see Manage access credentials.