Skip to Content

Attachments and media

Read attachment and media reference properties with fetchContents and fetchMetadata, and upload files by passing them to action parameters.

Foundry docsMedia set API

Objects store files in two property types. Attachment properties hold files uploaded through actions. Media reference properties point to items in a Foundry media set, such as images, documents, audio, and video. Both load lazily: the object holds a handle, and you download the content when you need it.

The samples assume Employee has an attachment property resume and a media reference property photo, and that an action updateEmployeeFiles takes employee, resume (attachment), and photo (media reference).

Read an attachment

An attachment property is an Attachment:

MemberTypeDescription
ridstringThe attachment RID.
fetchMetadata()Promise<AttachmentMetadata>Loads rid, filename, sizeBytes, and mediaType.
fetchContents()Promise<Response>Downloads the file. Read it with blob(), arrayBuffer(), or text().
src/downloadResume.ts
import { Employee } from "@my-app/sdk";
import { client } from "./client";

const employee = await client(Employee).fetchOne(12345, { $select: ["resume"] });

if (employee.resume != null) {
  const metadata = await employee.resume.fetchMetadata();
  console.log(metadata.filename, metadata.mediaType, metadata.sizeBytes);

  const response = await employee.resume.fetchContents();
  const blob = await response.blob();
  console.log(`Downloaded ${blob.size} bytes`);
}

Read a media reference

A media reference property is a Media:

MemberTypeDescription
fetchMetadata()Promise<MediaMetadata>Loads mediaType, sizeBytes, and path when the item has one.
fetchFullMetadata?()Promise<MediaFullMetadata>Loads type-specific metadata, such as page count for documents and dimensions for images. Optional in the type, but every Media the client returns has it.
fetchContents()Promise<Response>Downloads the item.
getMediaReference()MediaReferenceThe reference itself: the MIME type and the media set item it points to. Pass it to an action or function.
src/employeePhoto.ts
import { Employee } from "@my-app/sdk";
import { client } from "./client";

const employee = await client(Employee).fetchOne(12345, { $select: ["photo"] });

const metadata = await employee.photo?.fetchMetadata();
console.log(metadata?.mediaType, metadata?.sizeBytes);

const full = await employee.photo?.fetchFullMetadata?.();
if (full?.itemMetadata.type === "imagery") {
  console.log(full.itemMetadata.dimensions);
}

const response = await employee.photo?.fetchContents();
if (response != null) {
  const image = await response.blob();
}

itemMetadata.type is one of "document", "imagery", "audio", "video", "dicom", "email", "model3d", "spreadsheet", or "untyped". When the server returns a type this client version does not know, type is "unknown" and the payload is on raw.

fetchContents resolves to a standard Response on success and rejects with a PalantirApiError on a non-2xx status, so wrap it in try/catch.

Upload through an action

The client does not write files to objects directly. Pass the file as an action parameter, and the client uploads it before it applies the action:

Parameter typePass
AttachmentcreateAttachmentUpload(blob, fileName), a File, or the RID of an existing attachment.
Media referenceA MediaUpload ({ data: blob, fileName }), a Media from another object, or a MediaReference.
src/uploadFiles.ts
import { updateEmployeeFiles } from "@my-app/sdk";
import { createAttachmentUpload, type MediaUpload } from "@osdk/client";
import { openAsBlob } from "node:fs";
import { client } from "./client";

const resume = await openAsBlob("./resume.pdf", { type: "application/pdf" });
const photo: MediaUpload = {
  data: await openAsBlob("./photo.png", { type: "image/png" }),
  fileName: "photo.png",
};

await client(updateEmployeeFiles).applyAction({
  employee: 12345,
  resume: createAttachmentUpload(resume, "resume.pdf"),
  photo,
});

openAsBlob needs Node.js 19.8 or later.

To reuse a file that is already in Foundry, pass its handle instead of uploading again:

src/copyPhoto.ts
import { Employee, updateEmployeeFiles } from "@my-app/sdk";
import { client } from "./client";

const source = await client(Employee).fetchOne(12345, { $select: ["resume", "photo"] });

await client(updateEmployeeFiles).applyAction({
  employee: 67890,
  resume: source.resume?.rid,
  photo: source.photo,
});

Uploads are temporary until an action links them to an object. If the action fails, or you never apply one, Foundry deletes the uploaded file after one hour.

Functions accept the same values for attachment and media reference parameters. See Functions.

On this page