downloadAttachment
async downloadAttachment(
guid: string,
options?: {
original?: boolean;
force?: boolean;
height?: number;
width?: number;
quality?: number;
}
): Promise<Buffer>
Download an attachment by its GUID. Supports image resizing and quality adjustment.
Parameters
The GUID of the attachment to download
Download options
If true, downloads the original file without any processing. Default: false
If true, forces a fresh download even if cached. Default: false
Target height in pixels for image resizing (only applies to images)
Target width in pixels for image resizing (only applies to images)
Image quality (0-100). Lower values reduce file size. Only applies to images
Returns
Binary data of the downloaded attachment
Examples
Download Original File
const buffer = await client.attachment.downloadAttachment(
"attachment-guid-123",
{ original: true }
);
await fs.writeFile("/path/to/save/file.jpg", buffer);
Download with Resizing
const thumbnail = await client.attachment.downloadAttachment(
"attachment-guid-123",
{
width: 300,
height: 300,
quality: 80
}
);
await fs.writeFile("/path/to/thumbnail.jpg", thumbnail);
Download and Convert to Base64
const buffer = await client.attachment.downloadAttachment("attachment-guid-123");
const base64 = buffer.toString('base64');
console.log(`data:image/jpeg;base64,${base64}`);
downloadAttachmentLive
async downloadAttachmentLive(guid: string): Promise<Buffer>
Download the Live Photo component of an attachment. Only works for attachments that have a Live Photo.
Parameters
The GUID of the attachment with a Live Photo
Returns
Binary data of the Live Photo video component (typically a MOV file)
Example
// First, check if attachment has a Live Photo
const attachment = await client.attachment.getAttachment("attachment-guid-123");
if (attachment.hasLivePhoto) {
const livePhotoBuffer = await client.attachment.downloadAttachmentLive(
"attachment-guid-123"
);
await fs.writeFile("/path/to/live-photo.mov", livePhotoBuffer);
}
getAttachmentBlurhash
async getAttachmentBlurhash(
guid: string,
options?: {
height?: number;
width?: number;
quality?: number;
}
): Promise<string>
Generate a blurhash string for an image attachment. Blurhashes are compact representations of images that can be used as placeholders while loading.
Parameters
The GUID of the image attachment
Image processing options
Target height in pixels for processing
Target width in pixels for processing
Image quality (0-100) for processing
Returns
A blurhash string representing the image
Example
const blurhash = await client.attachment.getAttachmentBlurhash(
"attachment-guid-123",
{ width: 32, height: 32 }
);
console.log(blurhash); // "LGF5]+Yk^6#M@-5c,1J5@[or[Q6."
Use Case: Image Placeholders
import { decode } from 'blurhash';
// Get blurhash for fast loading
const blurhash = await client.attachment.getAttachmentBlurhash(
"attachment-guid-123"
);
// Decode to pixels for display (client-side)
const pixels = decode(blurhash, 32, 32);
// Later, load full image
const fullImage = await client.attachment.downloadAttachment(
"attachment-guid-123"
);
Error Handling
try {
const buffer = await client.attachment.downloadAttachment("invalid-guid");
} catch (error) {
if (error.response?.status === 404) {
console.error('Attachment not found');
} else {
console.error('Download failed:', error);
}
}
See Also
getAttachment
Get metadata for a specific attachment.
Method Signature
await sdk.attachments.getAttachment(guid: string): Promise<AttachmentResponse>
Parameters
Unique identifier of the attachment
Response
Unique attachment identifier
Example
const attachment = await sdk.attachments.getAttachment("attachment-guid");
console.log(`File: ${attachment.transferName}`);
console.log(`Size: ${attachment.totalBytes} bytes`);
console.log(`Type: ${attachment.mimeType}`);
getAttachmentCount
Get the total count of attachments.
Method Signature
await sdk.attachments.getAttachmentCount(): Promise<number>
Returns
Returns the total number of attachments.
Example
const totalAttachments = await sdk.attachments.getAttachmentCount();
console.log(`Total attachments: ${totalAttachments}`);