All Products
Search
Document Center

Alibaba Cloud SDK:Use the Alibaba Cloud SDK for .NET in an IDE

Last Updated:Jun 08, 2026

Set up and run your first Alibaba Cloud SDK for .NET project in VS Code on Windows.

Prerequisites

Use the SDK

Use a sample project from OpenAPI Explorer

Note

If the sample project is unavailable, Install the SDK in an existing project.

  1. In OpenAPI Explorer, search for the API operation to call, such as ECS DescribeRegions, and click the API name to open the debugging page.

    1716346246233_4A41CC11-FA46-4973-BC7B-C4AAD6E7F3F0

  2. Configure the API parameters on the parameters tab. Review the documentation tab for operation details and billing information.

    1716346635851_0B018C7F-D759-497b-B529-58E23E4AC41B

  3. On the SDK Sample Code tab, download and extract the SDK project.

    image

  4. In VS Code, open the extracted project folder.

  5. Obtain a RAM user AccessKey pair. Create an AccessKey for a RAM user.

    Important

    Configure the AccessKey pair as environment variables. Configure environment variables on Linux, macOS, and Windows systems.

  6. Run the project. A response without exceptions indicates success.

    cd core
    dotnet run 

Install the SDK in an existing project

  1. In VS Code, open a .NET project folder. Run dotnet new console -n V2SDKProject to create a console project, then run cd V2SDKProject to enter it.

  2. Install the SDK. In SDK Center, select a cloud product (ECS in this example), set SDK Version to V2.0 and the language to C#, then copy and run the installation command.

    dotnet add package AlibabaCloud.SDK.Ecs20140526 --version 4.1.9

  3. Initialize the ECS client.

    Important
    1. You must use an AccessKey pair to complete identity verification when you initialize the client. In this case, you must obtain an AccessKey pair in advance. For more information about how to obtain an AccessKey pair, see Create an AccessKey.

    2. After you obtain the AccessKey pair of a RAM user, you must configure the AccessKey pair in environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.

    3. For more information about how to configure the endpoint, see Endpoints.

            public static AlibabaCloud.SDK.Ecs20140526.Client CreateClient()
            {
                AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
                {
                    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
                    AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
                    AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
                };
                // For more information about endpoints, see https://api.aliyun.com/product/Ecs
                config.Endpoint = "ecs.cn-shanghai.aliyuncs.com";
                return new AlibabaCloud.SDK.Ecs20140526.Client(config);
            }
  4. Call the API. Review the API Documentation for the target API. The following example calls the `DescribeRegions` API of ECS.

    Note

    Each API has a separate request object that follows the `${APIName}${Request}` naming convention, such as `DescribeRegionsRequest`.

                AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest describeRegionsRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest
                {
                    AcceptLanguage = "en-US",
                };
  5. Add error handling. Exception handling.

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading.Tasks;
    
    using Tea;
    using Tea.Utils;
    
    
    namespace AlibabaCloud.SDK.Sample
    {
        public class Sample 
        {
    
            /**
             * Use an AccessKey ID and AccessKey secret to initialize the client.
             * @return Client
             * @throws Exception
             */
            public static AlibabaCloud.SDK.Ecs20140526.Client CreateClient()
            {
                AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
                {
                    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set.
                    AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set.
                    AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
                };
                config.Endpoint = "ecs.cn-shanghai.aliyuncs.com";
                return new AlibabaCloud.SDK.Ecs20140526.Client(config);
            }
    
            public static void Main(string[] args)
            {
                AlibabaCloud.SDK.Ecs20140526.Client client = CreateClient();
                AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest describeRegionsRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest
                {
                    AcceptLanguage = "en-US",
                };
                AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
                try
                {
                    AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsResponse resp = client.DescribeRegionsWithOptions(describeRegionsRequest, runtime);
                    Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(resp));
                }
                catch (TeaException error)
                {
                    // This is for demonstration only. Handle exceptions with caution and do not ignore them in your project.
                    // Error message
                    Console.WriteLine(error.Message);
                    // Diagnosis URL
                    Console.WriteLine(error.Data["Recommend"]);
                    AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                }
                catch (Exception _error)
                {
                    TeaException error = new TeaException(new Dictionary<string, object>
                    {
                        { "message", _error.Message }
                    });
                    // This is for demonstration only. Handle exceptions with caution and do not ignore them in your project.
                    // Error message
                    Console.WriteLine(error.Message);
                    // Diagnosis URL
                    Console.WriteLine(error.Data["Recommend"]);
                    AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                }
            }
    
        }
    }
  6. Run the sample code.

    dotnet run

References

Advanced topics