whatsapp-api-js - v6.1.1
    Preparing search index...

    Interface API

    interface API {
        deleteMedia(
            id: string,
            phoneID?: string,
        ): Promise<ServerMediaDeleteResponse>;
        fetchMedia(url: string): Promise<Response>;
        retrieveMedia(
            id: string,
            phoneID?: string,
        ): Promise<ServerMediaRetrieveResponse>;
        uploadMedia(
            phoneID: string,
            form: unknown,
            check: boolean,
        ): Promise<ServerMediaUploadResponse>;
    }

    Implemented by

    Index

    Methods

    • Delete a Media object with an ID

      Parameters

      • id: string

        The Media's ID

      • OptionalphoneID: string

        Business 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.

      Returns Promise<ServerMediaDeleteResponse>

      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.

      Parameters

      • url: string

        The Media's url

      Returns Promise<Response>

      The fetch raw response

      import { WhatsAppAPI } from "whatsapp-api-js";

      const token = "token";
      const appSecret = "appSecret";

      const Whatsapp = new WhatsAppAPI({ token, appSecret });

      const id = "mediaID";
      const { url } = await Whatsapp.retrieveMedia(id);
      const response = Whatsapp.fetchMedia(url);

      If url is not a valid url

    • Get a Media object data with an ID

      Parameters

      • id: string

        The Media's ID

      • OptionalphoneID: string

        Business 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.

      Returns Promise<ServerMediaRetrieveResponse>

      The server response

    • Upload a Media to the API server

      Parameters

      • phoneID: string

        The bot's phone ID

      • form: unknown

        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.

      • check: boolean

        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.

      Returns Promise<ServerMediaUploadResponse>

      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" }

      If check is set to true and form is not a FormData

      If check is set to true and the form doesn't have valid required properties (file, type)

      If check is set to true and the form file is too big for the file type