SubEthaConsole →

Getting started

You need an account with a little credit, and a token. Then publishing is one HTTP request.

1. Get a token

  1. Sign in at subetha.us/console and pick a handle. Your topics live under @handle/….
  2. Top up your balance (minimum $5). See Pricing & billing.
  3. Create a token under Tokens. For scripts, scope it narrowly and give it a daily cap (see Tokens & policies).
export SUBETHA_TOKEN=se_xxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Prefer to try it locally first? Run the relay on your machine and get a funded account without signing up:

#!/usr/bin/env bash
# Run the relay locally and get a funded account and admin token.
set -euo pipefail

# In one terminal (from the repository root):
#   pnpm install && pnpm --filter @subetha/sdk build && pnpm --filter @subetha/site build
#   pnpm --filter @subetha/api exec wrangler d1 migrations apply DB --local
#   pnpm --filter @subetha/api dev          # http://localhost:8787

# In another:
export SUBETHA_URL=http://localhost:8787
SUBETHA_TOKEN=$(curl -sS -X POST "$SUBETHA_URL/v1/dev/bootstrap" \
  -d '{"secret":"local-dev-only","handle":"me","credit_usd":5}' | jq -r .token)
export SUBETHA_TOKEN

curl -sS -H "Authorization: Bearer $SUBETHA_TOKEN" -d "hello from localhost" "$SUBETHA_URL/@me/test"
curl -sS -H "Authorization: Bearer $SUBETHA_TOKEN" "$SUBETHA_URL/@me/test/json?poll=1"

2. Publish

#!/usr/bin/env bash
# Publish a message: the body is the message text.
set -euo pipefail
SUBETHA_URL="${SUBETHA_URL:-https://subetha.us}"

curl -sS -H "Authorization: Bearer $SUBETHA_TOKEN" \
  -d "Backup done" \
  "$SUBETHA_URL/@me/backups"

The response is the stored message, with its id. The X-Cost-Micros response header says what you were charged, in micro-dollars (µ$; 1,000,000 µ$ = $1).

Topics are created on first publish in your own namespace (@yourhandle/anything) or as secret t_… topics (see Concepts).

3. Subscribe

In another terminal:

#!/usr/bin/env bash
# Stream messages as newline-delimited JSON (one envelope per line).
# The first line is an "open" event; "keepalive" events arrive periodically.
set -euo pipefail
SUBETHA_URL="${SUBETHA_URL:-https://subetha.us}"

curl -sSN -H "Authorization: Bearer $SUBETHA_TOKEN" \
  "$SUBETHA_URL/@me/doorbell/json"

# Pipe it through jq to print just the messages:
#   curl -sSN -H "Authorization: Bearer $SUBETHA_TOKEN" "$SUBETHA_URL/@me/doorbell/json" \
#     | jq --unbuffered -r 'select(.event == null or .event == "message") | .message'

Publish again and the message appears on the stream immediately. Or open https://subetha.us/@me/backups in a browser: topic URLs open in the web console.

4. Use an SDK

The TypeScript SDK (npm i @subetha/sdk) handles reconnects, dedupe, acks, signatures, and encryption. It runs in browsers, Node 18+, Deno, Bun, and Workers.

const se = new SubEtha({ baseUrl, token, fetch });
const msg = await se.publish(topic, "Backup finished in 4m12s", {
  title: "nightly-backup",
  priority: 4,
  tags: ["backup", "white_check_mark"],
  click: "https://example.com/backups/latest",
});
console.log(`published ${msg.id} to ${msg.topic}`);
const first = new Promise<Envelope>((resolve, reject) => {
  const sub = se.subscribe(topic, (m) => {
    console.log(`${m.title ?? ""} ${m.message}`);
    sub.close();
    resolve(m);
  }, {
    transport: "stream",   // "auto" (the default) prefers a WebSocket when there is one
    priority: [3, 4, 5],   // filtered on the server
    onOpen: () => void se.publish(topic, "hello, subscriber").catch(reject),
    onError: (e) => console.warn("reconnecting:", e.message),
  });
});
const message = await first;

There are also a Python SDK, a Rust core, a CLI, and drop-in web components.

Built-in tools

Next

Edit this page · Markdown