This example demonstrates file upload and download using the JMAP client's blob API.
Create a .env file in the repo root or export these variables in your shell:
JMAP_BEARER_TOKEN=your-token
JMAP_HOSTNAME=api.fastmail.com
JMAP_LOG_LEVEL=info
Clone the jmap-kit repository, then from the repo root:
yarn tsx examples/blob-operations/blob-operations.ts
client.uploadFile(accountId, blob) and logs the response (blobId, type, size)client.downloadFile(accountId, blobId, name, type)client.getUploadUrl() and client.getDownloadUrl()client.uploadFile() accepts a Blob, ArrayBuffer, or File and returns a response with accountId, blobId, type, and size:
const blob = new Blob(["Hello!"], { type: "text/plain" });
const response = await client.uploadFile(accountId, blob);
console.log(response.blobId);
client.downloadFile() returns a Blob that can be read as text, an ArrayBuffer, etc.:
const downloaded = await client.downloadFile(accountId, blobId, "file.txt", "text/plain");
const text = await downloaded.text();
The JMAP session provides RFC 6570 URI Templates for uploads and downloads. For example, FastMail's session includes:
uploadUrl: https://api.fastmail.com/jmap/upload/{accountId}/
downloadUrl: https://api.fastmail.com/jmap/download/{accountId}/{blobId}/{name}?type={type}
The client expands these by substituting the template variables. For example, given:
accountId: u1234abcdblobId: G5a3b7c9d1e2fname: report.txttype: text/plainThe resolved URLs would be:
Upload: https://api.fastmail.com/jmap/upload/u1234abcd/
Download: https://api.fastmail.com/jmap/download/u1234abcd/G5a3b7c9d1e2f/report.txt?type=text%2Fplain
In code:
client.getUploadUrl(accountId); // → URL
client.getDownloadUrl(accountId, blobId, "report.txt", "text/plain"); // → URL