The protocol: mail.v1 over gRPC
Tern's native protocol is gRPC. One file, mail/v1/mail.proto, holds everything a client can do: 48 calls in five services, over HTTP/2. The tmail apps speak nothing else.
Native means the store was built for these calls. IMAP is served too, beside the API and on the same store. It is not what the API is made of, and no gRPC call is turned into IMAP commands anywhere.
Why gRPC
- The screen decides the call. A list of folders with their counts is one request. Over IMAP it is a
LISTand aSTATUSfor each folder. A page of conversations is one request, where it was a sort of the whole folder. - One contract, typed. The fields, their numbers and the comments that say what each costs are in one file. The clients' code is generated from it.
- Streams both ways. HTTP/2 carries a part in chunks, the rows of a query in as many replies as they need, an upload in pieces, and a mailbox's changes for as long as the client listens.
- Many calls on one connection. A client asks for several things at once without opening more connections or waiting for each answer in turn.
Two doors
| The public door | The services' door | |
|---|---|---|
| Who calls | A person's device | A trusted service |
| Presents | The access token the device got at sign-in, as authorization: Bearer, over TLS | The services' token |
| The user | The token's. A request need not name one; if it does, it must be that one | Named in each request, and may be any |
| Allowed | A call needs a scope: mail.read, mail.write, mail.send or settings. Only WhoAmI needs none | Every call, those marked "services only" among them |
| Where | Open to the network, HTTP/2 only | Loopback |
Ids, folders and the state
- A message is known by its id, unique within a user and never reused. So is a folder.
- All mail is folder 0. Every message is in it, and it is not in the list of folders.
- A user is given by id or by address. By id saves the server a lookup.
- Times are Unix seconds.
- A state is the number of changes a user's mailbox has seen. The folder list and the replies about changes say the state the mailbox is at. A request that changes the mailbox returns the state it made. Replies that would have to read it specially do not carry it.
The five services
Mail, 21 calls
| Call | Kind | What it does |
|---|---|---|
ListFolders | unary | Folders, with their counts, in one request |
CreateFolder | unary | Makes a folder, or returns the one there is with that name |
RenameFolder | unary | Renames one |
DeleteFolder | unary | Deletes a folder, for a trusted service. Its messages stay in All mail |
ListMessages | unary | A page of a folder, or of All mail, by arrival or sorted |
ListConversations | unary | A page of a folder's conversations, newest first, each with what a list's row shows. One state of the mailbox answers the page |
GetMessage | unary | One message: what a list shows, and as asked its header, its structure and its text. Never fetches a large part |
GetMessages | unary | Several messages in one call, answered in the order asked, as many as fit a reply |
GetPart | server stream | One part's content, decoded |
GetRaw | server stream | The message as it was received |
GetThread | unary | The messages of a thread, oldest first, whole or in pages |
Query | server stream | Answers a query of the query language: the columns, the rows, then what it read |
SetFlags | unary | Adds and removes flags, optionally only on messages unchanged since a state |
Relabel | unary | Puts messages in folders and takes them out of others, as one change: move, copy, archive |
Delete | unary | Deletes messages for good |
Append | client stream | Stores a message the caller has: a sent message, a draft |
Send | client stream | Sends a message as the user, and keeps a copy in Sent |
Upload | client stream | Keeps a file for a message to refer to. An upload that broke goes on from what the server holds |
Changes | unary | What changed after a position |
Watch | server stream | The same, and then each change as it is made |
GetQuota | unary | What the user holds, and their limit |
Accounts, 12 calls
Users and their addresses, the rules for arriving mail, passwords and quota. CreateUser, ResolveUser, SetPassword, SetQuota, AddAddress and ImportVacationMemory are for services only. A person reads and sets their own rules with GetRules and PutRules: rules are checked whole, every search and action, and refused whole if something is wrong, naming the rule and what. AskAddress and ConfirmAddress let a person add an address by a code sent to it. ImportVacationMemory brings what Cyrus remembered of the vacation replies it sent, so nobody is answered twice after a move.
Settings, 4 calls
The applications' settings, kept for them: names and values the server does not read. Each was last changed at a revision, which counts every change, and ListSettings with a revision gives only what changed since. PutSetting can be made to succeed only if the setting is still at the revision the device read, so two devices do not overwrite each other.
Push, 4 calls
Push notifications through Apple's service: each device's token, kept with its session and forgotten with it, and the labels whose new mail the account is told of.
Sessions, 7 calls
A person's own devices: who am I, my sessions, end one, make a password for a device that can only give one, change my password, list and remove passkeys.
Keeping a copy: Changes and Watch
A client that keeps a copy of the mailbox needs to know one thing: what happened after the point it has got to. That point is a position, a state and, where one change touched many messages, the message a reply ended at.
// How far a reader of changes has got. message Position { uint64 state = 1; optional uint64 after_message = 2; } message ChangesResponse { // New and changed messages as they are now, in the order of // their last change. A message that changed often is here once. repeated MessageSummary changed = 1; repeated uint64 deleted = 2; // How far this reply goes. Pass it as `after` to read on. Position position = 3; bool more = 4; bool folders_changed = 5; uint64 state = 6; uint64 settings_revision = 7; repeated uint64 changed_ids = 8; }
Changesanswers what changed after a position, up to 1,000 changes a reply. With no position it starts from the beginning, which is how a new copy is filled.Watchanswers the same and then stays open, sending each change as it is made. A client that reconnects passes the last position it was given, and misses nothing.- A message that changed many times is told once, as it is now.
- A client without a copy asks
Watchfor ids alone. The server then reads only its index of changes, and no message. - The same stream says when the settings moved, so one connection follows mail and settings both.
| Scenario | Calls | Received | Time, p50 |
|---|---|---|---|
| From nothing to the first page of the inbox in the client's copy | 4 to 5 | 229 KiB | 288 ms |
Back after 160 changes, caught up and following Watch | 7 | 41 KiB | 463 ms |
| A page of the inbox's conversations, with no copy | 1 | 6.4 KiB | 11 ms |
| A page of All mail's conversations, with no copy | 1 | 6.7 KiB | 14 ms |
Measured 2026-12-04 on NVMe with a warm server, on a machine that was busy with other builds, so the times show the shape of each scenario more than they are a benchmark. The core was timed in a test; what an app takes to draw is not in it. Of the 463 ms, 286 are the core opening its own copy before it asks the server anything. Before ListConversations a page was 3 calls and 35 KiB.
Reading
- What costs more is asked for. A page gives what a list shows. Each message's thread, folders and last change are a row of their own in the store, so they come only with
details.GetMessagegives the header, the structure and the text each only when asked. - A reply is kept under about 3 MiB. A page whose messages show a great deal comes back shorter than asked, with where to go on from. What a list shows of one message is bounded too: 64 addresses a field, the 32 nearest references.
- A message's structure costs one row and no attachment. The stored row keeps every byte of a message that is not part content, and its outline is the tree of parts.
- Parts are streamed as they are read.
GetPartandGetRawsend 64 KiB chunks, each read once the one before has been taken. A large part is checked against its size and hash as the last of it goes out. One that does not match ends the stream in an error, never in a shorter success.
Changing
- A change to messages or folders returns the state it made, so a client knows where its own change stands among the others.
- Move, copy and archive are one call,
Relabel, and one change in the store. SetFlagscan be conditional: only the messages that have not changed since a state.- A file is sent once.
Uploadkeeps it, resumable from the byte the server holds; a draft and the message sent then refer to it instead of carrying it again. Sendis safe to ask again. A message whose Message-ID the user has sent already is not sent a second time.
A refusal says why in a word
A gRPC status has a code and a sentence. One code answers for several things a client does different things about, and the sentence is for a person. So a refusal that a client may decide on carries a word beside them, in the metadata tern-reason: not-found, too-large, over-quota, too-many, changed, busy and so on. The words are part of the API as its fields are, and none changes its meaning.
Old and new together
A client may be newer than its server. A call the server does not have answers UNIMPLEMENTED, and the proto file says for each newer call what gives the same: ListMessages and a Query for ListConversations, a GetMessage each for GetMessages. Replies are compressed for the callers that ask for it.
What a call costs
The cost of a request is part of the contract, so it is measured in what a spinning disk charges for: seeks. With nothing cached, on the 403,757-message mailbox as first delivered, a store of 2 index runs and 7 message runs:
| Request | Seeks, average | What it reads |
|---|---|---|
| Folders with counts and the state | 2 | the user's first rows |
| A page of a folder, newest 50 | 4.0 | listing rows and the flags |
| A message's structure and text | 1 | the message's row |
| A thread | 5.3 | |
| Changes, the last 50 messages | 4 |
Counted 2026-11-26. Since then, rows that lie close together are read together, which these counts do not show.
And through the server, warm, on NVMe, one request at a time, 300 of each (100 of the query):
| Request | Median | 99th percentile |
|---|---|---|
| Folders with counts | 0.23 ms | 0.39 ms |
| A message with its structure and text | 0.32 ms | 0.53 ms |
| A page of a folder, newest 50 | 3.64 ms | 5.55 ms |
| A page of All mail, somewhere in the past | 3.75 ms | 5.95 ms |
| The thread of a message | 3.97 ms | 7.59 ms |
| Changes since the state before last | 5.22 ms | 9.57 ms |
| A query: a subject word in all folders, 50 results | 5.9 ms | 11.3 ms |
Measured 2026-11-26. The first run of these had every request at 41 ms, the folder list included: replies were waiting for the caller's delayed acknowledgement. The server now sends without delay.
Trying it
Any gRPC tool that reads a proto file can call the API. At the services' door of a development server, with grpcurl:
grpcurl -plaintext \
-import-path crates/mailapi/proto -proto mail/v1/mail.proto \
-H "authorization: Bearer $(cat token)" \
-d '{"user": {"address": "ann@example.org"}}' \
127.0.0.1:2500 mail.v1.Mail/ListFolders
grpcurl -plaintext \
-import-path crates/mailapi/proto -proto mail/v1/mail.proto \
-H "authorization: Bearer $(cat token)" \
-d '{"user": {"address": "ann@example.org"},
"query": "is:unread | stats by (folder) count() as unread"}' \
127.0.0.1:2500 mail.v1.Mail/Query