All Products
Search
Document Center

Intelligent Media Management:IMM SDK for TypeScript

Last Updated:Mar 28, 2025

Alibaba Cloud OpenAPI Explorer provides API documentation, API debugging, and SDK examples to help you get started with API development. This topic describes how to install and use Intelligent Media Management (IMM) SDK for TypeScript.

Prerequisites

  • An AccessKey pair is created and obtained. For more information, see Create an AccessKey pair.

  • OSS is activated, a bucket is created, and objects are uploaded to the bucket. For more information, see Upload objects.

  • IMM is activated. For more information, see Activate IMM.

  • A project is created in the IMM console. For more information about how to create a project by using the IMM console, see Create a project.

    Note
    • You can also create a project by calling the CreateProject operation. For more information, see CreateProject.

    • You can call the ListProjects operation to query existing projects in a specific region. For more information, see ListProjects.

Install the SDK

Important

To use IMM API V2020-09-30, you must use IMM SDK V2020-09-30.

For more information about how to use IMM SDK for TypeScript, see Quick Start.

  1. Create a project directory and enter the directory.

  2. Run the following command to perform initialization:

    npm init
  3. Complete the configurations as prompted. After the initialization is complete, a file named package.json is created. The following details provide an example of file content.

    {
      "name": "resources",
      "version": "1.0.0",
      "description": "",
      "main": "demo.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    }
  4. Run the following commands to install the SDK and the required dependencies:

    npm install aliyun-sdk
    npm install --save @alicloud/imm20200930
    npm install --save @alicloud/openapi-client 
    npm install --save @alicloud/tea-util      
    npm install --save @alicloud/tea-typescript
    npm install -g typescript

Use the SDK

The following sample code provides an example on how to detect faces and face attributes in an image within a project in the China (Beijing) region by using IMM SDK for TypeScript V20.10.0.

  1. Create a demo.ts file and write the following content to the file:

    import imm20200930, * as $imm20200930 from '@alicloud/imm20200930';
    import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
    import Util, * as $Util from '@alicloud/tea-util';
    import * as $tea from '@alicloud/tea-typescript';
    
    
    export default class Client {
    
      /**
       * Use the AccessKey ID and AccessKey secret to initialize the client. 
       * @param accessKeyId
       * @param accessKeySecret
       * @return Client
       * @throws Exception
       */
      static createClient(accessKeyId: string, accessKeySecret: string): imm20200930 {
        let config = new $OpenApi.Config({
          accessKeyId: accessKeyId,
          accessKeySecret: accessKeySecret,
        });
        // Specify the endpoint. 
        config.endpoint = `imm.cn-beijing.aliyuncs.com`;
        return new imm20200930(config);
      }
    
      static async main(): Promise<void> {
        // The AccessKey pair of an Alibaba Cloud account has permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the environment variables. For more information about how to configure environment variables, visit https://www.alibabacloud.com/help/document_detail/2361894.html. 
        const imm_access_key_id = process.env.ALIBABA_CLOUD_ACCESS_KEY_ID;
        const imm_access_key_secret = process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET;
        // Create a client. 
        let client = Client.createClient(imm_access_key_id, imm_access_key_secret);
        // Create an API request and configure the parameters. 
        let detectImageFacesRequest = new $imm20200930.DetectImageFacesRequest({
          // Specify the name of the IMM project. 
            projectName: "immtest",
            sourceURI: "oss://your-bucket-name/your-path/your-image.jpg"
        });
        let runtime = new $Util.RuntimeOptions({ });
        try {
          // Print the response. 
          let response = await client.detectImageFacesWithOptions(detectImageFacesRequest, runtime);
          console.log(JSON.stringify(response.body));
        } catch (error) {
          // Print the error message if necessary. 
          Util.assertAsString(error.message);
          console.log(error);
        }
      }
    
    }
    
    Client.main();
  2. Run the following command to convert the demo.ts file to a JavaScript file named demo.js:

    tsc demo.ts

  3. Run the node demo.js command. The following content provides a sample response.

    {
      "faces": [
        {
          "attractive": 0.9810000061988831,
          "beard": "none",
          "beardConfidence": 0.9990000128746033,
          "boundary": {
            "height": 320,
            "left": 217,
            "top": 169,
            "width": 226
          },
          "emotion": "happiness",
          "emotionConfidence": 1,
          "faceQuality": 0.9869999885559082,
          "figureClusterId": "figure-cluster-id-unavailable",
          "figureConfidence": 1,
          "figureId": "92b7ed67-6344-4410-b5ed-****",
          "figureType": "face",
          "glasses": "none",
          "glassesConfidence": 0.9990000128746033,
          "hat": "none",
          "hatConfidence": 1,
          "headPose": {
            "pitch": -17.742000579833984,
            "roll": 3.2850000858306885,
            "yaw": -0.7279999852180481
          },
          "mask": "none",
          "maskConfidence": 0.7559999823570251,
          "mouth": "open",
          "mouthConfidence": 1,
          "sharpness": 1,
          ...
        }
      ],
      "requestId": "5BE08720-554C-566F-A642-****"
    }