Metadata-Version: 2.5
Name: airboxhq
Version: 0.3.0
Summary: Python SDK for the Airbox API: WhatsApp and Instagram messages through one REST API.
Project-URL: Source, https://github.com/airboxhq/airbox-python
License-Expression: MIT
License-File: LICENSE
Keywords: airbox,api,instagram,messaging,sdk,whatsapp
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Communications :: Chat
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# Airbox Python SDK

WhatsApp and Instagram messages through one REST API, from Python.

Every call of the API, grouped by resource: messages, templates, media, contacts, channels and test.

## Install

```bash
pip install airboxhq
```

Python 3.9 or newer. No dependencies.

## Make the client

```python
import os

from airboxhq import Airbox

client = Airbox(api_key=os.environ["AIRBOX_API_KEY"])
```

The key falls back to `AIRBOX_API_KEY` when you leave it out. A test key sends to the simulator, so you can try every call before connecting a number.

## Messages

```python
data = client.messages.send_text(
    to="+77011234567",
    body="Your order is ready",
    idempotency_key="order-1042",
)

print(data)
```

`channel` defaults to `"auto"`, which replies on the channel the customer last wrote on. The same `idempotency_key` never sends twice.

Images, video, audio and voice take a `url` or the `media_id` of an upload. Images and video take a `caption` too.

```python
client.messages.send_image(to="+77011234567", url="https://example.com/receipt.jpg", caption="Your receipt")
client.messages.send_video(to="+77011234567", media_id="med_123")
client.messages.send_audio(to="+77011234567", url="https://example.com/song.mp3")
client.messages.send_voice(to="+77011234567", media_id="med_456")
```

`messages.send(**params)` still takes the body of `POST /v1/messages` as it is. Read a message back with its status:

```python
message = client.messages.get(data["id"])
print(message["status"])
```

## Templates

A template is how you write first, outside the 24 hour window.

```python
templates = client.templates.list()

client.templates.send(
    "order_ready",
    to="+77011234567",
    language="ru",
    parameters=["1042"],
)
```

`parameters` is a list for positional placeholders or a dict for named ones. `channel` defaults to `"whatsapp"`.

## Media

```python
uploaded = client.media.upload("receipt.pdf")
client.messages.send_image(to="+77011234567", media_id=uploaded["id"])

data = client.media.download("med_123")
with open("photo.jpg", "wb") as file:
    file.write(data)
```

`upload` takes bytes, a path or a binary file object. Pass `filename` and `content_type` when they cannot be guessed. `download` returns the raw bytes.

## Contacts

```python
page = client.contacts.list(phone="+77011234567")
print(page["data"], page["has_more"])

contact = client.contacts.get(page["data"][0]["id"])

conversation = client.contacts.list_messages(contact["id"], limit=50)
print([message["direction"] for message in conversation["data"]])

client.contacts.grant_consent("+77011234567")
client.contacts.withdraw_consent("+77011234567")
```

`list` and `list_messages` return a page, `{"data": [...], "has_more": ...}`, newest first for messages. Pass the `id` of the last item as `after` to get the next page, and `limit` (1 to 100) to size it. Params you leave out are not sent.

## Channels

```python
for channel in client.channels.list():
    print(channel)
```

## Test

Simulate a customer writing to you. Only a test key may call it, otherwise the API answers `test_key_required`.

```python
client.test.inbound(from_="+77011234567", text={"body": "Hello"}, name="Aida")
```

## With asyncio

```python
import asyncio
import os

from airboxhq import AsyncAirbox


async def main():
    async with AsyncAirbox(api_key=os.environ["AIRBOX_API_KEY"]) as client:
        data = await client.messages.send_text(to="+77011234567", body="Hi")
        print(data)


asyncio.run(main())
```

Every resource and call is here, awaited: `await client.templates.list()`.

## Errors

A refused request raises `airboxhq.AirboxError` with the API's own code and the request id:

```python
from airboxhq import AirboxError

try:
    client.messages.send_text(to="+77011234567", body="Hi")
except AirboxError as error:
    print(error.code, error.request_id)
```

## License

MIT
