# From your bank | Badlen Docs

> Reading a current account at its bank through an application of your own at Enable Banking, set up step by step, how often it reads, what the reading service sees, and what each refusal means.

Source: https://docs.badlen.fr/getting-data-in/from-your-bank/

Badlen can read a current account's booked operations and balance at its bank, four times a day,
through an application of your own at Enable Banking, a reading service that holds the registration
banks require. Setting it up takes four steps, below: create the application, turn the reading on
in the instance, give Badlen the application's key, then link each account. The first two are
done by the person running the instance; the key and the accounts are each reader's own, and no
account of anyone else's goes through them. Enable Banking keeps
[the list of banks it reaches](https://enablebanking.com/open-banking-apis), and
[How Badlen reads a bank](https://docs.badlen.fr/how-it-works/reading-a-bank/) explains why it works this way.

## Current accounts only

It reads **current accounts**: their booked operations and their booked balance. Pending
operations are left out, because their wording changes once they book. A card whose purchases
are debited from its current account is not read as an account of its own: its spending already
arrives as operations of that current account.

Banks do not offer savings books, PEAs, life insurance contracts or brokerage accounts through this
channel. Those stay on the balances you state each month, [by hand](https://docs.badlen.fr/getting-data-in/by-hand/) or
[from a statement](https://docs.badlen.fr/getting-data-in/from-a-statement/).

## 1. Create the application at Enable Banking

1. Sign up on the Enable Banking control panel and create an application.
2. Choose **restricted production**. It reads the accounts you link to it yourself, which is all
   Badlen needs; there is no need to ask for the restriction to be lifted.
3. Under **Redirect URLs**, enter `https://badlen.fr/bank-return/`. That return page works for
   any instance. An instance with a public `https` address of its own can list that address's
   return page instead, and set `BANK_SYNC_RELAY_URL` to the same value.
4. Leave the **allowed widget origins** and the **webhook** fields empty: Badlen uses neither.
5. Download the application's **private key**, a `.pem` file, and keep it somewhere safe: it is
   offered once. Note the **application id** beside it; the two go together.
6. In the control panel, link to the application the bank accounts you want read.

## 2. Turn it on in Badlen

The person running the instance does this once:

1. Set `BANK_SYNC_ENABLED=true` in `.env`, beside the other settings of
   [Configuration](https://docs.badlen.fr/install/configuration/), and restart.
2. In **Settings → Administration**, the **Bank connection** switch is on by default. Turning it
   off closes the reading for every reader without editing `.env`.

## 3. Give Badlen the key

In **Settings → Syncs** (`/reglages/synchronisations`), the **Bank reading key** card asks for
the **Application identifier** and the private key: choose the `.pem` file. Before saving, the
card shows the file's name and says whether it holds a private key. Once saved, it shows the
application id and _stored encrypted_, and never a character of the key again; **Replace**
opens the form anew.

## 4. Link each current account

On the same tab, press **Connect a current account**, pick the account if you have several, then
your bank's country and name. Your bank's own page opens. Once you approve, it sends you back to
Badlen, which says _The account is linked to your bank, and its history is imported._

When the bank opens several accounts at once, Badlen links the one it already knows by its bank
identifier, or the only current one. Otherwise it asks which one this is, within the hour the
bank allows.

## When something goes wrong

| What Badlen says                                                            | What it means                                                                                                                                |
| --------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| _Reading bank accounts is turned off on this instance._                     | `BANK_SYNC_ENABLED` is not `true`, or the **Bank connection** switch is off: see step 2                                                      |
| _No bank reading key is stored._                                            | the key was never saved, or was removed: see step 3                                                                                          |
| The key card says _unreadable_                                              | the key was encrypted with another `SECRET_ENCRYPTION_KEY` than the instance's: choose the `.pem` again                                      |
| _The bank reading application does not accept the return address…_          | the address the message names is missing from the application's Redirect URLs (`REDIRECT_URI_NOT_ALLOWED`)                                   |
| _The bank reading service refuses the stored key…_                          | the application id and the private key do not belong together, or the application no longer exists                                           |
| _The bank reading service does not know this bank in this country._         | check the country picked before the bank; a bank missing from the service's list cannot be read                                              |
| _The bank did not grant access. Nothing has changed._                       | the approval stopped on the bank's page                                                                                                      |
| _Connecting to the bank failed. Nothing has changed; try again._            | the way back from the bank broke off; start again                                                                                            |
| _The account is linked to your bank, but reading its operations failed…_    | **Sync** within the hour reads the whole history; after that the bank serves 90 days only, and only renewing the access brings back the rest |
| _The bank opened no current account. Nothing is linked._                    | the bank offered only cards, or only accounts already linked                                                                                 |
| _The account can no longer be chosen: the hour the bank allows has passed…_ | the bank opened several accounts and none was picked within the hour: reconnect the account                                                  |
| _The bank refuses further reads for now…_                                   | the bank's allowance for the day is used up; the next scheduled read tries again                                                             |
| _The bank reading service did not answer properly._                         | the service failed this time; the next scheduled read tries again                                                                            |
| _The authorisation given to the bank has expired or was withdrawn…_         | the access ended, the bank withdrew it early, or Badlen closed it when the key changed: reconnect the account                                |

On the Syncs tab, an account whose last read failed shows **Read failing** with the reason. When
the reason is the key, the key card is on that same tab.
An account whose first read did not finish within the hour says _History incomplete_: renewing
the access catches up the rest.

## How the reads work

- **The first read** takes the whole history the bank serves in the hour after you approve; after
  that hour, the bank serves 90 days back at most. On an account whose operations already came in
  from statements, it starts the day after the last one, so a line the bank words differently is
  not written twice.
- **Afterwards** Badlen reads each linked account four times a day, at 01:00, 07:00, 13:00 and 19:00
  by the server's clock, which is what the bank allows without you present. **Sync** reads at once.
  Each read starts a week before the previous one, because a bank lists some operations days after
  their booking date; the statement import's duplicate check keeps them single.
- **The access lasts** as long as the bank grants it, 180 days at most. Two weeks before its end, a
  notice above every page offers to renew it. Renewing goes straight back to the same bank, and the
  accounts it reads keep their history. A bank that withdraws the access early is shown the same
  way, as an account to reconnect.

## The balance rule

The bank's booked balance becomes the account's balance for the day only when it equals Badlen's:
the last balance plus every operation since, to the cent. Otherwise nothing is written and the
bank's figure is shown beside Badlen's, with the gap. A gap younger than a day is usually the bank
counting an operation it has not listed yet; one that outlasts a day of reads may be an operation
missing from Badlen, or in it twice.

## The Syncs tab

**Settings → Syncs** (`/reglages/synchronisations`) lists every source that keeps an account up to
date by itself, and the keys Badlen reads them with. Each bank connection shows its bank, when the
access ends and the days left, then each account it reads: its value, its state (up to date,
waiting for the bank, balance gap, to renew, to reconnect, read failing with the reason, or account
to choose), its last read, its next one, how many operations the last read imported, and how many
of the day's four scheduled reads are left.

From there you read an account now, renew the access early or reconnect it once it has ended,
disconnect an account, open it, or connect another one. The row actions are icons, named for a
screen reader and on hover; reconnecting an ended access is written out, being the one thing
left to do on that card. Disconnecting stops the reads and keeps every operation already imported;
once no account reads through it any more, the bank is told to close the access. Removing or
replacing the key by another, deleting your account, or restoring a sealed backup that carries
another key closes every access the old key opened, and the accounts it read are left to
reconnect; a sealed backup that links an account through another access closes the one it
replaces. A backup without a passphrase brings no bank link back, and leaves one in place.

The same tab lists the [crypto addresses](https://docs.badlen.fr/your-wealth/crypto-wallet/) Badlen follows. An account's
own **Settings** tab keeps one line saying whether a bank reads it, with a link to this tab.

## Seen by the reading service

Enable Banking carries out each read for you, so it sees what a read returns: the accounts you
linked, their operations and their balances. When you are at the keyboard, on **Sync** and on the
way back from your bank, Badlen also passes on your browser's address and user agent, as the bank
asks; the scheduled reads carry neither.

The way to your bank and back carries a signed note naming the instance's address, your user
identifier in Badlen and the account being linked, encoded but readable. The bank, Enable Banking
and the host of `badlen.fr` can read it, along with the one-time code the bank sends back. None of
it is usable without your private key, which never leaves your instance. An instance served over
plain `http` passes that code and that note unencrypted on its own network.

## Why the bank returns through badlen.fr

A bank sends you back only to an `https` address registered in the application, and a self-hosted
instance often has none, or one that differs from one reader to the next. So the bank returns to
`https://badlen.fr/bank-return/`, a static page that reads which instance started the connection
and forwards the answer there. It keeps nothing. The first time a browser comes back to a given
instance address, the page names that address and waits for you to confirm it; after that, it
forwards on its own. Your instance then refuses an answer it did not sign, one more than an hour
old, or one started in another browser.

An operator who prefers a page of their own sets `BANK_SYNC_RELAY_URL` to its address, and
registers that address in the application instead.
