Community

Start Vantage transaction from NodeJS script

NodeJS is a popular platform for server and client development. There are some aspects of working with files.

For starting transaction, you must upload file via POST call multipart/form-data. The easiest way to do this is to use the 'request' library

function LaunchTransaction (access_token, filepath, fileName, skillid,callback){

// create file payload
files=[];
files.push('application/pdf');
files.push(fileName);
files.push (fs.createReadStream(filepath));

var payloadform = {
'file' : files
}
var contentLength = payloadform.length;

const options = {
url: VURL+'/api/publicapi/v1/transactions/launch?skillId='+skillid,
method: 'POST',
//proxy: 'http://127.0.0.1:8080', // capture traffic. be sure that proxy runs
headers: {
'Authorization' : 'Bearer '+ access_token,
'Content-Length': contentLength,
'Content-Type': 'multipart/form-data'
},
formData : payloadform
};
request(options, function(err, res, body) {
if(err) console.log(err);
let json = JSON.parse(body);
return callback(json.transactionId);
});
}

There are the following requirements to make a correct call:

  • Every file should be encapsulated to an Array
  • The request should have content length in the headers
  • A payload form contains the JSON structure 'file' : fileArray


The complete script of console NodeJS application is below

const request = require('request');
const fs=require('fs');
const VURL ='https://vantage.abbyy.com';
const VLOGIN ='your-login';
const VPASSWORD ='your-password';
const VANTAGE_SKILL_ID= 'fae19ed1-f4d5-42ed-87f2-d10a3b03171d';
function Authenticate (login,password,callback){
  const options = {
      url: VURL+'/auth/connect/token',
      method: 'POST',
      form : {
          'grant_type': 'password',
          'scope': 'openid permissions',
          'username' : login,
          'password' : password,
          'client_id' : 'ABBYY.Vantage',
          'client_secret' :'f3ec6136-6ccc-75a1-3780-caeef723e998'
        }
  };
  request(options, function(err, res, body) {
      if(err) console.log(err);
      let json = JSON.parse(body);
      return callback(json.access_token);
  });
}
function LaunchTransaction (access_token, filepath, fileName, skillid,callback){
  // create file payload
  files=[];
  files.push('application/pdf');
  files.push(fileName);
  files.push (fs.createReadStream(filepath));
  var payloadform = {
      'file' : files
  }
  var contentLength = payloadform.length;
  const options = {
      url: VURL+'/api/publicapi/v1/transactions/launch?skillId='+skillid,
      method: 'POST',
      headers: {
          'Authorization' : 'Bearer '+ access_token,
          'Content-Length': contentLength,
          'Content-Type': 'multipart/form-data'
      },
      formData : payloadform
  };
  request(options, function(err, res, body) {
      if(err) console.log(err);
      let json = JSON.parse(body);
      return callback(json.transactionId);
  });
}
function GetTransactionResults(access_token,transactionID, callback){
  const options = {
      url: VURL+'/api/publicapi/v1/transactions/'+transactionID,
      method: 'GET',
      headers: {
          'Authorization' : 'Bearer '+ access_token
      }
  };
  request(options, function(err, res, body) {
      if(err) console.log(err);
      return callback (body)
  });
}
function GetProcessedFile(access_token,transactionID,fileID,filepath){
  const options = {
      url: VURL+'/api/publicapi/v1/transactions/'+transactionID+'/files/'+fileID+'/download',
      method: 'GET',
      headers: {
          'Authorization' : 'Bearer '+ access_token
      }
  };
  let file = fs.createWriteStream(filepath);
  let stream = request(options)
  .pipe(file)
  .on('finish', () => {
      console.log(`The file has been downloaded`);
      //resolve();
  })
  .on('error', (error) => {
      //reject(error);
  })
}
// main flow
Authenticate(VLOGIN,VPASSWORD,function (data){
  _access_token = data;
  console.log('Authenticated')
  LaunchTransaction(_access_token,"./invoice.PDF",'invoice.pdf',VANTAGE_SKILL_ID, function(data){
      _transactionID=data;
      console.log('Transaction ID: '+_transactionID);
      console.log('Wait for 15 seconds');
      setTimeout(
          GetTransactionResults,
          15000,
          _access_token,_transactionID,function(data){
              let json = JSON.parse(data);
              console.log('Transaction status: '+json.status);
              var files=json.documents[0].resultFiles;
              var _fileName='';
              var _fileID='';
              for (k=0;k<files.length;k++){ //looking for json file
                if (files[k].type=="FieldsJson"){
                  _fileID=files[k].fileId;
                  _fileName=files[k].fileName;
                  break;
                }
              }
              console.log('File Name:'+_fileName);
              console.log('File ID: '+_fileID);
              GetProcessedFile(_access_token,_transactionID,_fileID,'./'+_fileName)
          }
      )
  })
});





Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.