Node.js

更新时间:
复制 MD 格式

This document explains how to install and use the Node.js SDK for Alibaba Cloud Vision Intelligence Platform services and provides code examples.

Note

For questions about SDK integration, API usage, or the visual AI capabilities of the Alibaba Cloud Vision Intelligence Platform, contact us through our DingTalk group (ID: 23109592).

This SDK requires Node.js 8.x or later. If your environment does not meet this requirement, upgrade Node.js.

Prerequisites

Before using an Alibaba Cloud SDK, you must have an Alibaba Cloud account and an AccessKey. For more information, see Create an AccessKey.

Run the following commands to install the SDK packages for the AI categories you need.

  • Face and Body: npm install @alicloud/facebody20191230

  • Optical Character Recognition: npm install @alicloud/ocr20191230

  • Product Understanding: npm install @alicloud/goodstech20191230

  • Content Moderation: npm install @alicloud/imageaudit20191230

  • Image Recognition: npm install @alicloud/imagerecog20190930

  • Image Generation: npm install @alicloud/imageenhan20190930

  • Image Segmentation: npm install @alicloud/imageseg20191230

  • Object Detection: npm install @alicloud/objectdet20191230

  • Image Analysis and Processing: npm install @alicloud/imageprocess20200320

  • Visual Search: npm install @alicloud/imgsearch20200320

  • Video Generation: npm install @alicloud/videoenhan20200320

  • Video Understanding: npm install @alicloud/videorecog20200320

  • Video Segmentation: npm install @alicloud/videoseg20200320

  • Asynchronous Task Management: npm install @alicloud/viapi20230117

  • Face Liveness server-side 20200910-specific version: npm install @alicloud/facebody20200910

The following table provides the download links for the SDKs.

AI category

SDK link

GitHub link

Face and Body

@alicloud/facebody20191230

facebody-20191230

Optical Character Recognition

@alicloud/ocr20191230

ocr-20191230

Product Understanding

@alicloud/goodstech20191230

goodstech-20191230

Content Moderation

@alicloud/imageaudit20191230

imageaudit-20191230

Image Recognition

@alicloud/imagerecog20190930

imagerecog-20190930

Image Generation

@alicloud/imageenhan20190930

imageenhan-20190930

Image Segmentation

@alicloud/imageseg20191230

imageseg-20191230

Object Detection

@alicloud/objectdet20191230

objectdet-20191230

Visual Search

@alicloud/imgsearch20200320

imgsearch-20200320

Image Analysis and Processing

@alicloud/imageprocess20200320

imageprocess-20200320

Video Generation

@alicloud/videoenhan20200320

videoenhan-20200320

Video Understanding

@alicloud/videorecog20200320

videorecog-20200320

Video Segmentation

@alicloud/videoseg20200320

videoseg-20200320

Asynchronous Task Management

@alicloud/viapi-20230117

viapi-20230117

Face Liveness server-side 20200910-specific version

@alicloud/facebody20200910

facebody-20200910

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

Important
  • An Alibaba Cloud account has full access to all API operations. We recommend that you use a RAM user for API calls or routine O&M. For more information, see Create a RAM user.

  • Do not save your AccessKey ID or AccessKey Secret in project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.

  • Configure environment variables on Linux and macOS

    1. Open a terminal in IntelliJ IDEA.

    2. Run the following commands to configure the environment variables.

      Replace <access_key_id> with the AccessKey ID of your RAM user and <access_key_secret> with the AccessKey Secret of your RAM user. If you need to configure more permissions, see Control access permissions using a RAM policy.

      export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
      export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Configure environment variables on Windows

    Create a new environment variable file, add the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared. Then, restart the Windows operating system. The following example uses Windows 10.

    1. Open File Explorer, right-click This PC, and then select Properties.

    2. In the left-side navigation pane, click Advanced system settings.

    3. On the Advanced tab of the System Properties dialog box, click Environment Variables.

    4. In the Environment Variables dialog box, click New.

    5. In the New System Variable dialog box, add the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared.

    6. Restart the Windows operating system for the configurations to take effect.

SDK examples

The following examples use the RecognizeBankCard API operation.

File in OSS

Note

For the endpoints and supported regions for each AI category, see Endpoints.

// Import the SDK.
// 1. This example uses Optical Character Recognition (OCR). For other capabilities, import the package for the corresponding category. You can find package names in the "Prerequisites" section of this document and the capability name in the Action parameter of the corresponding API documentation. For example, to use the SegmentCommonImage API operation, refer to its API documentation at https://help.aliyun.com/document_detail/151960.html. You can determine that this capability belongs to the Image Segmentation category. Therefore, you must change `ocr20191230` to `imageseg20191230` and `RecognizeBankCard` to `SegmentCommonImage`.
const OcrClient = require('@alicloud/ocr20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');

let config = new OpenapiClient.Config({
  // 2. To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
  // If you use an AccessKey of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
  // The AccessKey ID and AccessKey Secret are read from environment variables. You must configure the environment variables before you run the sample code.
  accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,   
  accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// 3. The service endpoint. Note: You must change this to the endpoint of the corresponding category. For more information, see https://help.aliyun.com/document_detail/143103.html.
config.endpoint = `ocr.cn-shanghai.aliyuncs.com`;

// 4. This example uses OCR. For other capabilities, use the Client class from the corresponding category package.
const client = new OcrClient.default(config);
// 5. This example uses the RecognizeBankCard operation of OCR. For other capabilities, use the package and class of the corresponding category. For information about how to set the request parameters, see the documentation for the specific capability.
const recognizeBankCardRequest = new OcrClient.RecognizeBankCardRequest({
  imageURL: "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeBankCard/yhk1.jpg"
});
let runtime = new TeaUtil.RuntimeOptions({ });
// 6. This example uses the RecognizeBankCard operation of OCR. For other capabilities, use the package and class of the corresponding category. The method name `recognizeBankCardWithOptions` must also be changed to the method name of the corresponding capability. The method name is generated based on the capability name according to specific rules. For example, if the capability name is `SegmentCommonImage`, the method name is `segmentCommonImageWithOptions`.
client.recognizeBankCardWithOptions(recognizeBankCardRequest, runtime)
  .then(function(recognizeBankCardResponse) {
    // Obtain the full response.
    console.log(recognizeBankCardResponse);
    // Obtain a specific field. This is only an example. For information about the fields for a specific capability, see the documentation for that capability.
    console.log(recognizeBankCardResponse.body.data.cardNumber);
  }, function(error) {
    // Obtain the full error message.
    console.log(error);
    // Obtain a specific field from the error message.
    console.log(error.data.Code);
  })
Note

The parts of the code that you need to modify are described in the comments. The following list provides a summary:

  1. When importing packages, import the package and related classes of the corresponding category. You can find package names in the preceding list of SDK packages and find the capability name in the Action parameter of the corresponding API documentation.

    For example, to use the SegmentCommonImage API operation, you can learn from the API documentation for SegmentCommonImage that this capability belongs to the Image Segmentation category (imageseg20191230) and the capability name is SegmentCommonImage. You must change ocr20191230 to imageseg20191230 and RecognizeBankCard to SegmentCommonImage in the code.

  2. Make sure to change the endpoint to the one for the correct category. If the endpoint and category do not match, an InvalidAction.NotFound error is returned. For more information about endpoints, see Endpoints.

  3. The Client class must be from the package of the corresponding category.

  4. The Request and Response objects must be from the package and classes of the corresponding category.

  5. When you call a Client method, change the method name to the one for the corresponding capability. The method name is generated based on the capability name. For example, if the capability name is SegmentCommonImage, the method name is segmentCommonImageWithOptions.

Local file or URL

Important

The main difference from the previous example is the use of an xxxAdvanceRequest object, where the file is passed as a stream to the imageURLObject parameter.

// Import the SDK.
// 1. This example uses Optical Character Recognition (OCR). For other capabilities, import the package for the corresponding category. You can find package names in the "Prerequisites" section of this document and the capability name in the Action parameter of the corresponding API documentation. For example, to use the SegmentCommonImage API operation, refer to its API documentation at https://help.aliyun.com/document_detail/151960.html. You can determine that this capability belongs to the Image Segmentation category. Therefore, you must change `ocr20191230` to `imageseg20191230` and `RecognizeBankCard` to `SegmentCommonImage`.
const OcrClient = require('@alicloud/ocr20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
const fs = require('fs');
const http = require('http');
const https = require('https');

let config = new OpenapiClient.Config({
  // 2. To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
  // If you use an AccessKey of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
  // The AccessKey ID and AccessKey Secret are read from environment variables. You must configure the environment variables before you run the sample code.
  accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,   
  accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// 3. The service endpoint. Note: You must change this to the endpoint of the corresponding category. For more information, see https://help.aliyun.com/document_detail/143103.html.
config.endpoint = `ocr.cn-shanghai.aliyuncs.com`;
// 4. This example uses OCR. For other capabilities, use the Client class from the corresponding category package.
const client = new OcrClient.default(config);
const getResponse = function (httpClient, url) {
  return new Promise((resolve, reject) => {
    httpClient.get(url, function (response) {
      resolve(response);
    })
  })
}
const request = async function () {
  try {
    // 5. This example uses the RecognizeBankCard operation of OCR. For other capabilities, use the package and class of the corresponding category. For information about how to set the request parameters, see the documentation for the specific capability.
    let recognizeBankCardAdvanceRequest = new OcrClient.RecognizeBankCardAdvanceRequest();
    // Scenario 1: Use a local file.
    // const fileStream = fs.createReadStream('/tmp/bankCard.png');
    // recognizeBankCardAdvanceRequest.imageURLObject = fileStream;
    // Scenario 2: Use any accessible URL.
    const url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeBankCard/yhk1.jpg");
    const httpClient = (url.protocol == "https:") ? https : http;
    recognizeBankCardAdvanceRequest.imageURLObject = await getResponse(httpClient, url);
    let runtime = new TeaUtil.RuntimeOptions({});
    // 6. This example uses the RecognizeBankCard operation of OCR. For other capabilities, use the package and class of the corresponding category. The method name `recognizeBankCardAdvance` must also be changed to the method name of the corresponding capability. The method name is generated based on the capability name according to specific rules. For example, if the capability name is `SegmentCommonImage`, the method name is `segmentCommonImageAdvance`.
    client.recognizeBankCardAdvance(recognizeBankCardAdvanceRequest, runtime)
    .then(function(recognizeBankCardResponse) {
      // Obtain the full response.
      console.log(recognizeBankCardResponse);
      // Obtain a specific field from the response.
      console.log(recognizeBankCardResponse.body.data);
    }, function(error) {
      // Obtain the full error message.
      console.log(error);
      // Obtain a specific field from the error message.
      console.log(error.data.Code);
    })
  } catch (error) {
    console.log(error);
  }
}();
Note

The parts of the code that you need to modify are described in the comments. The following list provides a summary:

  1. When importing packages, import the package and related classes of the corresponding category. You can find package names in the preceding list of SDK packages and find the capability name in the Action parameter of the corresponding API documentation.

    For example, to use the SegmentCommonImage API operation, you can learn from the API documentation for SegmentCommonImage that this capability belongs to the Image Segmentation category (imageseg20191230) and the capability name is SegmentCommonImage. You must change ocr20191230 to imageseg20191230 and RecognizeBankCard to SegmentCommonImage in the code.

  2. Make sure to change the endpoint to the one for the correct category. If the endpoint and category do not match, an InvalidAction.NotFound error is returned. For more information about endpoints, see Endpoints.

  3. The Client class must be from the package of the corresponding category.

  4. The Request and Response objects must be from the package and classes of the corresponding category.

  5. When you call a Client method, change the method name to the one for the corresponding capability. The method name is generated based on the capability name. For example, if the capability name is SegmentCommonImage, the method name is segmentCommonImageAdvance.

FAQ

How do I troubleshoot API call errors?

If an API call fails, first upgrade the SDK package to the latest version, available at the SDK link for each category. If your program uses multiple packages, upgrade all of them to their latest versions to prevent conflicts.

Latest package version not in npm

If you find that the latest package version displayed in the OpenAPI interface is not available in the npm repository, this is because the new version has just been released and repository synchronization may be delayed. If you encounter this issue, please try again later or use the latest version that is available in the npm repository.

Client-side SDK usage

No, this SDK is designed for server-side use. To call the API from a web client or mini program, you must build and sign the request manually based on the request signing mechanism. For more information, see Direct calls from a web client or Direct calls in mini program scenarios.

Technical support

If the preceding suggestions do not resolve your issue, join the Alibaba Cloud Vision Intelligence Platform support group on DingTalk (ID: 23109592). A technical specialist will be available to assist you.