WhatsApp Cloud TS

Media Utilities

Helper functions for handling media and read receipts.

Downloading Media

When you receive media via webhooks (e.g. an image, document, or audio message), Meta provides a media ID instead of a direct URL. You need to use the WhatsApp Cloud API to retrieve the actual download URL.

const media = await client.getMediaUrl("media-id-from-webhook");
console.log(`Download URL: ${media.url}`); 

// Note: You must pass your WA_ACCESS_TOKEN as a Bearer token in the Authorization header 
// when making a GET request to download this URL.

Uploading Media

You can upload local files directly to Meta's servers to get a reusable Media ID. This is particularly useful for sending the same media to multiple users without re-uploading, or sending large documents efficiently.

import { readFileSync } from "fs";

// 1. Read your local file into a native Blob
const buffer = readFileSync("./brochure.pdf");
const blob = new Blob([buffer], { type: "application/pdf" });

// 2. Upload to Meta
const { id } = await client.uploadMedia(blob, "application/pdf");
console.log(`Uploaded Media ID: ${id}`);

// 3. Send using the ID instead of a public URL
await client.sendDocument("919876543210", id, "brochure.pdf");

Mark as Read

You can programmatically mark a received message as read (this shows the blue double ticks to the user).

// Pass the WhatsApp Message ID (wamid) received from the webhook
await client.markAsRead("wamid.xxx...");