π Client#
The WhatsApp client is the heart of the pywa library.
It is responsible for managing communication with the WhatsApp Business API.
Its three main responsibilities are:
Sending messages β text, media, location, contacts, etc.
Listening β handling incoming messages, events, and statuses.
Managing resources β templates, flows, profiles, and other business-related settings.
Tip
Pywa provides two types of clients:
Synchronous (pywa)
Asynchronous (pywa_async)
Choose the one that best fits your applicationβs needs.
from pywa import WhatsApp, types
wa = WhatsApp(...)
@wa.on_message
def on_message(_: WhatsApp, msg: types.Message):
msg.reply("Hello!")
from pywa_async import WhatsApp, types
wa = WhatsApp(...)
@wa.on_message
async def on_message(_: WhatsApp, msg: types.Message):
await msg.reply("Hello!")
For optimal type checking, ensure that all your imports come from the same packageβeither pywa or pywa_async.
- class pywa.client.WhatsApp#
- __init__(phone_id: str | int | None = None, token: str = None, *, session: ~httpx.Client | None = None, server: ~pywa.utils.Flask | ~pywa.utils.FastAPI | None = <object object>, webhook_endpoint: str = '/', verify_token: str | None = None, filter_updates: bool = True, continue_handling: bool = False, skip_duplicate_updates: bool = True, validate_updates: bool = True, business_account_id: str | int | None = None, callback_url: str | None = None, callback_url_scope: ~pywa.utils.CallbackURLScope = CallbackURLScope.APP, webhook_fields: ~typing.Iterable[str] | None = None, app_id: int | str | None = None, app_secret: str | None = None, webhook_challenge_delay: int = 3, business_private_key: str | None = None, business_private_key_password: str | None = None, flows_request_decryptor: ~typing.Callable[[str, str, str, str, str | None], tuple[dict, bytes, bytes]] | None = <function default_flow_request_decryptor>, flows_response_encryptor: ~typing.Callable[[dict, bytes, bytes], str] | None = <function default_flow_response_encryptor>, api_version: str | int | float | ~typing.Literal[Version.GRAPH_API] = Version.GRAPH_API, handlers_modules: ~typing.Iterable[~types.ModuleType] | None = None) None#
The WhatsApp client.
Full documentation on pywa.readthedocs.io.
Example without webhook:
>>> from pywa import WhatsApp >>> wa = WhatsApp(phone_id="1234567890",token="EAADKQl9oJxx") >>> wa.send_message(to="1234567890", text="Hello from PyWa!")
Example with webhook (using FastAPI):
>>> from pywa import WhatsApp, types, filters >>> from fastapi import FastAPI >>> fastapi_app = FastAPI()
>>> wa = WhatsApp( ... phone_id="1234567890", ... token="EAADKQl9oJxx", ... server=fastapi_app, ... callback_url='https://pywa.ngrok.io', ... verify_token="XYZ123", ... app_id=1234567890, ... app_secret="my_app_secret", ... )
>>> @wa.on_message(filters.text) ... def new_message(_: WhatsApp, msg: types.Message): ... msg.reply("Hello from PyWa!")
$ fastapi dev wa.pysee uvicorn docs for more options (port,host, etc.)- Parameters:
phone_id β The Phone number ID to send messages from (if you manage multiple WhatsApp business accounts (e.g. Solution Partners, Tech Providers), you can specify the phone ID when sending messages when sending or when calling the API methods).
token β The token to use for WhatsApp Cloud API (In production, you should use permanent token).
api_version β The API version of the WhatsApp Cloud API (default to the latest version).
session β The session to use for api requests (default: new
httpx.Client(), For cases where you want to use a custom session, e.g. for proxy support. Do not use the same session across multiple WhatsApp clients!).server β The Flask or FastAPI app instance to use for the webhook. required when you want to handle incoming updates. pass None to insert the updates with the
webhook_update_handler().callback_url β The server URL to register (without endpoint. optional).
callback_url_scope β The scope of the callback URL to register (default:
APP).verify_token β A challenge string (Required when
serveris provided. The verify token can be any string. It is used to challenge the webhook endpoint to verify that the endpoint is valid).webhook_challenge_delay β The delay (in seconds, default to
3) to wait for the verify token to be sent to the server (optional, for cases where the server/network is slow or the server is taking a long time to start).webhook_fields β The fields to register for the callback URL (optional, if not provided, all supported fields will be registered. modify this if you want to reduce the number of unused requests to your server). See Availble fields.
app_id β The ID of the app in the App Basic Settings (optional, required when registering a
callback_url).app_secret β
The secret of the app in the App Basic Settings (optional, recomended for validating updates, required when registering a
callback_url).webhook_endpoint β The endpoint to listen for incoming messages (if youβre using the server for another purpose, you can change this to avoid conflicts).
filter_updates β Whether to filter out user updates that are not sent to this phone_id (default:
True, does not apply to raw updates or updates that are not user-related).business_account_id β The WhatsApp business account ID (WABA ID) that owns the phone ID (optional, required for some API methods).
business_private_key β The global private key to use in the
flows_request_decryptorbusiness_private_key_password β The global private key password (if needed) to use in the
flows_request_decryptorflows_request_decryptor β The global flows requests decryptor implementation to use to decrypt Flows requests.
flows_response_encryptor β The global flows response encryptor implementation to use to encrypt Flows responses.
continue_handling β Whether to continue handling updates after a handler or listener has been found (default:
False).skip_duplicate_updates β Whether to skip duplicate updates (default:
True).validate_updates β Whether to validate updates payloads (default:
True,app_secretrequired).handlers_modules β Modules to load handlers from.
Sending Messages#
The client allows you to send a wide variety of messages:
Method |
Description |
|---|---|
Send a text message |
|
Send an image |
|
Send a video |
|
Send an audio file |
|
Send a document |
|
Share a location |
|
Request location from a user |
|
Send one or multiple contacts |
|
Send a sticker |
|
Send a template message |
|
Send a product catalog |
|
Send a single product |
|
Send multiple products |
|
React to a message |
|
Remove a reaction |
|
Mark a message as read |
|
Indicate typing status to the user |
Handling Updates#
Register event handlers to listen for updates:
Method |
Description |
|---|---|
Handle incoming messages |
|
Handle callback button clicks |
|
Handle list or menu selections |
|
Track message delivery, read, and failure statuses |
|
Detect when a user opens a chat |
|
Handle incoming flow requests |
|
Handle flow completions |
|
Handle phone number changes |
|
Handle identity changes |
|
Handle incoming/outgoing call connections |
|
Handle call terminations |
|
Handle call status updates |
|
Handle call permission updates |
|
Handle user marketing preferences updates |
|
Handle template status updates |
|
Handle template category changes |
|
Handle template quality changes |
|
Handle template components updates |
|
Handle raw updates from WhatsApp |
|
Dynamically add handlers programmatically |
|
Remove handlers programmatically |
|
Remove handlers by callbacks |
|
Add flow request handlers programmatically |
|
Load handlers from external modules |
Listening#
You can listen for updates from specific users:
Method |
Description |
|---|---|
Listen to a specific user update |
|
Stop listening |
Media#
Manage media with ease:
Method |
Description |
|---|---|
Upload media to WhatsApp servers |
|
Download media |
|
Get direct media URL |
|
Delete media from WhatsApp servers |
Templates#
Create, update, and manage message templates:
Method |
Description |
|---|---|
Create a new template |
|
Bulk create or update authentication templates |
|
Retrieve all templates |
|
Get details of a specific template |
|
Update an existing template |
|
Delete a template |
|
Unpause a previously paused template |
|
Compare two templates |
|
Migrate templates between WABAs |
Flows#
Programmatically manage flows:
Method |
Description |
|---|---|
Create a flow |
|
Update flow metadata (name, categories, endpoint, etc.) |
|
Update flow JSON definition |
|
Publish a flow |
|
Delete a flow |
|
Deprecate a flow |
|
Get details of a flow |
|
List all flows |
|
Get flow performance metrics |
|
Get flow assets |
|
Migrate flows between WABAs |
Business#
Manage business account and profile:
Method |
Description |
|---|---|
Get business account details |
|
Get business profile |
|
Get all business phone numbers |
|
Get a specific business phone number |
|
Update profile details (name, description, picture, etc.) |
|
Update phone number display name |
|
Update commands and ice breakers |
|
Upload business public key |
|
Get phone number settings |
|
Update phone number settings |
|
Register a new phone number |
|
Deregister a phone number |
Managing Users#
Method |
Description |
|---|---|
Block users |
|
Unblock users |
|
Retrieve blocked users |
QR Codes#
Method |
Description |
|---|---|
Create a QR code |
|
Get details of a QR code |
|
List all QR codes |
|
Update a QR code |
|
Delete a QR code |
Commerce#
Method |
Description |
|---|---|
Get commerce settings |
|
Update commerce settings |
Calls#
Method |
Description |
|---|---|
Get call permissions |
|
Pre-accept a call |
|
Accept a call |
|
Reject a call |
|
Terminate a call |
Server#
Integrate with webhook events manually:
Method |
Description |
|---|---|
Handle webhook updates manually |
|
Handle webhook challenge manually |
|
Retrieve flow request handler |
Others#
Method |
Description |
|---|---|
Retrieve app access token |
|
Set app callback URL |
|
Override WABA callback URL |
|
Delete WABA callback URL |
|
Override phone callback URL |
|
Delete phone callback URL |
- Client Reference
WhatsApp.send_message()WhatsApp.send_image()WhatsApp.send_video()WhatsApp.send_audio()WhatsApp.send_document()WhatsApp.send_location()WhatsApp.request_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.indicate_typing()WhatsApp.listen()WhatsApp.stop_listening()WhatsApp.upload_media()WhatsApp.download_media()WhatsApp.get_media_url()WhatsApp.delete_media()WhatsApp.get_business_account()WhatsApp.get_business_profile()WhatsApp.get_business_phone_numbers()WhatsApp.get_business_phone_number()WhatsApp.get_business_phone_number_settings()WhatsApp.update_business_phone_number_settings()WhatsApp.update_business_profile()WhatsApp.update_display_name()WhatsApp.update_conversational_automation()WhatsApp.set_business_public_key()WhatsApp.get_commerce_settings()WhatsApp.update_commerce_settings()WhatsApp.create_template()WhatsApp.upsert_authentication_template()WhatsApp.update_template()WhatsApp.get_template()WhatsApp.get_templates()WhatsApp.delete_template()WhatsApp.unpause_template()WhatsApp.compare_templates()WhatsApp.migrate_templates()WhatsApp.create_flow()WhatsApp.update_flow_metadata()WhatsApp.update_flow_json()WhatsApp.publish_flow()WhatsApp.delete_flow()WhatsApp.deprecate_flow()WhatsApp.get_flow()WhatsApp.get_flows()WhatsApp.get_flow_metrics()WhatsApp.get_flow_assets()WhatsApp.migrate_flows()WhatsApp.block_users()WhatsApp.unblock_users()WhatsApp.get_blocked_users()WhatsApp.register_phone_number()WhatsApp.deregister_phone_number()WhatsApp.create_qr_code()WhatsApp.get_qr_code()WhatsApp.get_qr_codes()WhatsApp.update_qr_code()WhatsApp.delete_qr_code()WhatsApp.get_call_permissions()WhatsApp.pre_accept_call()WhatsApp.accept_call()WhatsApp.reject_call()WhatsApp.terminate_call()WhatsApp.get_app_access_token()WhatsApp.set_app_callback_url()WhatsApp.override_waba_callback_url()WhatsApp.delete_waba_callback_url()WhatsApp.override_phone_callback_url()WhatsApp.delete_phone_callback_url()WhatsApp.webhook_update_handler()WhatsApp.webhook_challenge_handler()WhatsApp.get_flow_request_handler()WhatsApp.load_handlers_modules()
- API Reference