Developers

Midrak Cloud API

One account, many devices. This is the contract the Midrak desktop app, the phone app and nodes use. Base URL: https://ai.midrak.net/api/v1. Everything is JSON over HTTPS; the relay is a WebSocket.

Overview

AreaWhat it is for
/auth/*, /account/*Browser sign-in (cookie session): email and password, Google, Microsoft, verification, recovery, profile. Used by the website.
/app/*How an app gets an account session: authorization code with PKCE, or a device code. Returns a bearer token pair bound to a device.
/devicesThe machines and phones of the account, who is online, and where a device can be reached on its own network.
/relayA WebSocket (and an HTTP fallback) that carries messages between devices of one account.
/models, /chat/completionsThe Midrak Cloud models. Sadim is in training: the shapes exist, nothing can be called yet.

Apps authenticate with Authorization: Bearer mka_…. The website uses a cookie and sends X-Midrak-Client: web on writes; an app never needs cookies.

Errors and limits

Every error is {"error": {"code": "…", "message": "…"}} with a matching HTTP status. Branch on code, show message to people.

StatusCodes
400 / 422invalid_request, invalid_grant, invalid_redirect_uri, invalid_code, password_*
401not_signed_in, invalid_token, invalid_credentials
403email_not_verified, bad_origin, registration_closed
409 / 413too_many_devices (25 per account), email_taken, too_large
429rate_limited (with Retry-After), account_locked

The recommended flow is the authorization code with PKCE: the app opens the browser, the person signs in and presses Allow, and the page returns to the app.

1. Open the consent page

https://ai.midrak.net/authorize
  ?client_id=midrak-desktop            # midrak-desktop | midrak-mobile | midrak-node | midrak-cli
  &redirect_uri=http://127.0.0.1:51234/api/cloud/callback
  &state=<random>
  &code_challenge=<base64url(sha256(verifier))>
  &code_challenge_method=S256
  &device_id=g-3f9c…               # stable, 8–80 chars [A-Za-z0-9_.:-]
  &device_name=Ali%27s%20PC&device_kind=desktop&platform=linux&app_version=0.1.0

redirect_uri may be http://127.0.0.1:<port>/… (also localhost, [::1]) or, for the phone and desktop deep link, midrak://…. Nothing else is accepted.

2. Receive the code

On Allow the page is redirected to redirect_uri?code=mac_…&state=…; on Cancel, to redirect_uri?error=access_denied&state=…. Check state. The code lives two minutes and works once; a wrong guess burns it.

3. Trade it

POST /api/v1/app/token
{"grant_type":"authorization_code","client_id":"midrak-desktop","code":"mac_…",
 "code_verifier":"<the verifier>","redirect_uri":"<the same one>"}

200 {"token_type":"Bearer","access_token":"mka_…","expires_in":3600,
     "refresh_token":"mkr_…","refresh_expires_in":7776000,
     "user":{…},"device":{…}}

Device code (no browser on the device)

POST /api/v1/app/device/code   {"client_id":"midrak-node","device":{"id":"n-…","name":"Rack 2","kind":"node"}}
→ {"device_code":"mdc_…","user_code":"ABCD-EFGH","verification_uri":"https://ai.midrak.net/link","interval":5,"expires_in":600}

# show user_code; the person enters it at /link on any signed-in browser; meanwhile poll:
POST /api/v1/app/token   {"grant_type":"device_code","device_code":"mdc_…"}
→ 400 authorization_pending | slow_down | access_denied | expired_token, then 200 with the token pair

Tokens

Access tokens last an hour. Refresh tokens last 90 days and rotate: each use returns a new pair.

POST /api/v1/app/token   {"grant_type":"refresh_token","refresh_token":"mkr_…"}

Using a refresh token twice means one copy was stolen: the whole family is revoked and the app must sign in again (401 invalid_grant). A password change or reset also ends every token. POST /app/logout ends this device's tokens; GET /app/me returns the profile.

Devices

RequestDoes
GET/devicesAll devices of the account with status (online, recent, offline), roles, capabilities, endpoints, current.
PUT/devices/selfThis device announces itself (bearer only), see below.
POST/devices/self/heartbeatKeeps recent alive when no socket is open.
PATCH/devices/{id}Rename.
DELETE/devices/{id}Remove: its tokens end, its socket closes.
PUT /api/v1/devices/self
{"name":"Ali's PC","roles":["gateway","node"],
 "capabilities":{"tools":["exec","files","browser"],"gpu":false},
 "endpoints":{"lan":[{"host":"192.168.1.20","node_port":8766,"pair_port":8767}],
              "gateway_id":"g-…","ca_fingerprint":"…","pairing_open":false}}

roles is any of gateway, node, mobile, cli. Only private, loopback and link-local addresses are kept in endpoints.lan; a public address is dropped on purpose.

The relay

Connect to wss://ai.midrak.net/api/v1/relay with the bearer header, or send {"t":"auth","token":"mka_…"} as the first frame within ten seconds. Frames are JSON objects of at most 256 KiB. The server routes and never inspects data; it never crosses accounts. Send a fresh auth frame before the access token expires to keep the socket.

FrameMeaning
{"t":"ready","device":…,"peers":[…],"queued":N}Authenticated. peers are the other devices with their online flag. Held messages follow.
{"t":"send","id","to","topic","data","queue","ttl"}Message to a device id, or "*" for every other online device. With queue:true an offline target keeps it (up to 200 messages, 64 KiB each, ttl seconds, at most 7 days).
{"t":"msg","id","from","topic","data","ts","queued"}A delivered message.
{"t":"ack","id","delivered","queued"}Answer to send.
{"t":"rpc","id","to","method","params","timeout"}Ask a device to do something and wait. Only that device may answer.
{"t":"rpc.res","id","ok","result"|"error"}The answer; error.code may be device_offline or timeout.
{"t":"presence","device","online"}A peer connected or left.
{"t":"peers"}, {"t":"ping"}List peers; keep-alive (pong).

Without a socket, POST /relay/send takes the same fields, and GET /relay/inbox / DELETE /relay/inbox/{id} read and acknowledge held messages. Topics match [A-Za-z0-9_.:/-]{1,64}. The server sees every payload unless the devices encrypt it themselves: use the device public_key you registered.

Models

GET /api/v1/models            → {"object":"list","data":[{"id":"sadim","status":"training","available":false,…}]}
POST /api/v1/chat/completions → 503 {"error":{"code":"model_in_training",…}}

Account endpoints (website)

RequestDoes
GET /auth/providersWhich sign-in methods are switched on.
GET /auth/session{"signed_in":bool,"user":…}
POST /auth/register · /auth/login · /auth/logoutEmail and password.
POST /auth/verify · /auth/resend-verificationSix-digit code or link token.
POST /auth/forgot · /auth/resetRecovery by emailed code or link; always answers ok so addresses cannot be probed.
GET /auth/oauth/{google|microsoft}/startRedirects to the provider (?next=, ?mode=link when signed in).
PATCH /account · POST /account/avatar · POST /account/passwordProfile, picture, password.
GET /account/sessions · DELETE /account/identities/{provider} · DELETE /accountBrowser sessions, unlink a provider, delete the account.

Security notes

  • Passwords are stored with scrypt (N=215); eight wrong tries lock an account for 15 minutes, doubling each time.
  • Tokens, sessions, codes and device flows are stored only as SHA-256 hashes.
  • Google sign-in may join an existing account by its verified email. Microsoft never does: the person links it from the account page while signed in.
  • OAuth uses state bound to the browser by a cookie, and PKCE where the provider supports it.