🔌 Client

🔌 Client#

Note

WORK IN PROGRESS

The WhatsApp client has two responsibilities. Sending messages & handling updates. You can use both or only one of them.

class pywa.client.WhatsApp#
__init__(phone_id: str | int, token: str, base_url: str = 'https://graph.facebook.com', api_version: float | int = 18.0, session: Session | None = None, server: FlaskApp | FastAPIApp | None = None, webhook_endpoint: str = '/', verify_token: str | None = None, filter_updates: bool = True, business_account_id: str | int | None = None) None#
Initialize the WhatsApp client.

Example without webhook:

>>> from pywa import WhatsApp
>>> wa = WhatsApp(phone_id="100944",token="EAADKQl9oJxx")

Example with webhook (using Flask):

>>> from pywa import WhatsApp
>>> from flask import Flask
>>> flask_app = Flask(__name__)
>>> wa = WhatsApp(
...     phone_id="100944",
...     token="EAADKQl9oJxx",
...     server=flask_app,
...     verify_token="my_verify_token",
... )
>>> @wa.on_message()
... def message_handler(_: WhatsApp, msg: Message): print(msg)
>>> flask_app.run()  # or by using a WSGI server (e.g. gunicorn, waitress, etc.)
Parameters:
  • phone_id – The Phone number ID (Not the phone number itself, the ID can be found in the App settings).

  • token – The token of the WhatsApp account (In production, you should use permanent token).

  • base_url – The base URL of the WhatsApp API (default: https://graph.facebook.com).

  • api_version – The API version of the WhatsApp API (default: 18.0).

  • session – The session to use for requests (default: new requests.Session(), Do not use the same session across multiple WhatsApp clients!)

  • server – The Flask or FastAPI app instance to use for the webhook.

  • webhook_endpoint – The endpoint to listen for incoming messages (default: /).

  • verify_token – The verify token of the registered webhook (Required when server is provided).

  • filter_updates – Whether to filter out updates that not sent to this phone number (default: True, does not apply to raw updates).

  • business_account_id – The business account ID of the WhatsApp account (optional, required for some API methods).

The available methods are:

Category

Methods

Sending messages

send_message(), send_image(), send_video(), send_audio(), send_document(), send_location(), send_contact(), send_sticker(), send_template(), send_catalog(), send_product(), send_products(), send_reaction(), remove_reaction(), mark_message_as_read()

Media

upload_media(), download_media(), get_media_url()

Business profile

create_template(), get_business_profile(), update_business_profile()

Commerce

get_commerce_settings(), update_commerce_settings()