JavaScript (NodeJS) for Exporting Data from Rossum API

This JavaScript NodeJS code shows how to download XML data captured in a given Rossum queue for the previous calendar day.

This JavaScript NodeJS code shows how to download XML data captured in a given queue for the previous calendar day – as part of the regular export routine. Update the parameters at the beginning based on your scenario.

The export API is more powerful - you can filter by different criteria or download the data e.g. as a JSON instead - see the full docs.

This code is designed to run using NodeJS - install NodeJS first. It also requires the node-fetch package, install it using npm install node-fetch 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 queue_id = 28984;
const loginUrl = `https://example.rossum.app/api/v1/auth/login`;
const username = '[email protected]'
const password = 'yourpassword';

let today = new Date();
let yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

const pad = (number) => number < 10 ? '0' + number : number
const toISOString = date =>
    date.getUTCFullYear() +
        '-' + pad(date.getUTCMonth() + 1) +
        '-' + pad(date.getUTCDate())

const makeUrl = (id, date_start, date_end) =>
    `https://example.rossum.app/api/v1/queues/${id}/export?format=xml&exported_at_after=${toISOString(date_start)}&exported_at_before=${toISOString(date_end)}&page_size=100&page=1`;

const createHeaders = token => ({ headers: { 'Content-Type': 'application/xml', Authorization: `token ${token}` }})

const fetchXMLs = (date_start, date_end, token) => {
    const dataStream = fs.createWriteStream('data_' + date_start.getTime() + '_' + date_end.getTime() + '.xml');
    fetch(makeUrl(queue_id, date_start, date_end), createHeaders(token))
        .then(r => r.body.pipe(dataStream))
        .catch(console.error);
}


fetch(loginUrl, {
    method: 'POST',
    body: JSON.stringify({ username, password }),
    headers: { 'Content-Type': 'application/json' }
})
.then(r => r.json())
.then(({ key }) => {
    fetchXMLs(yesterday, today, key);
})

console.log('Data are being fetched from Rossum...')

This example will export at most 100 documents. To export more documents, you need to modify the code to download multiple pages one by one, until it reaches the page count (or it can simply stop when an empty set of results is finally downloaded).