Skip to content

Upload and Attach a Custom File to a Record

Tim Williamson edited this page May 13, 2025 · 2 revisions

This example uploads a file to Aprimo and attaches it as a custom additional file to the latest version of the masterfile on a record.

import { createClient, Expander } from "aprimo-js";

// Setup
const aprimo = createClient({
  type: "client_credentials",
  environment: "your-env",
  clientId: "your-client-id",
  clientSecret: "your-client-secret",
});

const recordId = "your-record-id";

// Upload a file

// All additional files of this version need to have a unique filename.
const file = new File([new Blob(["hello world"])], `file_${Math.random().toString(36).slice(2)}.txt`);
const upload = await aprimo.uploader.uploadFile(file);
const uploadToken = upload.data.token;

// Get record with masterfile + versions
const expander = Expander.create()
  .for("Record").expand("masterfile")
  .for("File").expand("fileversions");

const getRecordResponse = await aprimo.records.getById(recordId, expander);
const masterfile = getRecordResponse.data._embedded.masterfile;
const latestVersion = masterfile._embedded.fileversions.items.find(v => v.isLatest);

// Attach uploaded file
const updateRecordResponse = await aprimo.records.update(recordId, {
  files: {
    addOrUpdate: [
      {
        id: masterfile.id,
        versions: {
          addOrUpdate: [
            {
              id: latestVersion.id,
              additionalFiles: {
                addOrUpdate: [
                  {
                    id: uploadToken,
                    label: file.name,
                    filename: file.name,
                    type: "Custom"
                  }
                ]
              }
            },
          ],
        },
      },
    ],
  },
});

if (!updateRecordResponse.ok) console.error(updateRecordResponse.error);

Clone this wiki locally