All Products
Search
Document Center

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

Last Updated:May 31, 2022

This topic describes how to initialize an SDK client and use the SDK client to initiate API requests in the Darabonba SDK.

Initialize an SDK client

The Darabonba SDK uses the main API-based logic to process parameters, send requests, and process return values. The Darabonba SDK can be used to create a client for each Alibaba Cloud service. Each client provides request methods for all API operations of an Alibaba Cloud service. This way, the identity and authentication information about different Alibaba Cloud services is isolated to improve security. The following code provides an example on how to initialize an SDK client in the Darabonba SDK of Elastic Compute Service (ECS) in TypeScript:

import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
import * as $OpenApi from '@alicloud/openapi-client';

export default class Client {
  static async main(): Promise<void> {
    let config = new $OpenApi.Config({
      // Your AccessKey ID.
      accessKeyId: 'AccessKeyId',
      // Your AccessKey secret.
      accessKeySecret: 'AccessKeySecret',
      // The region that you want to access.
      regionId: 'RegionId'
    });
    const client = new Ecs20140526(config);
  }
}

The following code provides an example on how to initialize an SDK client in the Darabonba SDK of ECS in JavaScript:

const { default: Ecs20140526 } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');

async function main() {
  let config = new Config({
    // Your AccessKey ID.
    accessKeyId: 'AccessKeyId',
    // Your AccessKey secret.
    accessKeySecret: 'AccessKeySecret',
    // The region that you want to access.
    regionId: 'RegionId'
  });
  const client = new Ecs20140526(config);
}

Initiate API requests by using the SDK client

In the Darabonba SDK, the client of each Alibaba Cloud service provides three request methods for all API operations of the service. You can specify a request method for an API operation. The name of request method is the name of the API operation with the first letter in lowercase. The request methods are:

  • The request method that does not require runtime parameters. The following code provides an example on how to use this request method in TypeScript:

import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
import * as $OpenApi from '@alicloud/openapi-client';

export default class Client {
  static async main(): Promise<void> {
    const config = new $OpenApi.Config({
      // Your AccessKey ID.
      accessKeyId: 'AccessKeyId',
      // Your AccessKey secret.
      accessKeySecret: 'AccessKeySecret',
      // The region that you want to access.
      regionId: 'RegionId'
    });
    const client = new Ecs20140526(config);
    const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
      resourceOwnerAccount: "test",
      resourceOwnerId: 1,
      ownerAccount: "test",
      regionId: "test",
    });
    const resp = await client.modifySecurityGroupRule(request);
    console.log(resp.headers);
    console.log(resp.body);
  }
}

The following code provides an example on how to use this request method in JavaScript:

const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');

async function main() {
     const config = new Config({
       // Your AccessKey ID.
       accessKeyId: 'AccessKeyId',
       // Your AccessKey secret.
       accessKeySecret: 'AccessKeySecret',
       // The region that you want to access.
       regionId: 'RegionId'
    });
    const client = new Ecs20140526(config);
    const request = new ModifySecurityGroupRuleRequest({
      resourceOwnerAccount: "test",
      resourceOwnerId: 1,
      ownerAccount: "test",
      regionId: "test",
    });
    const resp = await client.modifySecurityGroupRule(request);
    console.log(resp.headers);
    console.log(resp.body);
}
  • The request method that requires runtime parameters. The following code provides an example on how to use this request method in TypeScript:

import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
import * as $OpenApi from '@alicloud/openapi-client';
import * as $Util from '@alicloud/tea-util';

export default class Client {
  static async main(): Promise<void> {
    const config = new $OpenApi.Config({
      // Your AccessKey ID.
      accessKeyId: 'AccessKeyId',
      // Your AccessKey secret.
      accessKeySecret: 'AccessKeySecret',
      // The region that you want to access.
      regionId: 'RegionId'
    });
    const client = new Ecs20140526(config);
    const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
      resourceOwnerAccount: "test",
      resourceOwnerId: 1,
      ownerAccount: "test",
      regionId: "test",
    });
    // Create a RuntimeOptions instance and configure runtime parameters. 
    const runtime = new $Util.RuntimeOptions({});
    const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
    console.log(resp.headers);
    console.log(resp.body);
  }
}

The following code provides an example on how to use this request method in JavaScript:

const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');
const { RuntimeOptions } = require('@alicloud/tea-util');

async function main() {
  const config = new Config({
    // Your AccessKey ID.
    accessKeyId: 'AccessKeyId',
    // Your AccessKey secret.
    accessKeySecret: 'AccessKeySecret',
    // The region that you want to access.
    regionId: 'RegionId'
  });
  const client = new Ecs20140526(config);
  const request = new ModifySecurityGroupRuleRequest({
    resourceOwnerAccount: "test",
    resourceOwnerId: 1,
    ownerAccount: "test",
    regionId: "test",
  });
  // Create a RuntimeOptions instance and configure runtime parameters. 
  const runtime = new RuntimeOptions({});
  const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
  console.log(resp.headers);
  console.log(resp.body);
}
  • The request method that is used when you call an API operation to upload images. By default, this request method requires runtime parameters. This method is available only for the API operations that you can call to upload files. The following code provides an example on how to use this request method in TypeScript:

import Facebody20191230, * as $Facebody20191230 from '@alicloud/facebody20191230';
import * as $OpenApi from '@alicloud/openapi-client';
import * as $Util from '@alicloud/tea-util';
import { createReadStream } from 'fs';

export default class Client {
  static async main(): Promise<void> {
    const config = new $OpenApi.Config({
      // Your AccessKey ID.
      accessKeyId: 'AccessKeyId',
      // Your AccessKey secret.
      accessKeySecret: 'AccessKeySecret',
      // The region that you want to access.
      regionId: 'RegionId'
    });
    const client = new Facebody20191230(config);
    const request = new $Facebody20191230.DetectBodyCountAdvanceRequest({
      imageURLObject: createReadStream("The directory of the file that you want to upload in your computer"),
    });
    // Create a RuntimeOptions instance and configure runtime parameters. 
    const runtime = new $Util.RuntimeOptions({});
    const resp = await client.DetectBodyCountAdvance(request, runtime);
    console.log(resp.headers);
    console.log(resp.body);
  }
}

The following code provides an example on how to use this request method in JavaScript:

const { default: Facebody20191230, DetectBodyCountAdvanceRequest } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');
const { RuntimeOptions } = require('@alicloud/tea-util');
const { createReadStream } = require('fs');

async function main() {
  const config = new Config({
    // Your AccessKey ID.
    accessKeyId: 'AccessKeyId',
    // Your AccessKey secret.
    accessKeySecret: 'AccessKeySecret',
    // The region that you want to access.
    regionId: 'RegionId'
  });
  const client = new Facebody20191230(config);
  const request = new DetectBodyCountAdvanceRequest({
    imageURLObject: createReadStream("The directory of the file that you want to upload in your computer"),
  });
  // Create a RuntimeOptions instance and configure runtime parameters. 
  const runtime = new RuntimeOptions({});
  const resp = await client.DetectBodyCountAdvance(request, runtime);
  console.log(resp.headers);
  console.log(resp.body);
}
Note

Each API operation has a unique request object and response object. The request object is named in the ${API}${Request} format. For example, a request object can be DescribeRegionRequest.