Modern Treasury Python Library
The official Python library for the Modern Treasury API, providing convenient access to their REST API from any Python 3.9+ application. It features comprehensive type definitions for request parameters and response fields, offering both synchronous and asynchronous clients powered by `httpx`. The library undergoes very frequent updates, often with multiple releases per week, reflecting ongoing API development and new features.
Warnings
- breaking A significant breaking change occurred around version v0.5.0, where argument passing to methods transitioned from a single `TypedDict` object to individual keyword arguments. Code written for older versions using `TypedDict` will break.
- gotcha While the library uses `httpx` and has a default request timeout of 1 minute (and retries certain errors), long-running operations or slow network conditions may benefit from explicit timeout configuration. If not explicitly set, requests could hang for the default duration.
- gotcha List methods in the Modern Treasury API are paginated. While the Python library provides auto-paginating iterators (e.g., `for item in client.resource.list():`), direct access to raw page data or granular control requires using methods like `.has_next_page()`, `.next_page_info()`, or `.get_next_page()`.
- breaking For users accessing raw response data (e.g., headers), the `LegacyAPIResponse` object is changing in the next major version. In the synchronous client, `content` and `text` will become methods instead of properties. In the asynchronous client, all methods will be asynchronous.
Install
-
pip install modern-treasury
Imports
- ModernTreasury
from modern_treasury import ModernTreasury
- AsyncModernTreasury
from modern_treasury import AsyncModernTreasury
Quickstart
import os
from modern_treasury import ModernTreasury
# It's highly recommended to use environment variables for credentials
# e.g., in a .env file: MODERN_TREASURY_API_KEY='your_api_key' MODERN_TREASURY_ORGANIZATION_ID='your_org_id'
client = ModernTreasury(
api_key=os.environ.get("MODERN_TREASURY_API_KEY", ""),
organization_id=os.environ.get("MODERN_TREASURY_ORGANIZATION_ID", ""),
)
try:
# Example: Create a counterparty
counterparty = client.counterparties.create(
name="My First Counterparty"
)
print(f"Successfully created counterparty with ID: {counterparty.id}")
# Example: List counterparties (handles pagination automatically)
print("\nListing all counterparties:")
for cp in client.counterparties.list():
print(f" - {cp.name} (ID: {cp.id})")
except Exception as e:
print(f"An error occurred: {e}")