CLEMA protocol

Authentication

An account is created and signed in to by phone: the server sends a code, and receiving it proves the number. There are no passwords. A code to sign in to an account signed in elsewhere goes there, as a message from Mandarin; any other goes the ways the server sends by — to the number's Telegram account, by SMS — the cheapest first. A device keeps the tokens it is given and signs in with them afterwards, so the code is needed once per device. A device without a SIM of its own — a browser — can instead be let in by a phone already signed in, which scans a QR code it shows.

Only connection.hello, connection.ping, connection.close, auth.code, code.get, auth.phone, auth.qr, auth.token, auth.refresh, invite.get and link.open are available before authentication.

Request a Code

{
  "id": "1",
  "type": "auth.code",
  "data": {
    "phone": "+79161234567",
    "app_hash": "FA+9qCX9VSu",
    "language": "ru"
  }
}
Field Type Required Description
phone string Yes The number in E.164 form: +, then the country code and the subscriber number
app_hash string No The 11-character hash Android's SMS Retriever derives from the app. An SMS with the code then ends with it, unless the service sending it allows no such ending, which lets the app read the code without the SMS permission
language string No BCP 47 language tag for the SMS text, used when the server has it; the session the code signs in keeps it, unless its client says its own
via string No Send the code the way an earlier answer named in next, rather than the first way the server would

Success:

{
  "id": "1",
  "type": "auth.code",
  "ok": true,
  "data": {
    "verification_id": "v_7c1e...",
    "code_length": 6,
    "expires_in": 300,
    "resend_in": 60,
    "sent_to": "app",
    "devices": ["Google Pixel 10 Pro Fold", "Mac"],
    "more_devices": 2,
    "state": "sent",
    "next": "telegram"
  }
}
Field Type Description
verification_id string Opaque handle of this attempt, passed to auth.phone
code_length number Digits in the code, for sizing the input
expires_in number Seconds the code stays valid
resend_in number Seconds before another code may be asked for this number, the way next names
sent_to string The way the code went: app — the account's devices, as a message from Mandarin — telegram — the number's Telegram account, in its Verification Codes chat — sms, or fixed — a code the service set for the number, sent nowhere. A server may add ways: a client says something plain, such as "The code was sent", of one it does not know
to string Where the code went in words a person knows: the number, for a code sent to it
devices array With app: the most active of the devices the code went to — those connected now, then by when each was used last — by the device_name their sessions gave (else the app's name), each once: three at most, or two when there are more
more_devices number With app: how many devices the code went to besides those named
state string How far the code got, as far as its way tells: sent, delivered — on the person's device — read, or failed — it will not arrive
switch_in number Seconds until the server sends the code the next way itself, unless it arrives by then
next string The way another code may be asked for, by via: the first way after this one that reaches the person otherwise, else this one again. Absent for fixed

A server sends codes the ways it is set up to, in its order: the account's devices first, which costs nothing, then the channels it has — Telegram, SMS gateways — each reaching the numbers it reaches. The devices are those of the account's sessions made on CLEMA clients — by a code or a QR code, not through a transport, and not a staff console — and a code to change the number never goes there: it proves the number, not the account. A code in the app is told to the account's devices alone: no transport shows it. An account that blocked Mandarin gets its codes the other ways. The service may set the way of a number: one channel only, or a code of its own, sent nowhere (fixed), for a number to test with.

A way that refuses the code passes it to the next one at once. A way that gives a code a time to arrive — Telegram — and does not deliver it within it, or that reports the code will not arrive, hands the same code on to the next way, under the same verification, while a connection waits for it: the connection is told by code.updated. next and resend_in say how the client offers another code; one asked for by via goes that way and on from there.

Requesting again for the same number replaces the previous code: only the newest one is accepted. A number gets a code a minute, five SMS a day, and ten a day each other way: each is five more guesses at its code, and a message to its owner. An account asks to bind three numbers a day, and an address is sent a few codes an hour, and by channels a day (see Rate Limits). A code that did not go — a way that failed, or was spent — counts against none of these. When no way can send the code the answer is delivery_failed, and the client may try again later — after retry_after, when it is given; via naming a way codes to the number do not go is not_supported. sent_to tells whether the number has an account signed in somewhere, as Telegram's answer does, and devices names its devices to whoever asks; the answers do not otherwise depend on it.

Codes by channels — SMS gateways, Telegram — cost money: to a number that has no account yet, or that an account binds, they go only to the countries the service lets them go to, so many an hour and a day each, as its staff set it. A number of a country it sends no codes to is not_supported with reason country, and a country's codes spent for the hour or the day hold its new numbers' codes back: delivery_failed with retry_after. A number with an account was proven once, and gets its codes wherever it is. The pauses of codes by channels to a block of numbers hold its codes back too, delivery_failed with retry_after, whoever's they are: the server's own, of a block where codes to many numbers went unconfirmed — the stream of codes nobody waits on that spends someone else's money — and its staff's.

Follow the Code

While a client waits for a code, the server tells it how the code goes: code.updated carries the answer above anew, as state changes or the code goes another way. A client coming back to the code — after a reconnect, say — asks how it is, and waits for it from then on:

{"id":"2","type":"code.get","data":{"verification_id":"v_7c1e..."}}
{
  "id": "2",
  "type": "code.get",
  "ok": true,
  "data": {"verification_id": "v_7c1e...", "code_length": 6, "expires_in": 251, "resend_in": 11,
    "sent_to": "sms", "to": "+79161234567", "state": "delivered", "next": "sms"}
}
{
  "type": "code.updated",
  "data": {"verification_id": "v_7c1e...", "code_length": 6, "expires_in": 270, "resend_in": 30,
    "sent_to": "sms", "to": "+79161234567", "state": "sent", "next": "sms"}
}

A code that failed while nobody waited goes the next way once somebody asks. A verification spent, replaced or expired is verification_expired, as is one asked about by an account it is not for.

Sign In with the Code

{
  "id": "2",
  "type": "auth.phone",
  "data": {
    "verification_id": "v_7c1e...",
    "code": "123456"
  }
}

The new session is labelled by the client the connection's hello said, and takes the language the code was asked in where the hello gave none.

Success:

{
  "id": "2",
  "type": "auth.phone",
  "ok": true,
  "data": {
    "user_id": 12345,
    "access_token": "abc123...",
    "refresh_token": "xyz789...",
    "expires_in": 3600,
    "created": true,
    "session": {
      "session_id": "s_0123...",
      "client": {"device_name": "Pixel 8", "platform": "Android", "name": "Mandarin", "version": "2.1", "build": 693, "language": "ru-RU"},
      "created_at": "2026-09-24T10:00:00Z",
      "last_active_at": "2026-09-24T10:00:00Z",
      "expires_at": "2026-10-24T10:00:00Z",
      "current": true,
      "online": true,
      "push": false
    }
  }
}
Field Type Description
user_id number User's numeric ID
access_token string Short-lived token for session authentication (1 hour)
refresh_token string Long-lived token for obtaining new access tokens (30 days)
expires_in number Access token lifetime in seconds
created boolean true when this sign-in created the account, so the client asks for a profile next
session object Persistent login created for this client

The first sign-in with a number no account has creates an account for it, which Mandarin welcomes; later ones sign in to the account the number belongs to (see Phone Number), which Mandarin tells of the new session. The connection is then authenticated.

Failure:

{
  "id": "2",
  "type": "auth.phone",
  "ok": false,
  "error": {
    "code": "invalid_code",
    "message": "The code is wrong"
  }
}

A wrong code returns invalid_code. The verification stays usable until its code expires or five wrong codes have been tried; after that, and for an unknown or already used handle, auth.phone returns verification_expired and a new code has to be requested.

Sign In with a QR Code

A web client can skip the SMS: it shows a QR code, the user's phone scans it, the user confirms, and the web client receives a session of its own. Signing in with a code keeps working everywhere; the QR code only saves the SMS.

The waiting client asks for a code on an unauthenticated connection. The new session is labelled by the client its hello said, which the phone shows before the user agrees:

{"id":"qr-1","type":"auth.qr"}
{"id":"qr-1","type":"auth.qr","ok":true,"data":{"url":"https://mandarin.im/login#Q2vU9dX1sL8...","expires_in":60}}
Field Type Description
url string What the QR code shows, as it is: <link_base>/login#<token> on a server with a link base, otherwise https://<server>/login#<token> on the name clients reach the server by
expires_in number Seconds the code stays valid

A code lives for expires_in and no longer than its connection. The client asks for a fresh one before it runs out, and a new auth.qr replaces the connection's previous code.

A link keeps the token in its fragment, which a browser that opens the link instead of the app never sends anywhere. The phone scans the code and, when it belongs to the server it is signed in to — its link base, or its name — asks who is waiting:

{"id":"qr-2","type":"qr.get","data":{"token":"Q2vU9dX1sL8..."}}
{"id":"qr-2","type":"qr.get","ok":true,"data":{"client":{"device_name":"Chrome on Windows","platform":"Windows","name":"Mandarin Web","version":"1.0.0"},"ip":"203.0.113.7","location":"Moscow, Russia","expires_in":42}}
Field Type Description
client object What the waiting client says about itself; untrusted
ip string The waiting client's address
location string Where that address is, when the server can tell
expires_in number Seconds left

The phone shows all of it and asks the user to confirm. Only then:

{"id":"qr-3","type":"qr.accept","data":{"token":"Q2vU9dX1sL8..."}}
{"id":"qr-3","type":"qr.accept","ok":true,"data":{"session":{"session_id":"s_4567...","client":{"device_name":"Chrome on Windows","platform":"Windows","name":"Mandarin Web","version":"1.0.0"},"created_at":"2026-09-24T12:00:00Z","last_active_at":"2026-09-24T12:00:00Z","expires_at":"2026-10-24T12:00:00Z","current":false,"online":true,"push":false}}}

The waiting client receives the body auth.phone returns, without created, as an event, and its connection is authenticated from that moment:

{"type":"qr.accepted","data":{"user_id":12345,"access_token":"abc123...","refresh_token":"xyz789...","expires_in":3600,"session":{"session_id":"s_4567...","client":{"device_name":"Chrome on Windows","platform":"Windows","name":"Mandarin Web","version":"1.0.0"},"created_at":"2026-09-24T12:00:00Z","last_active_at":"2026-09-24T12:00:00Z","expires_at":"2026-10-24T12:00:00Z","current":true,"online":true,"push":false}}}

The new session is an ordinary one: it is refreshed, listed and revoked like any other. A code is used once. An unknown, expired or used code, or one whose connection has closed, returns verification_expired from qr.get and qr.accept.

Any session made on a CLEMA client can let another device in, one a QR code let in too: a code to sign in with reaches it anyway. A session made through a transport gets not_authorized: it has no way to see who is asking. The account hears of the new session from Mandarin, with the device that let it in. Whoever gets the user to scan their code gets a session in the user's account, which is why the phone always shows who is asking, and why a user should scan only a code on a screen they opened themselves.

Login with Token

Used to authenticate with a previously issued access token (e.g., after page refresh or reconnection). The session is relabelled by the client the connection's hello said, console aside, keeping its language where the hello gave none, and is answered so; a hello that said nothing leaves its label as it was, and a client of a build no longer supported, by that label, is answered client_outdated.

Request:

{
  "id": "2",
  "type": "auth.token",
  "data": {
    "access_token": "abc123..."
  }
}

Success:

{"id":"2","type":"auth.token","ok":true,"data":{"session":{"session_id":"s_0123...","client":{"device_name":"Alice's laptop","platform":"macOS","name":"Mandarin Web","version":"0.1.0"},"created_at":"2026-08-05T10:00:00Z","last_active_at":"2026-08-05T10:00:00Z","expires_at":"2026-09-04T10:00:00Z","current":true,"online":true,"push":false}}}

Failure:

{
  "id": "2",
  "type": "auth.token",
  "ok": false,
  "error": {
    "code": "invalid_token",
    "message": "Token is invalid or expired"
  }
}

Refresh Token

Used to obtain a new access and refresh token pair using a refresh token.

Request:

{
  "id": "3",
  "type": "auth.refresh",
  "data": {
    "refresh_token": "xyz789..."
  }
}

Success:

{
  "id": "3",
  "type": "auth.refresh",
  "ok": true,
  "data": {
    "access_token": "newtoken123...",
    "refresh_token": "newrefreshtoken456...",
    "expires_in": 3600,
    "session": {
      "session_id": "s_0123...",
      "client": {"device_name": "Alice's laptop", "platform": "macOS", "name": "Mandarin Web", "version": "0.1.0"},
      "created_at": "2026-08-05T10:00:00Z",
      "last_active_at": "2026-08-05T11:00:00Z",
      "expires_at": "2026-09-04T11:00:00Z",
      "current": false,
      "online": false,
      "push": false
    }
  }
}

A successful refresh rotates both tokens, so the client must replace its stored access and refresh tokens with this response. The refresh token it was made with stays good until the client uses what it got — authenticates with the new access token (auth.token), or refreshes with the new refresh token — as the answer may be lost on the way and leave the client holding the old one: a refresh with it meanwhile rotates the tokens again, and the pair given before stops working. Only the pair given last is ever good; a refresh token older than the one last refreshed with is refused. Concurrent refreshes are serialized. On an authenticated connection, the refresh token must belong to that same persistent session.

Failure:

{
  "id": "3",
  "type": "auth.refresh",
  "ok": false,
  "error": {
    "code": "invalid_token",
    "message": "Refresh token is invalid or expired"
  }
}

Logout

Revokes the current persistent session and returns every live connection using it to the unauthenticated state. Connections stay open: protected commands are rejected with not_authenticated until the client authenticates again.

If the user can no longer be reached afterwards — no active connection and no push registration are left — their followers receive a presence.offline event.

Request:

{
  "id": "4",
  "type": "auth.logout"
}

Success:

{"id": "4", "type": "auth.logout", "ok": true}

Token Security