A comprehensive Python client for the Mercury Bank API
Project description
Mercury Bank API Python Client
A comprehensive Python client library for interacting with the Mercury Bank API. This library provides easy access to account information, transaction data, and other Mercury banking services.
Features
- Complete API Coverage: Full support for Mercury Bank's transaction and account endpoints
- Type Safety: Comprehensive type hints and data models for all API responses
- Error Handling: Proper exception handling with custom error types
- Robust Parsing: Built-in parsing for complex transaction details and routing information
- Easy to Use: Simple, intuitive interface with sensible defaults
- Well Documented: Comprehensive documentation and examples
Installation
Prerequisites
- Python 3.7+
- A Mercury Bank API token (get one from your Mercury dashboard)
Install from PyPI (Recommended)
pip install mercury-bank-api
Quick Start
from mercury_bank_api import MercuryBankAPIClient
# Initialize the client with your API token
client = MercuryBankAPIClient(api_token="your_api_token_here")
# Get all accounts
accounts = client.get_accounts()
for account in accounts:
print(f"{account.name}: ${account.currentBalance:.2f}")
# Get transactions for a specific account
transactions = client.get_transactions(account_id="account_123", limit=10)
for transaction in transactions.transactions:
print(f"${transaction.amount:.2f} - {transaction.counterpartyName}")
Note: Make sure to replace "your_api_token_here" with your actual Mercury API token from your Mercury dashboard.
API Reference
MercuryBankAPIClient
The main client class for interacting with the Mercury Bank API.
Initialization
client = MercuryBankAPIClient(
api_token="your_token", # Required: Your Mercury API token
timeout=30 # Optional: Request timeout in seconds (default: 30)
)
Methods
get_accounts() -> List[Account]
Retrieve information about all your bank accounts.
accounts = client.get_accounts()
for account in accounts:
print(f"Account: {account.name}")
print(f"Balance: ${account.currentBalance:.2f}")
print(f"Available: ${account.availableBalance:.2f}")
print(f"Status: {account.status}")
get_transactions(account_id, **kwargs) -> TransactionResponse
Retrieve transactions for a specific account with optional filtering.
Parameters:
account_id(str): The ID of the accountlimit(int, optional): Maximum number of transactions to returnoffset(int, optional): Number of transactions to skipstart_date(str, optional): Start date filter (ISO format)end_date(str, optional): End date filter (ISO format)status(str, optional): Filter by status ("pending", "sent", "cancelled", "failed")kind(str, optional): Filter by transaction kind
# Get recent transactions
transactions = client.get_transactions(
account_id="account_123",
limit=50,
status="pending"
)
print(f"Total transactions: {transactions.total}")
for transaction in transactions.transactions:
print(f"${transaction.amount:.2f} - {transaction.counterpartyName}")
get_account_by_id(account_id) -> Optional[Account]
Get a specific account by its ID.
account = client.get_account_by_id("account_123")
if account:
print(f"Found account: {account.name}")
get_transaction_by_id(account_id, transaction_id) -> Optional[Transaction]
Get a specific transaction by its ID.
transaction = client.get_transaction_by_id("account_123", "transaction_456")
if transaction:
print(f"Transaction: ${transaction.amount:.2f}")
Data Models
Account
Represents a Mercury bank account.
Key Attributes:
id: Account IDname: Account namecurrentBalance: Current account balanceavailableBalance: Available balanceaccountNumber: Account numberroutingNumber: Routing numberstatus: Account status ("active", "deleted", "pending", "archived")type: Account type ("mercury", "external", "recipient")
Transaction
Represents a Mercury bank transaction.
Key Attributes:
id: Transaction IDamount: Transaction amountcounterpartyName: Name of the counterpartycreatedAt: Transaction creation datestatus: Transaction status ("pending", "sent", "cancelled", "failed")kind: Transaction type (see API docs for all types)details: Detailed routing and payment informationattachments: List of transaction attachments
TransactionResponse
Response object containing transactions and metadata.
Attributes:
total: Total number of transactions matching the querytransactions: List of Transaction objects
Error Handling
The client raises MercuryBankAPIError for API-related errors:
from mercury_bank_api import MercuryBankAPIClient, MercuryBankAPIError
try:
client = MercuryBankAPIClient(api_token="invalid_token")
accounts = client.get_accounts()
except MercuryBankAPIError as e:
print(f"API Error: {e}")
Common Error Messages
- 401 Unauthorized: Invalid or missing API token. Check your token in the Mercury dashboard
- 403 Forbidden: Token doesn't have sufficient permissions for the requested operation
- 404 Not Found: The requested resource (account, transaction) doesn't exist
- 429 Too Many Requests: Rate limit exceeded. Implement retry logic with backoff
Authentication
The Mercury API uses Bearer token authentication. You can obtain an API token from your Mercury dashboard.
Security Note: Never commit your API token to version control. Use environment variables:
import os
from mercury_bank_api import MercuryBankAPIClient
api_token = os.getenv("MERCURY_API_TOKEN")
client = MercuryBankAPIClient(api_token=api_token)
Examples
See examples.py for comprehensive usage examples, including:
- Fetching all accounts
- Retrieving transactions with filtering
- Finding specific transactions
- Working with transaction details and attachments
To run the examples:
export MERCURY_API_TOKEN="your_token_here"
python examples.py
API Token Permissions
Make sure your API token has the appropriate permissions:
- Read Only: Can fetch accounts and transactions
- Read and Write: Additionally can initiate transactions (requires IP whitelist)
- Custom: Can be configured with specific scopes
For this client library, a Read Only token is sufficient for all implemented features.
Rate Limiting
The Mercury API may have rate limits. The client will raise a MercuryBankAPIError if you exceed these limits. Implement appropriate retry logic in your application if needed.
Contributing
- Fork the repository
- Create a feature branch
- Add tests for your changes
- Submit a pull request
Support
For API-related questions, consult the Mercury API documentation.
For client library issues, please open an issue in this repository.
License
See LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mercury_bank_api-1.0.0.tar.gz.
File metadata
- Download URL: mercury_bank_api-1.0.0.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f67c27b7a7b8f702513193982b07c066dac206b8ae50824ed1aab978445128f3
|
|
| MD5 |
9ea7d728a3cf175751501c06db2c8f6a
|
|
| BLAKE2b-256 |
66059b643a81485e5d86930ae0692f1acac68bddc02ec0967466cef3e0f65730
|
Provenance
The following attestation bundles were made for mercury_bank_api-1.0.0.tar.gz:
Publisher:
push_pypi.yml on kumpeapps/mercury_bank_api-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mercury_bank_api-1.0.0.tar.gz -
Subject digest:
f67c27b7a7b8f702513193982b07c066dac206b8ae50824ed1aab978445128f3 - Sigstore transparency entry: 255294085
- Sigstore integration time:
-
Permalink:
kumpeapps/mercury_bank_api-python@9453af283001437d0455a239680486d186b22187 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/kumpeapps
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
push_pypi.yml@9453af283001437d0455a239680486d186b22187 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mercury_bank_api-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mercury_bank_api-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70bc865d40c57e2d8354587ad5755e55d52e4952e02a91820b55aac82a7c9b58
|
|
| MD5 |
929ea29d919daf98fd0cad8d4fb2b830
|
|
| BLAKE2b-256 |
bae4beef45270121bcb39633fe3f868ecc04e8ac513f90df5756cd42fa68753c
|
Provenance
The following attestation bundles were made for mercury_bank_api-1.0.0-py3-none-any.whl:
Publisher:
push_pypi.yml on kumpeapps/mercury_bank_api-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mercury_bank_api-1.0.0-py3-none-any.whl -
Subject digest:
70bc865d40c57e2d8354587ad5755e55d52e4952e02a91820b55aac82a7c9b58 - Sigstore transparency entry: 255294095
- Sigstore integration time:
-
Permalink:
kumpeapps/mercury_bank_api-python@9453af283001437d0455a239680486d186b22187 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/kumpeapps
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
push_pypi.yml@9453af283001437d0455a239680486d186b22187 -
Trigger Event:
push
-
Statement type: