🔌 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.
Full documentation on pywa.readthedocs.io.
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
serveris 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 |
|
Media |
|
Business profile |
|
Commerce |
- Client Reference
WhatsApp.send_message()WhatsApp.send_image()WhatsApp.send_video()WhatsApp.send_audio()WhatsApp.send_document()WhatsApp.send_location()WhatsApp.send_contact()WhatsApp.send_sticker()WhatsApp.send_catalog()WhatsApp.send_template()WhatsApp.send_product()WhatsApp.send_products()WhatsApp.send_reaction()WhatsApp.remove_reaction()WhatsApp.mark_message_as_read()WhatsApp.upload_media()WhatsApp.download_media()WhatsApp.get_media_url()WhatsApp.get_business_profile()WhatsApp.update_business_profile()WhatsApp.get_commerce_settings()WhatsApp.update_commerce_settings()WhatsApp.create_template()