All Products
Search
Document Center

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

Last Updated:Jun 25, 2024

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 sample code provides an example on how to initialize an SDK client in the Classic SDK:

static void Main(string[] args)
{
    IClientProfile profile = DefaultProfile.GetProfile(
        // The region ID.
        "<your-region-id>",
        // Obtain the AccessKey ID of a Resource Access Management (RAM) user from environment variables.
        Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
        // Obtain the AccessKey secret of the RAM user from environment variables.
        Environment.GetEnvironmentVariable("ALIBABA_CLOUD_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, risks of unauthorized access may occur.

Initiate API requests by using an SDK client

When you use 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 sample code provides an example on how to call an API operation:

static void Main(string[] args)
{
    IClientProfile profile = DefaultProfile.GetProfile(
        // The region ID.
        "<your-region-id>",
        // Obtain the AccessKey ID of the RAM user from environment variables.
        Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
        // Obtain the AccessKey secret of the RAM user from environment variables.
        Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    DefaultAcsClient client = new DefaultAcsClient(profile);
    // Generate a request object in the SDK. 
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    // Process the HTTP request to serialize the returned results 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 operation name}${Request} format. For example, a request object can be DescribeInstancesRequest.