Upload media once and reuse the returned media ID in messages. You can also query metadata and delete media.
- POST /{PHONE_NUMBER_ID}/media
- GET /{MEDIA_ID}
- DELETE /{MEDIA_ID}
- GET {MEDIA_URL}
- Uploads use
multipart/form-datawithfile,type, andmessaging_product. - Download uses the
urlfromgetMediaById. - Delete requires the media ID, not the URL.
import WhatsApp from 'meta-cloud-api';
const client = new WhatsApp({
accessToken: process.env.CLOUD_API_ACCESS_TOKEN!,
phoneNumberId: Number(process.env.WA_PHONE_NUMBER_ID),
businessAcctId: process.env.WA_BUSINESS_ACCOUNT_ID!,
});
const file = new File([Buffer.from('hello')], 'note.txt', { type: 'text/plain' });
const upload = await client.media.uploadMedia(file);
const mediaInfo = await client.media.getMediaById(upload.id);
const blob = await client.media.downloadMedia(mediaInfo.url);
await client.media.deleteMedia(upload.id);uploadMediaexpects aFilewith a MIME type; the responseidis used in messages.getMediaByIdreturns metadata and a temporaryurlfor downloads.downloadMediauses theurlfromgetMediaById, anddeleteMediaremoves the media by ID.