πŸ”Œ Client

πŸ”Œ Client#

Note

WORK IN PROGRESS

The WhatsApp client has two responsibilities. Sending messages & handling updates.

class pywa.client.WhatsApp#
__init__(phone_id: str | int, token: str, base_url: str = 'https://graph.facebook.com', api_version: str | int | float | Literal[utils.Version.GRAPH_API] = Version.GRAPH_API, session: requests.Session | None = None, server: Flask | FastAPI | None = None, webhook_endpoint: str = '/', verify_token: str | None = None, filter_updates: bool = True, business_account_id: str | int | None = None, callback_url: str | None = None, fields: Iterable[str] | None = None, app_id: int | None = None, app_secret: str | None = None, verify_timeout: int | None = None, business_private_key: str | None = None, business_private_key_password: str | None = None, flows_request_decryptor: utils.FlowRequestDecryptor | None = <function default_flow_request_decryptor>, flows_response_encryptor: utils.FlowResponseEncryptor | None = <function default_flow_response_encryptor>) None#
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,
...     callback_url='https://6b3e.ngrok.io',
...     verify_token="XYZ123",
...     app_id=1234567890,
...     app_secret="my_app_secret",
... )
>>> @wa.on_message()
... def message_handler(_: WhatsApp, msg: Message): print(msg)
>>> flask_app.run(port=8000)
Parameters:
  • phone_id – The Phone number ID (Not the phone number itself, the ID can be found in the App dashboard).

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

  • base_url – The base URL of the WhatsApp API (Do not change unless you know what you’re doing).

  • api_version – The API version of the WhatsApp Cloud API (default to the latest version).

  • session – The session to use for requests (default: new requests.Session(), 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.

  • callback_url – The callback URL to register (optional, only if you want pywa to register the callback URL for you).

  • verify_token – The verify token of the registered callback_url (Required when server is provided. The verify token can be any string. It is used to challenge the webhook endpoint to verify that the endpoint is valid).

  • verify_timeout – The timeout (in seconds) 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).

  • 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).

  • 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, required when registering a callback_url).

  • webhook_endpoint – The endpoint to listen for incoming messages (if you’re using the server for another purpose, or for multiple WhatsApp clients, 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 that owns the phone ID (optional, required for some API methods).

  • business_private_key – The global private key to use in the flows_request_decryptor

  • business_private_key_password – The global private key password (if needed) to use in the flows_request_decryptor

  • flows_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.

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()

Templates

create_template()

Flows

create_flow(), update_flow_metadata(), update_flow_json(), publish_flow(), delete_flow(), deprecate_flow(), get_flow(), get_flows(), get_flow_assets()

Business profile

get_business_profile(), get_business_phone_number(), update_business_profile() update_conversational_automation() set_business_public_key()

Commerce

get_commerce_settings(), update_commerce_settings()