Install and use the Alibaba Cloud API Toolkit extension in Visual Studio Code (VS Code).
Install Alibaba Cloud API Toolkit
Prerequisites
VS Code is installed.
You can install Alibaba Cloud API Toolkit in two ways.
Method 1: Install from the Extensions view
-
In the VS Code Activity Bar, click the
icon. -
Search for Alibaba Cloud API Toolkit and click Install.
Method 2: Install from VS Code Marketplace
-
Visit the Alibaba Cloud API Toolkit page in VS Code Marketplace and click Install. VS Code opens with the extension page.
-
Click Install on the extension page in VS Code.
Verify the installation
After installation, the Alibaba Cloud icon appears in the Activity Bar.
In the Extensions view in the VS Code sidebar, search for Alibaba Cloud API Toolkit. The main panel shows the extension name, the version (v0.0.9), the publisher (Alibaba Cloud OpenAPI), and a blue Uninstall button. This indicates that the extension is installed.
Configure credentials
Configure an AccessKey pair before you call API operations. AccessKey pairs are the only credential type supported by this extension.
Add or modify credentials
-
Install the Alibaba Cloud CLI Tools extension.
-
Visit the Alibaba Cloud CLI Tools page in VS Code Marketplace and click Install.
-
In the message that appears, click Open.
-
Click Install on the extension page in VS Code.
-
After installation, the Alibaba Cloud icon appears in the Activity Bar.
NoteYou can also directly install Alibaba Cloud CLI Tools in VS Code. For more information, see Use the Alibaba Cloud CLI extension for VS Code.
-
-
Configure the identity and credential.
-
In VS Code, choose .
-
Run
aliyun configureand follow the prompts. Use an existing AccessKey pair orcreate a new one.ImportantThe AccessKey secret is displayed only at creation time and cannot be retrieved later. Store it in a secure location.
-
Press Enter after each prompt. If Saving profile[xxx] ...Done. is returned, the profile is saved. If !!! Configure Failed please configure again !!! is returned, the parameter format or values are invalid and credential verification failed. The profile is still saved.
$ aliyun configure Configuring profile 'default' ... Aliyun Access Key ID [None]: <Your AccessKey ID> Aliyun Access Key Secret [None]: <Your AccessKey Secret> Default Region Id [None]: cn-hangzhou Default output format [json]: json Default Language [zh]: zhImportantAn Alibaba Cloud account has full permissions on all resources. A leaked AccessKey pair exposes your resources to significant risks. Use the AccessKey pair of a RAM user instead.
-
View and switch user identities
-
Click the Alibaba Cloud icon in the lower-left status bar. The configured profiles appear in the search box.
-
Click a profile name to switch identities.
Features
Alibaba Cloud API Toolkit lets you search API operations, debug APIs, and generate SDK sample code directly in VS Code.
Subscribe to services
Subscribe to Alibaba Cloud services and select API versions. The extension recommends versions and requires no configuration file. Press Ctrl+Cmd+K (Ctrl+Win+K in Windows) to search and subscribe.
In the API service drop-down list, use the search box to filter and select the target service. For example, select Certificate Management Service (service code cas, recommended version 2020-04-07).
API search
Search subscribed APIs and view their references. Press Ctrl+Cmd+L (Ctrl+Win+L in Windows) to search APIs or insert code snippets.
After you enter a keyword such as describe in the search box, the matching API operations appear below the box. Each entry shows the request method, the operation name, a brief description, and the version path.
API reference preview
Click an API operation to view its reference in a new tab, including usage notes, request parameters, response parameters, and error codes.
In API Toolkit, the left pane shows the API navigation tree, where you can browse API operations by service and category. After you select an operation, such as RebootInstances — Restarts one or more instances, the main pane on the right shows the reference for that operation, including the description and the request parameters. Use the tabs at the top to switch between Document, Debug, and Code Sample.
API debugging
-
Debug API operations after credential verification.
-
Configure parameters and view debugging results.
-
View API responses directly in VS Code.
For example, select the DescribeRegions operation. In the parameter form, set Endpoint to China (Hangzhou), InstanceChargeType to PrePaid, ResourceType to instance, and AcceptLanguage to zh-CN. After you click Debug, the right pane shows a successful response with HTTP status code 200 in JSON format that contains the list of available regions.
Use sample code
Install the SDK of the Alibaba Cloud service before you use sample code.
Install the SDK of an Alibaba Cloud service
-
Go to SDK Center, select a service, set SDK Version to V2.0 and the All languages parameter to TypeScript. This example uses ECS.
npm install --save @alicloud/ecs20140526@4.1.8 -
Run the install command in the terminal.
Generate SDK sample code
-
View SDK sample code
Generate SDKSDK sample code for different programming languages. View the generated code or save it as a new file in VS Code.
In the API Toolkit extension, expand the target service in the left navigation tree, for example Elastic Compute Service (ECS), and select the API operation to call, for example DescribeRegions. In the middle pane, configure the request parameters, including Endpoint (for example, China (Hangzhou)), InstanceChargeType, ResourceType, and AcceptLanguage (for example,
zh-CN). The Clear and Debug buttons are at the bottom of the pane. On the right, switch to the Code Sample tab and select the target language and version, for example TypeScript SDK V2.0. The extension generates complete SDK sample code that you can copy and use directly. -
Insert code snippets
Insert SDK code snippets for Java, Java (async), TypeScript, Python, Go, PHP, and C#.
-
Method 1: Type a subscribed API operation name in the editor to search for its code snippet.
-
Method 2: Press
Ctrl+Cmd+L(Ctrl+Win+Lin Windows) to search for an API operation and insert a code snippet.
-
Complete and run the code
-
Complete the code
-
Modify the endpoint.
Log on to OpenAPI Explorer, hover over Cloud Products and select a service. View the supported endpoints on the Regions tab.
-
Specify request parameters. For more information, see the API reference preview section of this topic.
-
Optional. Add logging.
-
Save the code.
import OpenApi, * as $OpenApi from '@alicloud/openapi-client'; import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526'; export default class DescribeRegion { static async main(args: string[]): Promise<void> { let config = new $OpenApi.Config({ // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured in the code runtime environment. accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'], accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'], // Specify an endpoint. For more information, see endpoints at https://api.alibabacloud.com/product/Ecs. endpoint: `ecs.cn-hangzhou.aliyuncs.com`, }); let client = new Ecs20140526(config); let describeRegionsRequest = new $Ecs20140526.DescribeRegionsRequest({}); let response = await client.describeRegions(describeRegionsRequest); console.log(response.body?.regions?.region); } }
-
-
Run the code
-
Compile the TypeScript file into JavaScript:
tsc client.ts # client.ts is the file name. -
Create a main.js file with the following code:
const { default: Describeregion } = require("./describeregion"); Describeregion.main(); -
Run the sample code:
node .\main.js -
If the following result is returned, the call is successful.
PS D:\ide\tooldemo> tsc describeregion.ts PS D:\ide\tooldemo> node .\main.js [ DescribeRegionsResponseBodyRegionsRegion { localName: 'China (Qingdao)', regionEndpoint: 'ecs.cn-qingdao.aliyuncs.com', regionId: 'cn-qingdao' }, DescribeRegionsResponseBodyRegionsRegion { localName: 'China (Beijing)', regionEndpoint: 'ecs.cn-beijing.aliyuncs.com', regionId: 'cn-beijing' }, DescribeRegionsResponseBodyRegionsRegion { localName: 'China (Zhangjiakou)', regionEndpoint: 'ecs.cn-zhangjiakou.aliyuncs.com', regionId: 'cn-zhangjiakou' }, DescribeRegionsResponseBodyRegionsRegion { localName: 'China (Hohhot)', regionEndpoint: 'ecs.cn-huhehaote.aliyuncs.com', regionId: 'cn-huhehaote' }, DescribeRegionsResponseBodyRegionsRegion { localName: 'China (Ulanqab)', regionEndpoint: 'ecs.cn-wulanchabu.aliyuncs.com', regionId: 'cn-wulanchabu' }, DescribeRegionsResponseBodyRegionsRegion { localName: 'China (Hangzhou)', ... } ]
-
Feedback
Submit questions or feedback on the GitHub repository.