Backend Boilerplates python Beginner

HTTPX Async HTTP Client Wrapper

Reusable async HTTP client with retry, timeout, and base URL configuration.

python
import httpx
from tenacity import retry, stop_after_attempt, wait_exponential

class ApiClient:
    def __init__(self, base_url: str, api_key: str):
        self._client = httpx.AsyncClient(
            base_url=base_url,
            headers={"Authorization": f"Bearer {api_key}"},
            timeout=10.0,
        )

    @retry(stop=stop_after_attempt(3), wait=wait_exponential(min=1, max=10))
    async def get(self, path: str, **kwargs):
        r = await self._client.get(path, **kwargs)
        r.raise_for_status()
        return r.json()

    async def close(self):
        await self._client.aclose()

Prerequisites

  • httpx >= 0.27
  • tenacity >= 8

Pro Tips

  • → Use as an async context manager for automatic cleanup.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
python
Level
Beginner
Read time
3 min
Published
Jun 2026
License
Commercial License

Newsletter

New modules every week, directly to your inbox.