To facilitate API calls, we recommend that you integrate with Alibaba Cloud SDK in your project. SDKs simplify the development process, quickly integrate features, and greatly reduce the O&M cost. This topic describes how to integrate Alibaba Cloud SDK V1.0 for Go in your project and use the SDK for development.
Support for Alibaba Cloud SDK V1.0 for Go has ended. For more information, see End of support for Alibaba Cloud SDK V1.0 for Golang on March 1, 2025. We recommend that you use Alibaba Cloud SDK V2.0 for Go.
Prerequisites
Go 1.10.x is installed.
Install the SDK
When you install Alibaba Cloud SDK V1.0 for Go, the core library is automatically installed. Therefore, you only need to install the SDK.
Cloud service SDK
The SDK of a cloud service includes the request object that is required for calling API operations and the response object. The following example shows how t install the SDK of Elastic Compute Service (ECS). Run the following installation command on your terminal or command-line interface (CLI):
go get github.com/aliyun/alibaba-cloud-sdk-go/services/ecsThe SDK V1.0 for Go of an Alibaba Cloud service is named in the github.com/aliyun/alibaba-cloud-sdk-go/services/${Service code} format.
Core library of an SDK
The core library of an SDK includes the client object, the signature logic, and the error handling logic, which are required information for calling API operations. To make generic calls, run the following command:
go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdkUse the SDK
The following example shows how to use Alibaba Cloud SDK V1.0 to call the DescribeInstances API operation of ECS.
1. Initialize a request client
All API operations are called by using the client object provided by the SDK library. Before you call an API operation, you must initialize a request client. In this example, the request client is initialized by using an AccessKey pair. For more information, see Manage access credentials.
In this example, the AccessKey pair is obtained from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, which must be configured before you run the code. For more information, see Configure environment variables in Linux, macOS, and Windows.
import (
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)
func main() {
config := sdk.NewConfig()
credential, err := credentials.NewStaticAKCredentialsProviderBuilder().
WithAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
WithAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
Build()
if err != nil {
panic(err)
}
// Initialize the cloud service client
ecsClient, err := ecs.NewClientWithOptions("cn-hangzhou", config, credential)
if err != nil {
panic(err)
}
}2. Create a request object
You can use the request object provided by the SDK to encapsulate the request parameters.
Name the request object of the API operation in the following format: <API operation name>Request.
// In the sample code, the Requests library is used. Therefore, the following information must be added to import the library: "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests".
// Create a request object.
request := ecs.CreateDescribeInstancesRequest()
request.InstanceIds = "[\"i-bp1dXXXXXXXXXXXX\"]"
request.PageSize = requests.Integer("100")
request.PageNumber = requests.Integer("1")
3. Initiate an API request
Use the request client built in Step 1 to call the API operation. The request parameters of the operation are encapsulated in the request object created in Step 2.
response, err := ecsClient.DescribeInstances(request)
if err != nil {
panic(err)
}
fmt.Print(response.GetHttpContentString())
References
For more information about advanced SDK settings, such as proxy settings and timeout settings, see Advanced settings.
For more information about how to create an AccessKey pair, see Create an AccessKey pair.