Metadata-Version: 2.5
Name: baatzo
Version: 0.1.1
Summary: Send WhatsApp templates with the Baatzo API, read their status and verify webhooks.
Project-URL: Homepage, https://baatzo.com/developers
Project-URL: Documentation, https://api.baatzo.com/docs
Project-URL: Changelog, https://api.baatzo.com/docs/changelog.html
Author: Aashita Technosoft Private Limited
License-Expression: MIT
License-File: LICENSE
Keywords: baatzo,otp,whatsapp,whatsapp api,whatsapp business
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# baatzo

Send approved Utility and Authentication WhatsApp templates with the [Baatzo API](https://api.baatzo.com/docs), read their status and verify status webhooks. Python 3.10 or later, no dependencies, typed.

```sh
pip install baatzo
```

Make a key in the Baatzo web app under Settings, Developers. Start with a sandbox key (`bz_test_`): it is free on every plan and nothing reaches WhatsApp. Keep keys on your server.

## Send

```python
from baatzo import Baatzo

baatzo = Baatzo()  # reads BAATZO_API_KEY

sent = baatzo.messages.send(
    to="+919829012345",
    template={"name": "login_code", "language": "en"},
    variables={"body": {"code": "482913"}},
    reference="login-88213",
    idempotency_key="login-88213-1",
)
# {"id": "0193…", "status": "accepted", …}
```

Give your own `idempotency_key` when your code may run the same send again, after a crash for example: the same key never sends twice in 24 hours. Without one, each call makes a new key. The sending number's phone number ID, needed only with more than one number, is `from_`.

## Read

```python
message = baatzo.messages.get(sent["id"])
print(message["status"], message["charge"])
```

## Webhooks

Check the raw body, before any JSON parsing.

```python
from baatzo import BaatzoSignatureError, verify_webhook

@app.post("/baatzo-webhook")  # Flask
def baatzo_webhook():
    try:
        event = verify_webhook(
            request.get_data(), request.headers.get("Baatzo-Signature"), os.environ["BAATZO_WEBHOOK_SECRET"]
        )
    except BaatzoSignatureError:
        return "", 400
    # Drop a repeat by event["id"]; ignore a lower event["data"]["status_rank"] than you have.
    return "", 200  # answer within 5 seconds, then do the work
```

`verify_signature(raw_body, header, secret)` answers True or False instead.

## Errors and retries

- `BaatzoError`: Baatzo refused. It has `status`, `code` ([the list](https://api.baatzo.com/docs#errors)), `details`, `request_id` and `retry_after`.
- `BaatzoConnectionError`: no answer came. The message may have been sent: try again with its `idempotency_key`.
- `BaatzoSignatureError`: a webhook that does not verify.

The client tries again, twice by default and always with the same `Idempotency-Key`, after a 429, a 5xx, `request_in_progress` and a connection failure. It does not try `retry_later` again: send a fresh code.

A send can succeed as a request and still be refused by WhatsApp: check `status` for `failed` and `withheld`.

Options: `Baatzo(api_key, base_url=..., timeout=30, max_retries=2)`.

## Licence

MIT. By Aashita Technosoft Private Limited.
