Node.js SDK
Set up the Node.js SDK environment and install the required dependencies:
npm install @alicloud/docmind-api20220711 -S
npm install @alicloud/tea-util -SConfigure authentication
Create an AccessKey pair.
For more information, see Create an AccessKey pair.
ImportantAn Alibaba Cloud account's AccessKey pair grants access to all APIs. For security, we recommend using a RAM user for API calls and routine O&M. To do this, grant the RAM user the required permissions for Document Intelligence and then use the RAM user's AccessKey pair to call the SDK. For more information, see Control permissions on Document Intelligence by using RAM users.
We strongly recommend against hard-coding your AccessKey ID and AccessKey Secret in your project code. Doing so can expose your AccessKey pair, which compromises the security of all resources in your account.
For greater security, consider creating a RAM role and using a temporary credential from STS to call the service. For more information, see Use an STS-authorized account to call a service.
Add the credentials dependency.
npm install @alicloud/credentialsConfigure authentication.
This topic provides an example that uses a configuration file. For more information, see Manage access credentials.
Submit a document processing job
Document Intelligence offers asynchronous job APIs. You can call them with the Node.js SDK by uploading a local file or providing a document URL.
If you are processing a large file, the operation may take a long time. You can set the following properties in the client configuration to adjust the timeout periods.
// Connection timeout
connectTimeout: 60000,
// Read timeout
readTimeout: 60000For a complete list of Document Intelligence OpenAPI operations, see API overview.
Submit a job using a local document
The APIs for converting images to Word, Excel, or PDF formats do not support file uploads.
This code example shows how to submit a job by uploading a local file.
const Client = require('@alicloud/docmind-api20220711');
const Credential = require('@alicloud/credentials');
const Util = require('@alicloud/tea-util');
const fs = require('fs');
const getResult = async () => {
// The program reads your AccessKey from the credential to automatically authenticate API calls.
// Before running this example, complete the "Configure authentication" section.
// This example uses the default configuration file to create a default access credential.
// Initialize the Credentials client with the default credential.
const cred = new Credential.default();
const client = new Client.default({
// The endpoint. IPv4 and IPv6 are supported. For IPv6, use docmind-api-dualstack.cn-hangzhou.aliyuncs.com.
endpoint: 'docmind-api.cn-hangzhou.aliyuncs.com',
// Get the AccessKey ID from the credential object.
accessKeyId: cred.credential.accessKeyId,
// Get the AccessKey Secret from the credential object.
accessKeySecret: cred.credential.accessKeySecret,
type: 'access_key',
regionId: 'cn-hangzhou'
});
const advanceRequest = new Client.SubmitDocStructureJobAdvanceRequest();
const file = fs.createReadStream('./example.pdf');
advanceRequest.fileUrlObject = file;
advanceRequest.fileName = 'example.pdf';
const runtimeObject = new Util.RuntimeOptions({});
const response = await client.submitDocStructureJobAdvance(advanceRequest, runtimeObject);
return response.body;
}The following response is returned.
{
"requestId": "43A29C77-405E-4CC0-BC55-EE694AD0****",
"data": {
"id": "docmind-20220712-b15f****"
}
}Submit a job using a document URL
The document URL must be a publicly accessible, downloadable URL without cross-origin restrictions or special escaped characters.
This code example shows how to submit a job using a document URL.
const Client = require('@alicloud/docmind-api20220711');
const Credential = require('@alicloud/credentials');
const getResult = async () => {
// The program reads your AccessKey from the credential to automatically authenticate API calls.
// Before running this example, complete the "Configure authentication" section.
// This example uses the default configuration file to create a default access credential.
// Initialize the Credentials client with the default credential.
const cred = new Credential.default();
const client = new Client.default({
// The endpoint. IPv4 and IPv6 are supported. For IPv6, use docmind-api-dualstack.cn-hangzhou.aliyuncs.com.
endpoint: 'docmind-api.cn-hangzhou.aliyuncs.com',
// Get the AccessKey ID from the credential object.
accessKeyId: cred.credential.accessKeyId,
// Get the AccessKey Secret from the credential object.
accessKeySecret: cred.credential.accessKeySecret,
// If you are processing a large file that takes a long time, you can set the following connectTimeout and readTimeout properties.
// Connection timeout
connectTimeout: 60000,
// Read timeout
readTimeout: 60000,
type: 'access_key',
regionId: 'cn-hangzhou'
});
const request = new Client.SubmitDocStructureJobRequest();
request.fileName = 'example.pdf';
request.fileUrl = 'https://example.com/example.pdf';
const response = await client.submitDocStructureJob(request);
return response.body;
}The following response is returned.
{
"requestId": "43A29C77-405E-4CC0-BC55-EE694AD0****",
"data": {
"id": "docmind-20220712-b15f****"
}
}Query job results
When you query job results, a job has one of three statuses: processing, successful, or failed.
This code example shows how to query job results.
const Client = require('@alicloud/docmind-api20220711');
const Credential = require('@alicloud/credentials');
const getResult = async () => {
// The program reads your AccessKey from the credential to automatically authenticate API calls.
// Before running this example, complete the "Configure authentication" section.
// This example uses the default configuration file to create a default access credential.
// Initialize the Credentials client with the default credential.
const cred = new Credential.default();
const client = new Client.default({
// The endpoint. IPv4 and IPv6 are supported. For IPv6, use docmind-api-dualstack.cn-hangzhou.aliyuncs.com.
endpoint: 'docmind-api.cn-hangzhou.aliyuncs.com',
// Get the AccessKey ID from the credential object.
accessKeyId: cred.credential.accessKeyId,
// Get the AccessKey Secret from the credential object.
accessKeySecret: cred.credential.accessKeySecret,
type: 'access_key',
regionId: 'cn-hangzhou'
});
const resultRequest = new Client.GetDocStructureResultRequest();
resultRequest.id = "docmind-20220902-824b****";
const response = await client.getDocStructureResult(resultRequest);
return response.body;
}The response indicates one of three statuses: processing, successful, or failed.
The following response is returned if the job is still processing. The Completed field is false. In this case, you need to continue polling until Completed is true or the maximum polling time is reached.
{
"requestId": "2AABD2C2-D24F-12F7-875D-683A27C3****",
"completed": false,
"code": "DocProcessing",
"message": "Document processing",
"hostId": "ocr-api.cn-hangzhou.aliyuncs.com",
"recommend": "https://next.api.aliyun.com/troubleshoot?q=DocProcessing&product=docmind-api"
}If the job is successful, the API returns a response where the Completed field is true, the Status is Success, and detailed results are in the Data node.
{
"Status": "Success",
"RequestId": "73134E1A-E281-1B2C-A105-D0EF****",
"Completed": true,
"Data": {
"styles": [{
"styleId": 0,
"underline": false,
"deleteLine": false,
"bold": true,
"italic": false,
"fontSize": 15,
"fontName": "Heiti",
"color": "000000",
"charScale": 0.95
},
{
"styleId": 1,
"underline": false,
"deleteLine": false,
"bold": false,
"italic": false,
"fontSize": 12,
"fontName": "Microsoft YaHei",
"color": "000000",
"charScale": 1
}
],
"layouts": [{
"text": "Sample Title",
"index": 0,
"uniqueId": "xxxx9816e77caea338df554b80ab95c7",
"alignment": "center",
"pageNum": [
0
],
"pos": [{
"x": 405,
"y": 192
},
{
"x": 860,
"y": 191
},
{
"x": 860,
"y": 236
},
{
"x": 406,
"y": 237
}
],
"type": "title",
"subType":"doc_title"
}, {
"text": "This paragraph is for testing purposes.",
"index": 1,
"uniqueId": "xxxx8606c213c01c12d70f98dcfb2525",
"alignment": "left",
"pageNum": [
0
],
"pos": [{
"x": 187,
"y": 311
},
{
"x": 1075,
"y": 311
},
{
"x": 1076,
"y": 373
},
{
"x": 187,
"y": 373
}
],
"type": "text",
"subType":"para",
"lineHeight": 7,
"firstLinesChars": 30,
"blocks": [{
"text": "This paragraph",
"pos": null,
"styleId": 0
},
{
"text": " is for testing purposes.",
"pos": null,
"styleId": 1
}
]
}],
"logics": {
"docTree": [{
"uniqueId": "xxxx9816e77caea338df554b80ab95c7",
"level": 0,
"link": {
"Children": [
],
"Contains": [
]
},
"backlink": {
"Parent": [
"ROOT"
]
}
}],
"paragraphKVs": null,
"tableKVs": null
},
"docInfo": {
"docType": "pdf",
"orignalDocName": "1.pdf",
"pages": [{
"imageType": "JPEG",
"imageUrl": "http://test.moshi.aliyuncs.com/docMind/image/xxxx3cccbfec45b48d3a8081c9c9659e/0",
"angle": null,
"imageWidth": 1273,
"imageHeight": 1801,
"pageIdCurDoc": 1,
"pageIdAllDocs": 1
}]
}
}
}The following response is returned if the job fails. The Completed field is true, and the status is Fail. The response also includes an error code (code) and a detailed message (message). For more information about error codes, see Error codes.
{
"requestId": "A8EF3A36-1380-1116-A39E-B377BE27****",
"completed": true,
"status": "Fail"
"code": "UrlNotLegal",
"message": "Failed to process the document. The document url you provided is not legal.",
"hostId": "docmind-api.cn-hangzhou.aliyuncs.com",
"recommend": "https://next.api.aliyun.com/troubleshoot?q=IDP.UrlNotLegal&product=docmind-api"
}