Topics and realtime
Tokens for app realtime (live rows per user, broadcast and presence channels over WebSocket), and topics and row-changed feeds over server-sent events.
On this page
Base URL: your app's origin ($APP_ORIGIN): the dev site (https://dev-<slug>.apps.appbricx.com), the published host or a custom domain. Errors answer { ok: false, error: { code, message } } unless noted. The host decides the environment: the dev site reads and writes the dev database, the published host and custom domains production's.
Authentication
- App key + data marker
- A project key (dpk_c_… in browsers, bound to the app's origins and sent with Origin; dpk_s_… on servers) plus the data-API marker header. With an app session (the header, or the appbricx_app_session cookie) the call acts as that user under row-level security; without one it acts as a signed-out visitor. A server key may assert x-appbricx-app-user and x-appbricx-app-role.
Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)
Endpoints
Publish on a topic
POST/__appbricx/topics/:name/publish
Publish a message to every subscriber of a topic. app:* and free-form names take any authenticated caller; row-changed:* takes server keys only.
- Auth
- App key + data marker:
Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)
Path parameters
| Name | Type | Description |
|---|---|---|
:namerequired | string | Letters, digits and _ : . - (e.g. app:kitchen). |
Body
| Name | Type | Description |
|---|---|---|
payloadoptional | any JSON | The message. |
Response
{
"ok": true
}Errors
| Status | Code | When |
|---|---|---|
| 401 | UNAUTHORIZED | Missing or invalid Authorization bearer. |
| 400 | PARAMS_INVALID | The x-appbricx-data-api: 1 header is missing. |
| 403 | ORIGIN_NOT_ALLOWED | A browser key used from an origin it isn't bound to (ORIGIN_REQUIRED when there is no Origin at all). |
| 429 | RATE_LIMITED | The project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user). |
| 400 | TOPIC_INVALID | Bad name. |
| 403 | TOPIC_PUBLISH_FORBIDDEN | row-changed:* without a server key. |
curl -X POST "$APP_ORIGIN/__appbricx/topics/app:kitchen/publish" \
-H "Authorization: Bearer $APP_KEY" \
-H "x-appbricx-data-api: 1" \
-H "x-appbricx-app-session: $APP_SESSION" \
-H "content-type: application/json" \
-d '{"payload":{"order":"1042","status":"ready"}}'const res = await fetch(`${APP_ORIGIN}/__appbricx/topics/app:kitchen/publish`, {
method: "POST",
headers: {
Authorization: `Bearer ${APP_KEY}`,
"x-appbricx-data-api": "1",
"x-appbricx-app-session": APP_SESSION,
"content-type": "application/json",
},
body: JSON.stringify({
"payload": {
"order": "1042",
"status": "ready"
}
}),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error?.message ?? data.error ?? res.statusText);Source: internal/svc/runtime/data/topics_http.go:35
Subscribe to a topic
GET/__appbricx/topics/:name/subscribe
A server-sent event stream: event: message with the JSON payload, and a ping every 15 seconds. Browsers' EventSource can't set headers, so this route also takes access_token and app_session as query parameters and doesn't need the data marker.
- Auth
- App key + data marker:
Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)
Path parameters
| Name | Type | Description |
|---|---|---|
:namerequired | string | app:*, a free-form name, or row-changed:<table> (needs read access to the table). |
Query parameters
| Name | Type | Description |
|---|---|---|
access_tokenoptional | string | The app key, for EventSource. |
app_sessionoptional | string | The user's session, for EventSource. |
Response
text/event-stream.
event: message
data: {"event":"created","table":"tickets"}
event: ping
data:Errors
| Status | Code | When |
|---|---|---|
| 401 | UNAUTHORIZED | Missing or invalid Authorization bearer. |
| 429 | RATE_LIMITED | The project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user). |
| 403 | TOPIC_FORBIDDEN | Unknown namespace, or no read access to the table. |
curl -N "$APP_ORIGIN/__appbricx/topics/row-changed:tickets/subscribe" \
-H "Authorization: Bearer $APP_KEY" \
-H "x-appbricx-data-api: 1" \
-H "x-appbricx-app-session: $APP_SESSION"const res = await fetch(`${APP_ORIGIN}/__appbricx/topics/row-changed:tickets/subscribe`, {
headers: {
Authorization: `Bearer ${APP_KEY}`,
"x-appbricx-data-api": "1",
"x-appbricx-app-session": APP_SESSION,
},
});
const reader = res.body!.getReader(); // text/event-streamSource: internal/svc/runtime/data/topics_http.go:36
Realtime connection token
POST/__appbricx/realtime/connect
A connection token for the app's realtime socket (WebSocket at url, Centrifuge protocol v2, JSON), plus what this caller may follow. Signed-in callers connect as their app user; a signed-out caller gets an anon: id only when the app has a public channel or a live table the guest role may read. The SDK (app.realtime) calls this for you.
- Auth
- App key + data marker:
Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)
Body
| Name | Type | Description |
|---|---|---|
anonoptional | string | A previous connection token of this visitor: keeps the same anon: id across a refresh. |
Response
The token lives 15 minutes; the client refreshes it. live and channels list only what this caller may use.
{
"ok": true,
"url": "/__appbricx/realtime/ws",
"token": "eyJhbGciOiJIUzI1NiIs…",
"tenant": "6f1c…-….prod",
"user": "1b2c…",
"expires_in": 900,
"live": [
"message"
],
"channels": [
{
"name": "room",
"presence": true,
"keyed": true,
"publish": true
}
]
}Errors
| Status | Code | When |
|---|---|---|
| 401 | UNAUTHORIZED | Missing or invalid Authorization bearer. |
| 400 | PARAMS_INVALID | The x-appbricx-data-api: 1 header is missing. |
| 403 | ORIGIN_NOT_ALLOWED | A browser key used from an origin it isn't bound to (ORIGIN_REQUIRED when there is no Origin at all). |
| 429 | RATE_LIMITED | The project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user). |
| 401 | SIGN_IN_REQUIRED | Signed out, and nothing in the app is open to guests. |
| 503 | REALTIME_DISABLED | The server has no realtime service configured, or the app declares no realtime. |
curl -X POST "$APP_ORIGIN/__appbricx/realtime/connect" \
-H "Authorization: Bearer $APP_KEY" \
-H "x-appbricx-data-api: 1" \
-H "x-appbricx-app-session: $APP_SESSION" \
-H "content-type: application/json" \
-d '{}'const res = await fetch(`${APP_ORIGIN}/__appbricx/realtime/connect`, {
method: "POST",
headers: {
Authorization: `Bearer ${APP_KEY}`,
"x-appbricx-data-api": "1",
"x-appbricx-app-session": APP_SESSION,
"content-type": "application/json",
},
body: JSON.stringify({}),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error?.message ?? data.error ?? res.statusText);Source: internal/svc/runtime/rtauth/rtauth.go:83
Realtime subscription token
POST/__appbricx/realtime/subscribe
A token for one channel, checked against app.config.ts realtime and the caller's access every time. live:<table> needs the table listed in realtime.live and read access for the caller's role, and registers the caller's identity for that table's change feed. <name> or <name>:<key> is a channel of realtime.channels: public, signed_in or listed roles; a per-row channel needs read access to row <key>. The token says whether the caller may send (publish) and sees presence.
- Auth
- App key + data marker:
Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)
Body
| Name | Type | Description |
|---|---|---|
channelrequired | string | live:<table>, <channel> or <channel>:<key>. |
connection_tokenrequired | string | The caller's own token from realtime/connect. |
Response
{
"ok": true,
"token": "eyJhbGciOiJIUzI1NiIs…",
"channel": "t:6f1c…-….prod:ch:room:3f2a…",
"expires_in": 900
}Errors
| Status | Code | When |
|---|---|---|
| 401 | UNAUTHORIZED | Missing or invalid Authorization bearer. |
| 400 | PARAMS_INVALID | The x-appbricx-data-api: 1 header is missing. |
| 403 | ORIGIN_NOT_ALLOWED | A browser key used from an origin it isn't bound to (ORIGIN_REQUIRED when there is no Origin at all). |
| 429 | RATE_LIMITED | The project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user). |
| 400 | PARAMS_INVALID | No channel, or a per-row channel without its key. |
| 401 | TOKEN_INVALID | The connection token is missing, expired, forged, or another user's or app's. |
| 403 | CHANNEL_FORBIDDEN | The caller's role may not read the table or join the channel, or can't read that row. |
| 404 | CHANNEL_UNKNOWN | Not a live table or channel of this app. |
| 503 | REALTIME_DISABLED | No realtime on this server or in this app. |
curl -N -X POST "$APP_ORIGIN/__appbricx/realtime/subscribe" \
-H "Authorization: Bearer $APP_KEY" \
-H "x-appbricx-data-api: 1" \
-H "x-appbricx-app-session: $APP_SESSION" \
-H "content-type: application/json" \
-d '{"channel":"room:3f2a…","connection_token":"eyJhbGciOiJIUzI1NiIs…"}'const res = await fetch(`${APP_ORIGIN}/__appbricx/realtime/subscribe`, {
method: "POST",
headers: {
Authorization: `Bearer ${APP_KEY}`,
"x-appbricx-data-api": "1",
"x-appbricx-app-session": APP_SESSION,
"content-type": "application/json",
},
body: JSON.stringify({
"channel": "room:3f2a…",
"connection_token": "eyJhbGciOiJIUzI1NiIs…"
}),
});
const reader = res.body!.getReader(); // text/event-streamSource: internal/svc/runtime/rtauth/rtauth.go:84