JavaScript for Uploading Documents in Rossum API

This JavaScript code shows how to upload a document automatically using Rossum API to a particular Rossum queue.

This JavaScript NodeJS (version 10+) example demonstrates how to upload a document using Rossum API to a particular Rossum queue. Set the options at the beginning of the script – they store your credentials, and also the identifier of the queue to upload in. You can find out the queue id by looking at your main screen URL, it is the number 12345 in https://elis.rossum.ai/annotations/12345.

This code is designed to run using NodeJS - install NodeJS first. It also requires the node-fetch and form-data packages, install it using npm install node-fetch and npm install form-data in a command line terminal. Finally, run this script by saving it as example.js and then typing nodejs example.js in the command line.

const fetch = require('node-fetch');
const fs = require('fs');
const FormData = require('form-data');
const queueId = 'ID OF QUEUE WHERE DOCUMENT SHOULD BE IMPORTED';
const username = 'YOUR USERNAME';
const password = 'YOUR PASSWORD';
const path = 'path/to/the/file';
const queueUploadUrl = `https://api.elis.rossum.ai/v1/queues/${queueId}/upload`;

fs.readFile(path, (err, data) => {
  let form = new FormData();
  const { length, [length - 1]: filename } = path.split('/');
  form.append('content', data, filename);
  return fetch(queueUploadUrl, {
    method: 'POST',
    body: form,
    headers: {
      Authorization: `Basic ${new Buffer.from(
        `${username}:${password}`,
      ).toString('base64')}`,
    },
  })
    .then((response) => {
      if (response.ok) {   
        return response.json();
      }
      throw new Error(`${response.status}: ${response.statusText}`);
    })
    .then(({ results: [{ annotation }] }) =>
      console.log(`The file is reachable at the following URL: ${annotation}`),
    )
    .catch((err) => {
      console.log('Was not able to upload the document...', err);
    });
});

See the reference API documentation for more details about how to use the upload API.

Moreover, the following pure JavaScript example demonstrates how to send extra information to populate data field value in Rossum during API document upload.

In the example below, we are using values form field to pass the Internal Document ID while uploading document to Rossum. This value will be used for populating value of datafield Internal Document ID in schema by setting its rir_field_names attribute accordingly ("upload:internal_document_id"). Internal Document ID may be referenced in schema like this:

{
        "category": "datapoint",
        "id": "internal_document_id",
        "label": "Internal Document Number",
        "type": "string",
        "rir_field_names": ["upload:internal_document_id"],
        ...
}

See the reference API documentation and Populating data field value from API upload section of this article for more details about how to initialize datapoint values during document upload.

You should be able to extend the form above by adding the document ID value.

const documentId = 123
const valuesInput = {"upload:internal_document_id":documentId};
formData.append('values', JSON.stringify(valuesInput));