Files
FargoImport/ynab_example.py
2026-03-22 23:30:38 -04:00

66 lines
2.0 KiB
Python

#!/usr/bin/env python
"""Example usage of YNAB client for fetching transactions."""
import json
from pathlib import Path
from ynab_client import YNABClient
def main():
# Initialize YNAB client from config
client = YNABClient("ynab.yml")
# Fetch transactions from the past 30 days
print("Fetching transactions from the past 30 days...")
exclude_cleared_statuses = ["reconciled"]
transactions = client.get_transactions_for_days(
days=30,
exclude_cleared_statuses=exclude_cleared_statuses,
)
print(
f"Found {len(transactions)} transaction(s) "
f"(excluding: {', '.join(exclude_cleared_statuses) or 'none'})\n"
)
# Display first few transactions
for i, txn in enumerate(transactions[:10]):
print(f"Transaction {i + 1}:")
print(f" Date: {txn['date']}")
print(f" Payee: {txn['payee_name']}")
print(f" Category: {txn['category_name']}")
print(f" Amount: ${txn['amount_milliCurrency'] / 1000:.2f}")
print(f" Account: {txn['account_name']}")
print(f" Memo: {txn['memo']}")
print(f" Cleared: {txn['cleared']}")
print()
# Optionally save all transactions to JSON
output_file = Path("ynab_transactions.json")
serializable = [
{
"id": txn["id"],
"date": txn["date"],
"payee_name": txn["payee_name"],
"category_name": txn["category_name"],
"amount": txn["amount_milliCurrency"] / 1000, # Convert to regular currency
"amount_milliCurrency": txn["amount_milliCurrency"],
"memo": txn["memo"],
"account_id": txn["account_id"],
"account_name": txn["account_name"],
"cleared": txn["cleared"],
}
for txn in transactions
]
with output_file.open("w", encoding="utf-8") as f:
json.dump(serializable, f, indent=2)
print(f"Saved {len(transactions)} YNAB transaction(s) to {output_file}")
if __name__ == "__main__":
main()