All Products
Search
Document Center

Alibaba Cloud SDK:Initialize an SDK client and use the SDK client to initiate API requests

Last Updated:May 27, 2022

This topic describes how to initialize an SDK client and use the SDK client to initiate API requests.

Initialize an SDK client

The Classic SDKs of all Alibaba Cloud services share the core library. You can use the method provided in the core library to initialize an SDK client to process API requests for all Alibaba Cloud services. The following code provides an example on how to initialize an SDK client in the Classic SDK:

static void Main(string[] args)
{
    // Use the core library to save the profile information about a user and initialize an SDK client.
    IClientProfile profile = DefaultProfile.GetProfile(
            "<your-region-id>",
            "<your-access-key-id>",
            "<your-access-key-secret>");
    DefaultAcsClient client = new DefaultAcsClient(profile);
}
Note

If one SDK client processes multiple threads, security issues may occur. If multiple Alibaba Cloud services share one piece of profile information, permission management becomes difficult.

Initiate API requests by using the SDK client

In the Classic SDK, API requests are initiated based on the core library. You can call an API operation based on the request object and response object in the SDK. The following code provides an example on how to call an API operation:

static void Main(string[] args)
{
    // Use the core library to save the profile information about a user and initialize an SDK client.
    IClientProfile profile = DefaultProfile.GetProfile(
            "<your-region-id>",
            "<your-access-key-id>",
            "<your-access-key-secret>");
    DefaultAcsClient client = new DefaultAcsClient(profile);
    // Generate a request object in the SDK. 
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    // Process HTTP requests to serialize the return result into a response object in the SDK based on the core library.
    DescribeInstancesResponse response = client.GetAcsResponse(request);
    System.Console.WriteLine(response.TotalCount);
}
Note

Each API operation has a unique request object. The request object is named in the ${API}${Request} format. For example, a request object can be DescribeInstancesRequest.