|
| 1 | +# WaAPI Python SDK |
| 2 | + |
| 3 | +Official Python client for the [WaAPI](https://waapi.app) REST API — send and |
| 4 | +receive WhatsApp messages, manage chats, groups and channels from Python. |
| 5 | + |
| 6 | +[](https://pypi.org/project/waapi/) |
| 7 | +[](https://pypi.org/project/waapi/) |
| 8 | +[](LICENSE) |
| 9 | + |
| 10 | +```bash |
| 11 | +pip install waapi |
| 12 | +``` |
| 13 | + |
| 14 | +## Quick start |
| 15 | + |
| 16 | +```python |
| 17 | +from waapi import WaAPI |
| 18 | + |
| 19 | +client = WaAPI(token="YOUR_API_TOKEN", instance_id=123) |
| 20 | + |
| 21 | +client.send_message( |
| 22 | + chat_id="4915112345678@c.us", |
| 23 | + message="Deployment finished.", |
| 24 | +) |
| 25 | +``` |
| 26 | + |
| 27 | +Get a token at [waapi.app/user/api-tokens](https://waapi.app/user/api-tokens) |
| 28 | +and create an instance connected to your number. |
| 29 | + |
| 30 | +### The chat ID is the one thing to get right |
| 31 | + |
| 32 | +Its suffix decides where the message lands, and a wrong suffix is accepted and |
| 33 | +delivers nothing: |
| 34 | + |
| 35 | +| Target | Format | |
| 36 | +|---|---| |
| 37 | +| One person | `4915112345678@c.us` | |
| 38 | +| Group | `123456789-123456789@g.us` | |
| 39 | +| Channel | `123456789@newsletter` | |
| 40 | + |
| 41 | +## Async |
| 42 | + |
| 43 | +Same method names, awaited: |
| 44 | + |
| 45 | +```python |
| 46 | +from waapi import AsyncWaAPI |
| 47 | + |
| 48 | +async with AsyncWaAPI(token="YOUR_API_TOKEN", instance_id=123) as client: |
| 49 | + await client.send_message(chat_id="4915112345678@c.us", message="Hi") |
| 50 | +``` |
| 51 | + |
| 52 | +## Errors |
| 53 | + |
| 54 | +A successful HTTP exchange is not proof the message was sent. The API answers |
| 55 | +`200` with `{"status": "error"}` when, for example, the instance is not |
| 56 | +connected — so the SDK raises on that too, rather than handing back a body that |
| 57 | +looks like success. |
| 58 | + |
| 59 | +```python |
| 60 | +from waapi import WaAPI, FailedActionError, AuthenticationError, RateLimitError |
| 61 | + |
| 62 | +try: |
| 63 | + client.send_message(chat_id="4915112345678@c.us", message="Hi") |
| 64 | +except AuthenticationError: |
| 65 | + ... # token wrong, expired, or missing scopes |
| 66 | +except RateLimitError as e: |
| 67 | + time.sleep(e.retry_after or 5) |
| 68 | +except FailedActionError as e: |
| 69 | + ... # accepted but not carried out — e.response has the detail |
| 70 | +``` |
| 71 | + |
| 72 | +| Exception | Raised on | |
| 73 | +|---|---| |
| 74 | +| `AuthenticationError` | HTTP 401, 403 | |
| 75 | +| `NotFoundError` | HTTP 404 | |
| 76 | +| `ValidationError` | HTTP 422 — `.errors` holds the field errors | |
| 77 | +| `RateLimitError` | HTTP 429 — `.retry_after` in seconds when the API sends it | |
| 78 | +| `FailedActionError` | HTTP 400, **and HTTP 200 with `status: error`** | |
| 79 | +| `ServerError` | HTTP 5xx | |
| 80 | + |
| 81 | +All inherit from `WaAPIError`. |
| 82 | + |
| 83 | +## Endpoints not wrapped yet |
| 84 | + |
| 85 | +The API exposes 122 client actions. This release wraps the common ones; every |
| 86 | +other action is reachable by name: |
| 87 | + |
| 88 | +```python |
| 89 | +client.action("send-seen", {"chatId": "4915112345678@c.us"}) |
| 90 | +client.action("create-group", {"title": "Ops", "participants": ["4915112345678@c.us"]}) |
| 91 | +``` |
| 92 | + |
| 93 | +The remaining wrappers are generated from the OpenAPI specification rather than |
| 94 | +written by hand — see [CONTRIBUTING.md](CONTRIBUTING.md). |
| 95 | + |
| 96 | +## Configuration |
| 97 | + |
| 98 | +```python |
| 99 | +WaAPI( |
| 100 | + token="...", # required |
| 101 | + instance_id=123, # optional; per-call instance_id overrides it |
| 102 | + base_url="https://waapi.app/api/v1", |
| 103 | + timeout=30.0, |
| 104 | +) |
| 105 | +``` |
| 106 | + |
| 107 | +Passing `instance_id` to the client keeps single-instance code short. Any call |
| 108 | +can still override it, and a call with neither raises before a request is sent. |
| 109 | + |
| 110 | +## Development |
| 111 | + |
| 112 | +```bash |
| 113 | +python -m venv .venv && source .venv/bin/activate |
| 114 | +pip install -e ".[dev]" |
| 115 | +pytest |
| 116 | +``` |
| 117 | + |
| 118 | +The suite runs entirely against `httpx.MockTransport` — no network, no token, |
| 119 | +no connected account. |
| 120 | + |
| 121 | +## License |
| 122 | + |
| 123 | +MIT. Not affiliated with, endorsed or sponsored by WhatsApp LLC or Meta. |
| 124 | +WhatsApp is a trademark of WhatsApp LLC. |
0 commit comments