Biblioteca Python para integração com a API Oficial do WhatsApp (Cloud API).
Project description
WhatsApp Cloud API Client (Python)
Biblioteca Python para integracao com a API Oficial do WhatsApp (Cloud API), com foco em simplicidade de uso em apps.
Instalacao
pip install whatsapp-cloud-api-client
Para desenvolvimento local:
pip install -e ".[dev]"
Uso rapido (sync)
from whatsapp_cloud_api import WhatsAppClient
client = WhatsAppClient(
access_token="SEU_TOKEN",
phone_number_id="SEU_PHONE_NUMBER_ID",
)
resp = client.send_text(
to="5511999999999",
body="Ola! Mensagem enviada via Cloud API.",
)
print(resp.model_dump())
Uso rapido (async)
import asyncio
from whatsapp_cloud_api import AsyncWhatsAppClient
async def main() -> None:
async with AsyncWhatsAppClient(
access_token="SEU_TOKEN",
phone_number_id="SEU_PHONE_NUMBER_ID",
) as client:
resp = await client.send_text(
to="5511999999999",
body="Mensagem async",
)
print(resp.model_dump())
asyncio.run(main())
Modelos tipados (Pydantic)
Os metodos retornam modelos Pydantic:
SendMessageResponseMediaUploadResponseMediaInfoResponseMarkAsReadResponse
Retry, backoff e rate limit
O cliente possui retry configuravel para erros transientes (429, 500, 502, 503, 504).
Por padrao, retry roda apenas para GET para evitar duplicidade em envio de mensagem (POST).
from whatsapp_cloud_api import WhatsAppClient
client = WhatsAppClient(
access_token="SEU_TOKEN",
phone_number_id="SEU_PHONE_NUMBER_ID",
max_retries=3,
backoff_factor=0.5,
max_backoff=8.0,
retry_methods={"GET", "POST"}, # habilite POST se quiser retry em envio
)
Se a API retornar Retry-After, esse valor sera respeitado.
Funcionalidades implementadas
- Envio de mensagem de texto
- Envio de mensagem template
- Envio de midia por
media_idoulink(imagem, documento, video, audio, sticker) - Upload de midia
- Marcar mensagem como lida
- Busca de informacoes de midia
- Validacao de assinatura de webhook (
X-Hub-Signature-256) - Cliente sincrono e assincrono
Exemplo com Flask (webhook)
from flask import Flask, request, jsonify
from whatsapp_cloud_api import verify_webhook_signature, verify_webhook_challenge
app = Flask(__name__)
APP_SECRET = "SEU_APP_SECRET"
VERIFY_TOKEN = "SEU_VERIFY_TOKEN"
@app.get("/webhook")
def webhook_verify():
ok, challenge = verify_webhook_challenge(
mode=request.args.get("hub.mode"),
token=request.args.get("hub.verify_token"),
challenge=request.args.get("hub.challenge"),
verify_token=VERIFY_TOKEN,
)
if not ok:
return "forbidden", 403
return challenge, 200
@app.post("/webhook")
def webhook_receive():
if not verify_webhook_signature(
app_secret=APP_SECRET,
raw_body=request.get_data(),
x_hub_signature_256=request.headers.get("X-Hub-Signature-256", ""),
):
return "invalid signature", 401
data = request.get_json(silent=True) or {}
return jsonify({"ok": True, "received": bool(data)}), 200
Rodar testes
pytest
CI e publicacao
- CI:
.github/workflows/ci.yml - Criacao de tag/release:
.github/workflows/release.yml - Publicacao PyPI:
.github/workflows/publish.yml
Fluxo recomendado:
- Execute o workflow
Create Releasee informe a tag (ex:v0.2.0). - O release publicado dispara
Publish to PyPI. - Configure Trusted Publisher no PyPI para este repositorio (OIDC), sem token manual.
Opcional: se preferir token, adapte o workflow para usar PYPI_API_TOKEN.
Licenca
MIT
Documentacao completa
- Guia de uso:
docs/GUIA_DE_USO.md
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file whatsapp_cloud_api_client-0.2.1.tar.gz.
File metadata
- Download URL: whatsapp_cloud_api_client-0.2.1.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30e11c50f51f2bdd9aadc04791ed31657cad744cc4c4eb6a6a1070b35635f71e
|
|
| MD5 |
245c3e71a86d99dccc4be6dc66b89f70
|
|
| BLAKE2b-256 |
225ae6aee5409faaecfeb86139a4edb30f0291517eb74b852d2bdd2981bbd2b0
|
Provenance
The following attestation bundles were made for whatsapp_cloud_api_client-0.2.1.tar.gz:
Publisher:
publish.yml on cfartes/api_whatsapp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
whatsapp_cloud_api_client-0.2.1.tar.gz -
Subject digest:
30e11c50f51f2bdd9aadc04791ed31657cad744cc4c4eb6a6a1070b35635f71e - Sigstore transparency entry: 1003447065
- Sigstore integration time:
-
Permalink:
cfartes/api_whatsapp@cf2e2b8e506fe87e5a85656650682f663d3659de -
Branch / Tag:
refs/heads/main - Owner: https://github.com/cfartes
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@cf2e2b8e506fe87e5a85656650682f663d3659de -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file whatsapp_cloud_api_client-0.2.1-py3-none-any.whl.
File metadata
- Download URL: whatsapp_cloud_api_client-0.2.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
feb152cc079ca0bfb26754afd0a16863b31cf527878066d4af2c432abec9a275
|
|
| MD5 |
75b5a6194e1db92b8fd2f2f8a8b4a2eb
|
|
| BLAKE2b-256 |
b930dd8f77428b4b4eab7ce4fe6ff87db02b4b7504e2a1cd2e52e77c16a44ec2
|
Provenance
The following attestation bundles were made for whatsapp_cloud_api_client-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on cfartes/api_whatsapp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
whatsapp_cloud_api_client-0.2.1-py3-none-any.whl -
Subject digest:
feb152cc079ca0bfb26754afd0a16863b31cf527878066d4af2c432abec9a275 - Sigstore transparency entry: 1003447067
- Sigstore integration time:
-
Permalink:
cfartes/api_whatsapp@cf2e2b8e506fe87e5a85656650682f663d3659de -
Branch / Tag:
refs/heads/main - Owner: https://github.com/cfartes
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@cf2e2b8e506fe87e5a85656650682f663d3659de -
Trigger Event:
workflow_dispatch
-
Statement type: