# Accounts

Source: https://docs.formance.com/v3.2/modules/ledger/core-concepts/accounts

Stack: v3.2

An account is a named container for asset balances inside a ledger. Its primary function is to group assets together based on criteria that best suit the business's needs.

In a food delivery app, accounts organize financial activities efficiently. Each restaurant has an account to manage their earnings from orders. Riders have accounts to track their delivery payments. Customers have accounts for their wallets, where they can add and spend money for orders. The platform maintains separate accounts for service fees and taxes.

## Creating accounts

The number of accounts in a ledger is unlimited. Accounts do not need to be created prior to being used; submitting a transaction involving an account automatically creates it in the ledger.

## Naming accounts

Account addresses are **permanent identifiers**. Once an account is used in a transaction, its address is fixed and cannot be renamed. Choose names that will remain accurate for the lifetime of the account.

### What belongs in the address vs in metadata

The address should encode structural identity: the account's type and a stable external ID. It should not contain mutable data, status flags, or PII that might change.

| Put in the address                            | Put in metadata                      |
| --------------------------------------------- | ------------------------------------ |
| Account type (`users`, `merchants`, `orders`) | Display name, email address          |
| Stable external ID (`usr-123`, `mch-abc`)     | Account status (`pending`, `active`) |
| Sub-account purpose (`wallet`, `earnings`)    | Business attributes, tags            |

Good examples:

```
users:usr-123:wallet
payments:stripe:ch_abc456
merchants:acme-corp:earnings
```

Bad examples:

```
users:john@example.com         ← email could change
orders:pending:ord-789         ← "pending" is mutable state
merchants:Acme Corp:main       ← spaces aren't allowed, name could change
```

### Hierarchy enables prefix filtering

The colon-separated segments are not just for readability; they enable prefix-based queries. You can retrieve every account under `users:usr-123:` to see all of a user's accounts, or every account under `payments:stripe:` to see all Stripe payment records. Design the hierarchy around how you'll query, not just how things look.

Account addresses follow a hierarchical structure using colon-separated segments, validated against the pattern: `^[a-zA-Z0-9_-]+(:[a-zA-Z0-9_-]+)*$`

Each segment matches `[a-zA-Z0-9_-]+`, meaning it can contain letters (a-z, A-Z), digits (0-9), underscores (\_), hyphens (-).

**Examples:**

- `users` — a single segment
- `users:001:main` — a multi-level address with `:` as separator

This hierarchical structure lets you organize accounts by type, user ID, or provider — for example `payments:stripe:customer-123` or `wallets:user:42:main`.

If an invalid account address is used in a transaction or API call, the system returns a validation error and the operation is rejected.

To enforce specific naming conventions beyond the basic pattern, use a [Ledger Schema](https://docs.formance.com/v3.2/modules/ledger/working-with/ledger-schema). Schemas let you define which account structures are valid and reject transactions that use undefined accounts.

## Using Metadata

Accounts can carry metadata: arbitrary key-value pairs for storing external references, labels, or any contextual information. Metadata is additive: each update merges new keys with existing ones.

| Operation           | Behavior                                                                                                         |
| ------------------- | ---------------------------------------------------------------------------------------------------------------- |
| Set a new key       | Key is created with the provided value                                                                           |
| Set an existing key | Previous value is overwritten                                                                                    |
| Remove a key        | Supported via the [delete account metadata](https://docs.formance.com/stack-api-reference/introduction) endpoint |

Account metadata can also be used in Numscript transactions, see [metadata in Numscript](https://docs.formance.com/modules/numscript/reference/metadata) for more information.

Transactions also support metadata. See [Transactions & Postings](https://docs.formance.com/v3.2/modules/ledger/core-concepts/transactions#metadata) for details.

## Closing accounts

Ledger has no operation that closes, freezes, or deletes an account. An account persists once it has been used, even when it is inactive. Its postings stay in transaction history, and any later transaction that names its address moves value into or out of it again.

To retire an account, such as when a customer leaves or a card is cancelled, handle it in your application:

1. **Drain the balance.** Record a transaction that moves each remaining asset to where it belongs, such as a payout account, a write-off account, or `world`.
2. **Mark the account.** Set account metadata such as `status: "closed"` and `closedAt`. Ledger stores this metadata but does not check it when it accepts a transaction.
3. **Stop writers.** Make every service and Numscript program that uses this address check the status before it writes.
4. **Filter it out of reads.** Exclude accounts marked as closed when you list accounts.

Ledger 3.0 adds account types, which can drop drained accounts out of balance reads and block whole address patterns. Neither closes an individual account.
