On November 22 and 23 we merged a feature into Kumiko, our open source TypeScript framework, that lets an app send a real paper letter. The app hands over a finished PDF, and a print service in Germany (LetterXpress) prints it, puts it in an envelope and gives it to Deutsche Post. Our property management app, Veridom, will be the first to use it, for operating cost statements and rent increases.

It's out as a pre-release on npm under the next tag, and no real letter has gone out yet. The screenshots below come from our end to end test run against a simulated print service. I'm writing this now because the fixes from those two days are the kind of bugs I'd rather talk about before anyone pays for them.

Why a letter is harder than an email

We already send email from these apps. A letter differs in three ways.

A letter costs money for every page. Once the print service has it, you can cancel only inside a short window, and after that it is on its way. And it has no happy ending you can observe: the provider tells you it posted the letter, never that it arrived. So our letter has no "delivered" state. The last good state is sent, and we keep polling a sent letter for 14 more days in case it comes back as returned.

Writing the letter

A letter starts as a draft from a template, the same draft editor the apps use for other PDFs. The placeholders are filled from the lease when the draft is created and keep those values while you edit the text around them.

Draft editor with a rent increase letter, placeholders filled from the lease

The interface is in English here, the letter stays German because it goes to a German tenant.

When the draft is finished, the app calls sendLetter. That call runs in the caller's transaction and always creates the letter in awaiting-approval. A background job then merges the files, counts pages and sheets and asks the provider for a price, so the approval screen shows stored values.

A person approves, with a separate role

Approving is its own write with its own role, LetterApprover. TenantAdmin doesn't include it. The reason in the README is one line: sending costs money.

Approval screen with net price, monthly limit and a preview of the letter

The screen shows the net price, how much of the monthly limit is used and what is left after this letter, and the letter exactly as it will be printed. The price here comes from the simulator.

Our apps have an AI assistant that can call the write handlers exposed to it. approve is one of them, but it is marked effect: "outbound", and for outbound effects the assistant asks before every single call. There is no "don't ask again" for it.

There is one way around the person. An app can create a letter with approval: "auto", and the preparation job approves it once the price is known. It is an option of the server call and not a field of the payload; the review of that change moved it out, so a client can't ask for it. The app sets it from its own settings. In Veridom each process (statements, rent increases and so on) will have a setting for this, and the default is review by a person.

The monthly limit starts at zero

Every tenant has a monthly limit in net cents. It is 0 until a privileged user sets it, so a new tenant sends nothing.

The approve button checks the limit early so the approver hears about a shortfall before releasing anything. The README says plainly that this check is not atomic: two approvals at the same moment can both pass. The binding check comes later, when the handover reserves the quoted price against the limit. A letter that doesn't fit then fails with monthly-limit, which also covers a limit lowered after approval.

Five minutes to change your mind

An approved letter waits five minutes by default before the handover starts. Until then, cancelling is free and never talks to the provider.

The timeout we never retry

This part has the most rules. When the handover to the provider times out, we don't know whether the provider has the letter. Retrying could print and post it twice, and both copies get billed.

So a timeout moves the letter to submission-unknown, and nothing sends it again by itself. A poll asks the provider whether it knows a letter with our reference, which is only the letter's id. If the provider can't tell, a person resolves it. A letter that sits in submitting for ten minutes is treated the same way.

What we fixed in the first two days

On November 22 the design was a background job that hands letters over, plus a poll every five minutes that took over whatever the job couldn't finish. That left two runners that could hand over the same letter. The fix a day later: the poll no longer hands anything over. For a letter stuck in approved it appends a handover-restart-requested event, and the submit job, which works through letters one at a time, does the handover. If an approved letter still can't be handed over two hours after its hold ended, it fails with handover-stalled instead of waiting forever.

We also found that switching the provider configuration could make the poll ask the new provider about letters the old one had. Now a letter belongs to the provider it was approved with, and a mismatch shows up as its own error.

Two smaller ones from the same week. In production the live switch has to be set to true or false explicitly; if it's missing, preview, send, approve and cancel answer 422 instead of quietly using the simulator. Outside production, setting it to true stops the boot. And when the address the provider recognised in the PDF differs from the address we stored, the poll cancels the letter while the cancel window is still open.

Every step is an event

The letter is an event-sourced entity like everything else in Kumiko. Each allowed transition is listed in one table, and each step writes an event, so "who approved this, and what happened next" is a list you can read. The events carry only the letter id and the state it came from. The recipient's address lives on the letter, encrypted for that letter, and only the id goes to the provider as a reference.

A letter that is legally worthless

One last thing we only learned while planning it. In Germany, terminating a residential lease requires the written form, which means a handwritten signature. A letter printed by a service has none, so it's invalid no matter how fast it arrives. The framework refuses a letter that needs the written form unless the caller explicitly acknowledges that risk. We'd rather the code say no up front.

What's next

Next, Veridom switches the operating cost statements over, and the first real letters go out. I'll write again when we have numbers from actual mail.

If you've sent physical mail from code: how did you handle the handover that timed out?