# Usage

### 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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://reno.gitbook.io/smtp-py/getting-started/usage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
