Upload files with the TypeScript SDK
One call for a file up to 1 GB, a way to finish after a crash, and a whole folder at once.
One file
client.documents.upload does every step of an upload: it hashes the file, starts the upload, sends the parts to storage four at a time, gets new addresses for parts whose address expired, sends a failed part again, and completes the upload.
import { ByteRover } from "@byterover/sdk";
const client = new ByteRover();
const driveId = "drv_4h7k2m9q8r1t5v6w3x0y";
const { document, operation } = await client.documents.upload(driveId, "/policies/customer-refund-policy.pdf", "./customer-refund-policy.pdf", {
tags: ["policy"],
wait: "searchable",
});
console.log(document.status, operation.status);The path decides the document: a document already at the path gets a new revision. By default the call returns once the upload is complete. With wait: "done" it also waits for the file's operation to finish, and with wait: "searchable" it fails unless the document is then searchable. waitTimeout is 10 minutes by default.
The source can be a file path, a Blob or File, bytes, or an object with a size and a read(start, end) method. The SDK reads it twice, once to hash it and once to send it, so a stream that can be read only once is refused.
The API chooses the part size: every part but the last is part_size bytes, 8 MiB today. onUploadCreated hands you part_count and part_size.
After the upload completed
With wait, anything that fails after the upload completed is an UploadWaitError. The document exists, so don't upload the file again: the error has its document_id and operation_id, and the first error as its cause.
import { UploadWaitError } from "@byterover/sdk";
try {
await client.documents.upload(driveId, "/reports/q3.pdf", "./q3.pdf", { wait: "done" });
} catch (error) {
if (!(error instanceof UploadWaitError)) throw error;
console.log("Saved as", error.document_id, "but", error.cause);
await client.operations.wait(error.operation_id);
}Survive a crash
Pass your own idempotencyKey when an upload must survive your process stopping. Run the same upload again, with the same key and the same file, and it either finishes the earlier upload or answers the document the earlier run made. The file is never stored twice. Keys last 24 hours. Here a key can have at most 240 characters, not 255: the upload makes two keys from yours, <key>:create and <key>:complete, and each must fit the API's limit.
await client.documents.upload(driveId, "/reports/q4.pdf", "./q4.pdf", { idempotencyKey: "report-q4-2026" });To finish a large upload after a crash without starting a new one, keep what onUploadCreated hands you, and resume with the same file. A resume works while the upload is still open, for up to 24 hours, and sends every part again.
import type { SavedUpload } from "@byterover/sdk";
import { readFile, writeFile } from "node:fs/promises";
await client.documents.upload(driveId, "/archive/2025.pdf", "./2025.pdf", {
onUploadCreated: (saved) => writeFile("upload.json", JSON.stringify(saved)),
});
// After a restart:
const saved = JSON.parse(await readFile("upload.json", "utf8")) as SavedUpload;
await client.uploads.resume(saved, "./2025.pdf");When a part can't be sent, the call throws UploadIncompleteError. Nothing was completed, so you can resume the upload while it is still open: the error has its upload_id, so you know which saved upload it is.
If completing an upload loses its response, the SDK completes it again, which is safe: a completed upload answers with its document. While another completion is still running, it keeps asking for completeWait, 2 minutes by default, then throws UploadIncompleteError: look for the document with documents.list before you upload the file again. Raise completeTimeout for very large files.
A whole folder
client.documents.uploadDirectory uploads every file whose type ByteRover reads. Hidden files and folders, such as .git and .env, empty files, symbolic links and files of a type ByteRover doesn't read are skipped and listed in skipped. Links inside the folder are never followed, and a subfolder or file swapped for a link while the folder is read is refused (in failed). The folder you pass is read even when it is itself a link. The checks go by path, so a program that keeps swapping a folder for a link while the upload runs could still, with exact timing, get a file from outside read: don't upload a folder that other programs are changing. A file or folder that can't be read, or that changed during the upload (swapped for a link, a folder or another file), is listed in failed. includeHidden and ignore change that. It isn't all or nothing: the result lists what succeeded, what failed and what was skipped. upload passes wait, waitTimeout and partConcurrency to each file's upload, so upload: { wait: "searchable" } waits until every file can be searched; they aren't top-level options here. A file in uploaded_not_ready was uploaded, but its wait failed: don't upload it again. Each error is typed unknown: check it with instanceof ByteRoverError (or a subclass) before you read its fields. onProgress gets each file's document_id once it exists. An abort rejects with your signal's reason and no result, so keep what onProgress reports. A file whose completion was cut off by the abort may still have been stored: look for it with documents.list before you upload it again.
const result = await client.documents.uploadDirectory(driveId, "./handbook", {
prefix: "/handbook",
tags: ["handbook"],
ignore: (path) => path.startsWith("drafts/"),
upload: { wait: "searchable" },
onProgress: ({ done, total, path }) => console.log(`${done}/${total} ${path}`),
});
for (const failure of result.failed) console.log(failure.path, failure.error);
for (const file of result.uploaded_not_ready) console.log(file.path, "is uploaded as", file.document_id);
console.log(result.succeeded.length, "uploaded,", result.skipped.length, "skipped");Step by step
The steps are there too: client.uploads.create, listParts, putPart and complete. putPart sends no API key, since a part goes straight to storage, and never logs the part's address, which is itself a credential.
import { createHash } from "node:crypto";
import * as fs from "node:fs/promises";
const bytes = await fs.readFile("./notes.pdf");
const plan = await client.uploads.create(driveId, {
path: "/notes/notes.pdf",
size_bytes: bytes.byteLength,
sha256: createHash("sha256").update(bytes).digest("hex"),
});
const parts: { part_number: number; etag: string }[] = [];
for await (const part of client.uploads.listParts(plan.upload_id)) {
const start = (part.part_number - 1) * plan.part_size;
const { etag } = await client.uploads.putPart({ url: part.url, body: bytes.subarray(start, start + plan.part_size) });
parts.push({ part_number: part.part_number, etag });
}
const written = await client.uploads.complete(plan.upload_id, { parts });
console.log(written.document.id);