Upload and download WhatsApp media with the Cloud API in Node.js
Meta’s media reference covers four operations: upload (POST /<PHONE_NUMBER_ID>/media), get a URL (GET /<MEDIA_ID>), download (GET <media URL>), and delete (DELETE /<MEDIA_ID>).
What the Cloud API does
Section titled “What the Cloud API does”- An upload is multipart form data with
messaging_product=whatsapp, thefileand itstype. It returns a mediaid. - Uploaded files persist for 30 days unless you delete them earlier. A media ID returned by the API expires after 30 days. A media ID received in a webhook expires after 7 days.
- A media URL expires after 5 minutes. Query the ID again for a fresh one.
- Downloading requires your access token in the
Authorizationheader, and the request fails without it. If a download returns404, request a new URL and try again. - Meta’s size limits per type: audio and video 16 MB, images 5 MB (JPEG, PNG, static WebP), documents 100 MB, static stickers 100 KB, animated stickers 500 KB. The supported types table lists the accepted MIME types.
- A mismatched MIME type is a common cause of error
131053(error codes).
Upload and send
Section titled “Upload and send”// Raw bytes carry no content type, so pass one. A Blob or File with a type works as is.const bytes = await readFile('./invoice.pdf');const { id } = await client.media.upload({ file: bytes, type: 'application/pdf', filename: 'invoice.pdf',});
// Send it by media ID.await client.messages.sendDocument('15551234567', { id, filename: 'invoice.pdf' });upload accepts a Blob or File, or an object { file, type, filename } where file is a Blob, Uint8Array or ArrayBuffer. A content type is required: from the Blob’s type or the explicit type, otherwise it throws WhatsAppValidationError. A phoneNumberId must be configured on the client or passed per call.
Uploads are sent as one attempt and are not retried, because a multipart body is not safely replayable. Retry an upload yourself if it fails with a network error or a 5xx response.
Download received media
Section titled “Download received media”// For a media ID taken from an inbound webhook message:const received = await client.media.download('1037543291543636');await writeFile('./received-file', received.data); // a Uint8Arrayconsole.log(received.mimeType, received.sha256);download looks up the URL and then fetches the bytes with your bearer token in one call, so the 5-minute URL lifetime is not an issue. It returns the bytes as a Uint8Array with the mimeType and, when Meta supplies one, the sha256. To fetch a URL you already have, use client.media.downloadByUrl(url). Downloads follow the normal retry policy.
Look up and delete
Section titled “Look up and delete”const info = await client.media.getUrl(id); // url, mimeType, sha256, fileSizeconsole.log(info.url, info.fileSize);
await client.media.delete(id); // { success: true }For sending media by link or by ID in messages, see Messaging and Media. Meta recommends media IDs over URLs for high throughput.
Independent open-source project. Not affiliated with, endorsed by or sponsored by Meta or WhatsApp. Released under the MIT licence. Source on GitHub.