Skip to main content

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

guid
string
required
The GUID of the attachment to download
options
object
Download options

Returns

Buffer
Buffer
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

guid
string
required
The GUID of the attachment with a Live Photo

Returns

Buffer
Buffer
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

guid
string
required
The GUID of the image attachment
options
object
Image processing options

Returns

blurhash
string
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

guid
string
required
Unique identifier of the attachment

Response

guid
string
Unique attachment identifier
transferName
string
Original file name
totalBytes
number
File size in bytes
mimeType
string
MIME type of the file
uti
string
Uniform Type 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}`);

Build docs developers (and LLMs) love