Initialize an SDK client and send API requests

Updated at:
Copy as MD

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

Initialize an SDK client

In SDK for Java V1.0, 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.

Dependency information

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.6.3</version>
    </dependency>

Sample code

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.profile.DefaultProfile;
public class Sample {

    public static void main(String[] args) {
        // The region ID, which is used for automatic addressing. For more information, see the "Configure an endpoint" topic.
        String regionId = "cn-shanghai";
        // Obtain the AccessKey pair by reading the values of the environment variables.
        EnvironmentVariableCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
        DefaultProfile profile = DefaultProfile.getProfile(regionId);
        // Use the Core SDK to save the profile information about a user and initialize an SDK client.
        DefaultAcsClient client = new DefaultAcsClient(profile, credentialsProvider);
    }
}
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. We recommend that you use SDK for Java V2.0 for development.

Send API requests by using an SDK client

In SDK for Java V1.0, 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 improve development efficiency. The following example shows how to send a request for calling the DescribeRegions operation of Elastic Compute Service (ECS) and the SendSms operation of Short Message Service (SMS). For more information about request and response parameters, see the API reference of each Alibaba Cloud service.

Dependency information

<!--    Core dependency-->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.6.3</version>
    </dependency>
<!--    ECS dependency-->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-ecs</artifactId>
      <version>5.11.7</version>
    </dependency>
<!--    SMS dependency-->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
      <version>2.2.1</version>
    </dependency>

Sample code

package com.aliyun.sample;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeRegionsRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeRegionsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

public class Sample {

    public static void main(String[] args) throws ClientException {
        // The region ID, which is used for automatic addressing. For more information, see the "Configure an endpoint" topic.
        String regionId = "cn-shanghai";
        // Obtain the AccessKey pair by reading the values of the environment variables.
        EnvironmentVariableCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
        DefaultProfile profile = DefaultProfile.getProfile(regionId);
        // Use the Core SDK to save the profile information about a user and initialize an SDK client.
        DefaultAcsClient client = new DefaultAcsClient(profile, credentialsProvider);
        // Create the DescribeRegionsRequest object.
        DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
        // Create the SendSmsRequest object and specify the parameters.
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumbers("<phoneNumbers>");
        // Process the HTTP requests to serialize the returned results into a response object in the SDK based on the Core SDK.
        DescribeRegionsResponse describeRegionsResponse = client.getAcsResponse(describeRegionsRequest);
        SendSmsResponse sendSmsResponse = client.getAcsResponse(sendSmsRequest);
        // If the request is successful, display the response content.
        System.out.println(new Gson().toJson(describeRegionsResponse));
        System.out.println(new Gson().toJson(sendSmsResponse));
    }
}
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 named DescribeRegionRequest.

References

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