Skip to content

Media

Upload a file once to get a reusable media id, then send it with any of the media senders. upload accepts a Blob/File, or { file, type, filename } wrapping Blob/Uint8Array/ArrayBuffer bytes. A content type is required: it comes from a Blob’s type or the explicit type, otherwise upload throws WhatsAppValidationError.

import { readFile } from 'node:fs/promises';
const bytes = await readFile('./photo.jpg');
const { id } = await client.media.upload({
file: bytes,
type: 'image/jpeg',
filename: 'photo.jpg',
});
// Reuse the id with any media sender.
await client.messages.sendImage('15551234567', { id });

Resolve a media id to its metadata, then download the bytes. The url returned by getUrl is short-lived and must be fetched with the access token — download handles that for you (it adds the bearer token and a User-Agent) and returns the raw bytes as a Uint8Array.

const info = await client.media.getUrl(id);
console.log(info.mimeType, info.fileSize); // e.g. "image/jpeg" 20481
// download() calls getUrl() internally, then fetches the short-lived url.
const { data, mimeType } = await client.media.download(id);
await writeFile('./downloaded.jpg', data); // data is a Uint8Array

Delete media you no longer need:

const { success } = await client.media.delete(id);

Uploaded media is retained by Meta for 30 days, after which the id stops resolving. Per-type upload size limits apply: images 5 MB, video 16 MB, audio 16 MB, documents 100 MB, and stickers 100 KB (static) / 500 KB (animated).

Independent open-source project. Not affiliated with, endorsed by or sponsored by Meta or WhatsApp. Released under the MIT licence. Source on GitHub.