Skip to main content

Overview

Monzoh provides both synchronous and asynchronous clients for the Monzo API. The AsyncMonzoClient offers native async/await support, while the synchronous MonzoClient can be integrated with async applications using thread execution patterns. This guide covers both approaches.

Why Async Integration?

Integrating Monzoh with async frameworks is useful for:
  • Web Applications: FastAPI, Starlette, and other async web frameworks
  • Concurrent Processing: Handle multiple API calls simultaneously
  • Non-blocking Operations: Keep your application responsive during API calls
  • Background Tasks: Process Monzo data without blocking the main thread

Native Async Client

The AsyncMonzoClient provides native async/await support:

Context Manager Usage

The async client supports async context managers for proper resource cleanup:

FastAPI Integration

Monzoh integrates seamlessly with FastAPI applications using the native async client:

Background Tasks with Celery

For background processing with Celery:

Custom Async Context Managers

You can create custom async context managers for additional functionality:

Concurrent API Calls

Process multiple accounts or operations concurrently using the native async client:

Error Handling in Async Context

Handle errors properly with the native async client:

Performance Considerations

Connection Pooling

The AsyncMonzoClient automatically handles connection pooling with proper resource management:

Best Practices

  1. Use AsyncMonzoClient for new projects - it provides native async/await support
  2. Use async context managers (async with) for proper resource cleanup
  3. Limit concurrency to avoid overwhelming the API (batch requests when needed)
  4. Handle errors gracefully with proper retry logic for transient failures
  5. Use connection pooling by passing a custom httpx.AsyncClient when needed
  6. Monitor performance and adjust concurrency levels based on your requirements

Migration from Sync to Async

To migrate from the synchronous client to the async client:
The async client provides all the same functionality as the sync client with native async/await support, better resource management, and improved performance for concurrent operations.