> For the complete documentation index, see [llms.txt](https://reno.gitbook.io/smtp-py/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://reno.gitbook.io/smtp-py/getting-started/usage.md).

# Usage

This page gives a brief introduction to the library. It assumes you have the library installed, if you don’t check the Installing portion.

### Mục tiêu

Tạo request đầu tiên với `SMTPDevClient`.

### Mục lục nhanh

* [Khởi tạo client](#khoi-tao-client)
* [Gọi API đầu tiên](#goi-api-dau-tien)
* [Gợi ý cấu hình nâng cao](#goi-y-cau-hinh-nang-cao)

### Khởi tạo client

{% code title="client.py" %}

```python
from smtppy import SMTPDevClient

client = SMTPDevClient(api_key="YOUR_API_KEY")
```

{% endcode %}

{% hint style="info" %}
Nếu bạn đã set `XAPIKEY` / `X_API_KEY` trong môi trường hoặc `.env` thì có thể bỏ `api_key`.
{% endhint %}

> Pro tip: local dev dùng `.env`. Production/CI nên set biến môi trường trực tiếp.

### Gọi API đầu tiên

{% code title="first\_request.py" %}

```python
from smtppy import SMTPDevClient

client = SMTPDevClient(api_key="YOUR_API_KEY")

domains = client.list_domains(is_active=True, page=1)
print(domains)
```

{% endcode %}

### Gợi ý cấu hình nâng cao

Xem trang [Client setup](/smtp-py/getting-started/client-setup.md) để:

* cấu hình timeout / retry
* xử lý exception (`SMTPDevAPIError`, `SMTPDevAuthError`)
* dùng context manager (`with SMTPDevClient(...) as client:`)

<details>

<summary>Migration từ API kiểu cũ (legacy)</summary>

Nếu bạn đang dùng kiểu gọi hàm dạng `smtppy.<function>()`, hãy chuyển sang `SMTPDevClient`:

| Legacy                     | Mới                          |
| -------------------------- | ---------------------------- |
| `smtppy.setapikey(key)`    | `SMTPDevClient(api_key=key)` |
| `smtppy.listdomains(...)`  | `client.list_domains(...)`   |
| `smtppy.createdomain(...)` | `client.create_domain(...)`  |
| `smtppy.listaccounts(...)` | `client.list_accounts(...)`  |
| `smtppy.listmessages(...)` | `client.list_messages(...)`  |

</details>
