Skip to content

Get a document listing from storage - Nodejs

You can get a list of all the documents in your storage by calling the /v1/files endpoint.

ts
const path = "/v1/files";
const url = `${api_url}${path}`;
const method = "GET";
const headers = getAuthorizationHeaders(method, path);

const response = await fetch(url, {
  headers,
  method,
});

Code

ts
import dotenv from "dotenv";
dotenv.config();

import { getError } from "./errorhandling";
import { getAuthorizationHeaders } from "./authorization";

const api_url = process.env.UNOPDF_API_URL;

async function getAllDocuments() {
  // #region get-all-documents
  const path = "/v1/files";
  const url = `${api_url}${path}`;
  const method = "GET";
  const headers = getAuthorizationHeaders(method, path);

  const response = await fetch(url, {
    headers,
    method,
  });

  // #endregion get-all-documents
  if (response.status !== 200) {
    const error = await getError(response);

    console.error(error);
    throw new Error(`Error "${error}" calling UnoPdf API "${url}".`);
  }

  const body = await response.json();

  if (body && (typeof body === "object") && "documents" in body) {
    return body.documents as unknown[];
  }

  return [];
} 

getAllDocuments().then((docs) => console.log(docs.length));

Running the example

  • Download the rest - Get a document listing from storage - Nodejs project
  • Unzip the file to a directory rest-get-all-documents-nodejs.
    shell
    unzip rest-get-all-documents-nodejs.zip -d rest-get-all-documents-nodejs
  • Open a terminal and go to that directory
    shell
    cd rest-get-all-documents-nodejs
  • Install dependencies
    shell
    npm install
    shell
    yarn
  • Start the project
    shell
    npm run start
    shell
    yarn start