Skip to main content

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

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 account
  • limit (int, optional): Maximum number of transactions to return
  • offset (int, optional): Number of transactions to skip
  • start_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 ID
  • name: Account name
  • currentBalance: Current account balance
  • availableBalance: Available balance
  • accountNumber: Account number
  • routingNumber: Routing number
  • status: Account status ("active", "deleted", "pending", "archived")
  • type: Account type ("mercury", "external", "recipient")

Transaction

Represents a Mercury bank transaction.

Key Attributes:

  • id: Transaction ID
  • amount: Transaction amount
  • counterpartyName: Name of the counterparty
  • createdAt: Transaction creation date
  • status: Transaction status ("pending", "sent", "cancelled", "failed")
  • kind: Transaction type (see API docs for all types)
  • details: Detailed routing and payment information
  • attachments: List of transaction attachments

TransactionResponse

Response object containing transactions and metadata.

Attributes:

  • total: Total number of transactions matching the query
  • transactions: 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

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for your changes
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mercury_bank_api-1.0.3.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mercury_bank_api-1.0.3-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file mercury_bank_api-1.0.3.tar.gz.

File metadata

  • Download URL: mercury_bank_api-1.0.3.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mercury_bank_api-1.0.3.tar.gz
Algorithm Hash digest
SHA256 bd42560832963beba9d5b0304cbe6900bb4d67f4f01060a0bdcf07fd60e93d7c
MD5 548f4a5e372d7a4b619f6add624418a8
BLAKE2b-256 c823df61ec99011fe2d62b102c5268afbfc5ac684e7dd74cbc47358a125e1891

See more details on using hashes here.

Provenance

The following attestation bundles were made for mercury_bank_api-1.0.3.tar.gz:

Publisher: push_pypi.yml on kumpeapps/mercury_bank_api-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mercury_bank_api-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for mercury_bank_api-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 450c2bf22903899aa2bbd3b76fa4c76e274b8c836e51fe99e3ed1f3cab987383
MD5 db49146cebec076f2a73bf23cdbfa3b5
BLAKE2b-256 43016a7b0e25be2d407621407e896f6782021dab40f5759bd74348e3c9e5e518

See more details on using hashes here.

Provenance

The following attestation bundles were made for mercury_bank_api-1.0.3-py3-none-any.whl:

Publisher: push_pypi.yml on kumpeapps/mercury_bank_api-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page