The Media's ID
Optional
phoneID: stringBusiness phone number ID. If included, the operation will only be processed if the ID matches the ID of the business phone number that the media was uploaded on.
The server response
Get a Media fetch from an url. When using this method, be sure to pass a trusted url, since the request will be authenticated with the token.
The Media's url
The fetch raw response
Get a Media object data with an ID
The Media's ID
Optional
phoneID: stringBusiness phone number ID. If included, the operation will only be processed if the ID matches the ID of the business phone number that the media was uploaded on.
The server response
Upload a Media to the API server
The bot's phone ID
The Media's FormData.
Must have a 'file' property with the file to upload as a blob and a valid mime-type in the 'type' field of the blob.
Example for Node ^18: new FormData().set("file", new Blob([stringOrFileBuffer], "image/png"));
.
Previous versions of Node will need an external FormData, such as undici's.
To use non spec complaints versions of FormData (eg: form-data) or Blob set the 'check' parameter to false.
Wether the FormData should be checked before uploading. The FormData must have the method .get("name") to work with the checks. If it doesn't (for example, using the module "form-data"), set this to false.
The server response
// author ekoeryanto on issue #322
import { WhatsAppAPI } from "whatsapp-api-js";
const token = "token";
const appSecret = "appSecret";
const Whatsapp = new WhatsAppAPI({ token, appSecret });
const url = "https://example.com/image.png";
const image = await fetch(url);
const blob = await image.blob();
// If required:
// import FormData from "undici";
const form = new FormData();
form.set("file", blob);
console.log(await Whatsapp.uploadMedia("phoneID", form));
// Expected output: { id: "mediaID" }
import { WhatsAppAPI } from "whatsapp-api-js";
const token = "token";
const appSecret = "appSecret";
const Whatsapp = new WhatsAppAPI({ token, appSecret });
// If required:
// import FormData from "undici";
// import { Blob } from "node:buffer";
const form = new FormData();
// If you don't mind reading the whole file into memory:
form.set("file", new Blob([fs.readFileSync("image.png")], "image/png"));
// If you do, you will need to use streams. The module "form-data",
// although not spec compliant (hence needing to set check to false),
// has an easy way to do this:
// form.append("file", fs.createReadStream("image.png"), { contentType: "image/png" });
console.log(await Whatsapp.uploadMedia("phoneID", form));
// Expected output: { id: "mediaID" }
Delete a Media object with an ID