Workspaces

Workspace files

client.workspaces.files lists, reads, replaces and deletes the files you uploaded to a workspace. To make them searchable, use client.workspaces.ingest.

  • Arguments: every method takes the workspaceId first, and the file methods take the fileId next.
  • A missing file: one that doesn't exist, or was deleted, rejects with NotFoundError, code file_not_found.
  • Cost: listing, reading, replacing and deleting files is free, at any balance.

See List workspace files in the API reference.

List workspace files

client.workspaces.files.list(workspaceId, params?, options?) returns APIPromise<WorkspaceFilePage>. params is a WorkspaceFileListParams; each list filter is sent once per value.

ParameterTypeRequiredDescription
workspaceIdstringYesThe workspace.
params.path_prefixstringNoOnly files whose path starts with this.
params.ingestion_statusIngestionStatus[]NoOnly files in these ingestion statuses.
params.categoriesstring[]NoOnly files in these categories, set by ingestion.
params.file_typesstring[]NoOnly these extensions, such as pdf or xlsx.
params.modified_afterstringNoOnly files whose updated_at falls after this time, ISO 8601.
params.modified_beforestringNoOnly files whose updated_at falls before this time, ISO 8601.
params.limitnumberNo1 to 200. Default: 50. Out of range throws a NaceError.
params.cursorstring | nullNoThe previous page's next_cursor. Keep going until it's null.
list-files.ts
const page = await client.workspaces.files.list(workspaceId, {
  path_prefix: "reports/",
  ingestion_status: ["stale"],
});
for (const file of page.items) console.log(file.path, file.ingestion_status);

Read a workspace file

client.workspaces.files.get(workspaceId, fileId, options?) returns APIPromise<WorkspaceFileDetail>, the file with download_url and preview_url, links to its bytes that work without your API key for 15 minutes.

ParameterTypeRequiredDescription
workspaceIdstringYesThe workspace.
fileIdstringYesThe file.
get-file.ts
const file = await client.workspaces.files.get(workspaceId, fileId);
console.log(file.path, file.download_url);

File metadata

client.workspaces.files.metadata(workspaceId, fileId, options?) returns APIPromise<WorkspaceFileMetadata>, what ingestion learned about the file, its outputs, and whether each representations entry is current. Most of it is empty until the file is ingested. See File metadata.

ParameterTypeRequiredDescription
workspaceIdstringYesThe workspace.
fileIdstringYesThe file.
file-metadata.ts
const metadata = await client.workspaces.files.metadata(workspaceId, fileId);
console.log(metadata.lane, metadata.representations, metadata.outputs?.length);

File versions

client.workspaces.files.versions(workspaceId, fileId, params?, options?) returns APIPromise<WorkspaceFileVersionPage>, the file's versions, newest first. Only the current version's bytes are kept. params is a WorkspaceFileVersionListParams.

ParameterTypeRequiredDescription
workspaceIdstringYesThe workspace.
fileIdstringYesThe file.
params.limitnumberNo1 to 200. Default: 50.
params.cursorstring | nullNoThe previous page's next_cursor.
file-versions.ts
const { items } = await client.workspaces.files.versions(workspaceId, fileId);
for (const version of items) console.log(version.version, version.size_bytes, version.is_current);

Replace a workspace file

client.workspaces.files.replace(workspaceId, fileId, content, params?, options?) returns APIPromise<DocumentJob>, a job that has already succeeded, with result.file and result.previous_version.

ParameterTypeRequiredDescription
workspaceIdstringYesThe workspace.
fileIdstringYesThe file. Its path stays the same.
contentFileContentYesThe new bytes: a Blob (a File included), a Uint8Array or an ArrayBuffer.
params.file_namestringNoThe name sent with the bytes. The file keeps its path either way. Default: the File's own name.
params.labelsRecord<string, string>NoYour own key-value metadata, replacing the file's. {} clears it. Default: keep them.

params is a ReplaceWorkspaceFileParams. The bytes go through Drex, so a replace takes about 4.5 MB at most; to replace a larger file, upload it to the same path with on_conflict: "new_version" instead. Sending the same bytes and labels again changes nothing. An ingested file becomes stale until you ingest it again. See Replace a file.

replace-file.ts
import { readFile } from "node:fs/promises";

const job = await client.workspaces.files.replace(workspaceId, fileId, await readFile("report-v2.pdf"));

Delete a workspace file

client.workspaces.files.delete(workspaceId, fileId, params?, options?) returns APIPromise<DocumentJob>, a file_delete job. It removes the file, its outputs and its search-index entries, cancels ingestions still running on it, and can't be undone.

ParameterTypeRequiredDescription
workspaceIdstringYesThe workspace.
fileIdstringYesThe file.
params.wait_secondsnumberNo0 to 60 (MAX_WAIT_SECONDS).
params.idempotency_keystringNoUp to 200 characters. Minted per call when omitted.

params is a DeleteWorkspaceFileParams.

delete-file.ts
const done = await client.jobs.wait((await client.workspaces.files.delete(workspaceId, fileId)).job_id);

Read an output

client.workspaces.files.output(workspaceId, fileId, outputId, options?) returns APIPromise<WorkspaceFileOutput>, with links to the output's bytes. See Read an output.

ParameterTypeRequiredDescription
workspaceIdstringYesThe workspace.
fileIdstringYesThe file.
outputIdstringYesA derived_file output from the file's metadata. Another kind rejects with UnprocessableEntityError.
file-output.ts
const output = await client.workspaces.files.output(workspaceId, fileId, outputId);
console.log(output.path, output.download_url);

List output members

client.workspaces.files.outputMembers(workspaceId, fileId, outputId, params?, options?) returns APIPromise<WorkspaceFileOutputMembers>, one page of the files inside a folder output, such as a recording's frames, with links to each. A single-file output has no members.

ParameterTypeRequiredDescription
workspaceIdstringYesThe workspace.
fileIdstringYesThe file.
outputIdstringYesA folder output.
params.offsetnumberNoHow many members to skip. Default: 0. A negative one throws a NaceError.
params.limitnumberNo1 to 200. Default: 50.

params is an OutputMembersParams.

output-members.ts
const { members, total_count } = await client.workspaces.files.outputMembers(workspaceId, fileId, outputId, { limit: 100 });

Ingest files

client.workspaces.ingest(workspaceId, params?, options?) returns APIPromise<DocumentJob>, an ingestion job. Ingestion builds the search index, metadata and outputs, and is the only call that makes files searchable. One bad file doesn't fail the run: the result's outcomes reports each file. See Ingest files.

ParameterTypeRequiredDescription
workspaceIdstringYesThe workspace.
params.file_idsstring[]NoIngest these files, 1 to 1000. Not with path_prefix or stale_only.
params.path_prefixstringNoIngest the files under this path. Not with file_ids. Leave both out for the whole workspace.
params.stale_onlybooleanNoOnly files that changed since they were last ingested, under path_prefix or across the whole workspace. Default: false.
params.reingest_unchangedbooleanNoRebuild files that are already ingested and unchanged, instead of skipping them. Default: false.
params.wait_secondsnumberNo0 to 60 (MAX_WAIT_SECONDS).
params.idempotency_keystringNoUp to 200 characters. Minted per call when omitted.

params is an IngestParams. file_ids with path_prefix or stale_only throws a NaceError before sending.

ingest.ts
const job = await client.workspaces.ingest(workspaceId, { path_prefix: "reports/", stale_only: true });
const done = await client.jobs.wait(job.job_id);

On this page