API Importers & ML Prediction¶
beancount-blue includes an extensible, secure banking API ingestion engine. It connects directly to bank and Open Banking APIs, stores state locally in compressed, version-controlled caches, and generates native Beancount directives.
It uniquely features a built-in Machine Learning Predictor using scikit-learn that learns from your existing Beancount books to automatically categorize payees and counter-accounts for incoming transactions.
Supported Bank Providers¶
1. Monzo (monzo)¶
- Authentication: OAuth 2.0 via Monzo Developer Portal (developers.monzo.com).
- Capabilities:
- Ingests transactions and balances from standard current accounts, joint accounts, and Monzo Flex.
- Automatically detects Monzo Pots and generates internal transfer postings to pot accounts (e.g.
Assets:Monzo:Main -> Assets:Monzo:HolidayPot). - Correctly resolves pending card authorizations and Mastercard clearing reversals using
mastercard_lifecycle_id. - Supports pocket money transfers for Young Monzo accounts.
2. Starling (starling)¶
- Authentication: Personal Access Token via Starling Developer Portal.
- Capabilities:
- Ingests primary accounts, spending spaces, and savings spaces.
- Automatically maps internal transfers between spaces.
- Supports multi-user joint accounts via
user_map(mapping Starling user UUIDs to friendly names in transaction metadata).
3. TrueLayer Open Banking (truelayer)¶
- Authentication: OAuth 2.0 Open Banking client credentials via TrueLayer Console (console.truelayer.com).
- Capabilities:
- Connects to dozens of UK and European banks, credit cards, and institutions (Amex, Chase, Barclaycard, etc.).
- Synchronizes both bank accounts and credit cards with automated debit/credit sign standardization.
Configuration¶
Importers are configured through YAML files typically stored in your api_configs/ directory.
Example Configuration: Monzo (api_configs/monzo.yaml)¶
importer_name: monzo
client_id: "oauth2client_00000000000000"
client_secret: "mnzpub.secret_key_here"
# Cache & State Settings
cache_data: "data/monzo.json.gz"
units: 2
# Native Machine Learning Prediction
auto_predict: true
predict_ledger_path: "main.beancount"
predict_model_path: "data/monzo_model.joblib"
predict_min_confidence: 0.6
predict_retrain_days: 7.0
# Account Mapping (Anchor Account)
account_map:
acc_123456789: "Assets:Current:Monzo"
Example Configuration: Starling (api_configs/starling.yaml)¶
importer_name: starling
personal_access_token: "your_starling_token_here"
cache_data: "data/starling.json.gz"
auto_predict: true
predict_ledger_path: "main.beancount"
predict_model_path: "data/starling_model.joblib"
account_map:
"00000000-0000-0000-0000-000000000001": "Assets:Current:Starling"
user_map:
"88888888-8888-8888-8888-888888888888": "Alice"
Example Configuration: TrueLayer (api_configs/truelayer.yaml)¶
importer_name: truelayer
client_id: "your_truelayer_client_id"
client_secret: "your_truelayer_client_secret"
cache_data: "data/truelayer.json.gz"
auto_predict: true
predict_ledger_path: "main.beancount"
predict_model_path: "data/truelayer_model.joblib"
account_map:
"acc_amex_gold": "Liabilities:CreditCard:Amex"
Machine Learning Auto-Categorization¶
When auto_predict: true is enabled, the importer automatically learns from your historical ledger:
- Training Heuristic: On extraction, it loads transactions from
predict_ledger_pathinvolving the accounts listed inaccount_map. - Feature Extraction & Classification: It extracts text features (narration, description, existing payee) using a
TfidfVectorizerand fits a fast, lightweight online logistic regression model (SGDClassifier). - Model Persistence: The pipeline is serialized to
predict_model_pathusingjoblib. - Inference: When new transactions arrive from the bank API, the model predicts the most likely
counter_account(e.g.Expenses:Groceries) and cleanpayee. Predictions with probability abovepredict_min_confidenceare automatically injected into the imported entries. - Smart Retraining: The model is automatically retrained when your ledger file modification time (
mtime) is newer than the saved model file, or if the model file is older thanpredict_retrain_days.
Command Line Interface (bean-blue-importer)¶
beancount-blue installs a dedicated CLI tool: bean-blue-importer.
Common Commands¶
# 1. Fetch latest data from bank API and save compressed cache
bean-blue-importer sync --settings api_configs/monzo.yaml
# 2. Output Beancount directives with ML predictions applied
bean-blue-importer beancount --settings api_configs/monzo.yaml > imported_entries.beancount
# 3. Force-retrain the ML categorization model
bean-blue-importer train --settings api_configs/monzo.yaml --ledger main.beancount
Python API Reference¶
beancount_blue.importer.delta_importer¶
beancount_blue.importer.delta_importer.APIImporter
¶
Bases: BaseSettings
Base class for all API Importers.
This class manages configuration, API state caching, filtering, and machine learning predictions.
Subclasses (like MonzoImporter or StarlingImporter) implement the refresh and extract logic.
Source code in beancount_blue/importer/delta_importer.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
extract(state)
abstractmethod
¶
Extract imported transactions from API data.
state: BaseModel to extract the transactions from.
Source code in beancount_blue/importer/delta_importer.py
167 168 169 170 171 172 | |
extract_available_balances(state)
abstractmethod
¶
Return a mapping of raw API account IDs to (available_balance, currency). Note: This represents the bank's available balance (often including pending transactions), not the cleared ledger balance.
Source code in beancount_blue/importer/delta_importer.py
174 175 176 177 178 179 | |
filter(data)
¶
Filter imported transactions based on config.
data: List of imported transactions. config: Configuration object.
Source code in beancount_blue/importer/delta_importer.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
filter_beancount(entries)
¶
Filter the final Beancount entries.
Source code in beancount_blue/importer/delta_importer.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
format_available_balances(state, acct=None)
¶
Translates raw available balances into a human-readable string using account_map.
Source code in beancount_blue/importer/delta_importer.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
get_types()
classmethod
¶
Magic introspection to avoid 'config_type = ...' boilerplate
Source code in beancount_blue/importer/delta_importer.py
150 151 152 153 154 155 156 157 158 | |
handle_callback(code, state)
¶
Handle an OAuth/web callback for this importer.
Source code in beancount_blue/importer/delta_importer.py
146 147 148 | |
refresh(state)
abstractmethod
¶
Refresh the data from the API.
state: mutable BaseModel for the state of the API connection and data.
Source code in beancount_blue/importer/delta_importer.py
160 161 162 163 164 165 | |
beancount_blue.importer.monzo¶
beancount_blue.importer.monzo.MonzoImporter
¶
Bases: APIImporter[MonzoData]
Monzo API Setup Instructions¶
To automatically sync your Monzo account, you need to create an OAuth API client in the Monzo Developer portal.
- Log in: Go to developers.monzo.com and log in with your email. You will receive a magic link in your email and a push notification in your Monzo app to approve the login.
- Create a Client:
- Click on "Clients" in the top navigation bar.
- Click "New OAuth Client".
- Give it a name (e.g.,
Beancount Sync). - Set the Confidentiality to
Confidential. - Set the Redirect URLs to
http://localhost:8000/callback(or your preferred local callback URL if you are using a custom auth flow). - Copy Credentials: Once created, copy the Client ID and Client Secret.
- Configure Fava: Paste these values into the
client_idandclient_secretfields below.
Note: Monzo requires you to re-authenticate API access via the app every 90 days. If your sync fails with an authentication error, you may need to approve the connection in your Monzo app.
Source code in beancount_blue/importer/monzo.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
beancount_blue.importer.starling¶
beancount_blue.importer.starling.StarlingImporter
¶
Bases: APIImporter[StarlingData]
Starling API Setup Instructions¶
To sync your Starling account, you need to generate a Personal Access Token from the Starling Developer portal.
- Log in: Go to developer.starlingbank.com and create a developer account if you haven't already.
- Connect your Bank Account: Follow the prompts to link your actual Starling Bank account to your developer account. You will need the Starling app on your phone to approve this.
- Create a Token:
- Navigate to "Personal Access Tokens" in the developer dashboard.
- Click "Create Token".
- Give it a name (e.g.,
Beancount Fava Sync). - Ensure you grant it read-only scopes for
account,balance, andtransactiondata. Do not grant payment or write scopes. - Copy the Token: Once generated, copy the token immediately. You will not be able to see it again.
- Configure Fava: Paste this token into the
personal_access_tokenfield below.
Source code in beancount_blue/importer/starling.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
beancount_blue.importer.truelayer¶
beancount_blue.importer.truelayer.TrueLayerImporter
¶
Bases: APIImporter[TrueLayerData]
TrueLayer API Setup Instructions¶
TrueLayer allows you to connect to dozens of UK/EU banks (like Amex, Chase, Barclaycard) using Open Banking.
- Create an Account: Go to console.truelayer.com and create a free developer account.
- Create an Application:
- In the TrueLayer Console, create a new application.
- Go to "App Settings".
- Under "Redirect URIs", add
http://localhost:8000/callback(or your preferred local callback URL). - Enable Data API: Ensure the "Data" (Open Banking) product is enabled for your application.
- Copy Credentials: Go to "App settings" -> "Credentials" and copy your Client ID and Client Secret.
- Configure Fava: Paste these into the
client_idandclient_secretfields below.
Note: To link a specific bank account, you will typically need to complete TrueLayer's Auth Link flow in a browser to authorize the connection and get a valid access_token.
Source code in beancount_blue/importer/truelayer.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |