# 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](https://subetha.us/console/) with
   [devicebasedauth.org](https://devicebasedauth.org) and pick a **handle**. Your topics live
   under `@handle/…`.
2. Top up your balance (minimum $5). See [Pricing & billing](pricing.md).
3. Create a token under **Tokens**. For scripts, scope it narrowly and give it a daily cap (see
   [Tokens & policies](tokens.md)).

```bash
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:

```bash title="samples/curl/local-dev.sh"
#!/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

```bash title="samples/curl/publish.sh"
#!/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](concepts.md#topics)).

## 3. Subscribe

In another terminal:

```bash title="samples/curl/subscribe-json.sh"
#!/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.

```ts title="samples/ts/publish.ts"
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}`);
```

```ts title="samples/ts/subscribe.ts"
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 [Python, Swift, Kotlin, Dart, and React Native SDKs, a Rust core, a CLI, and drop-in web components](sdks.md).

## Built-in tools

- [Pricing calculator](/tools/pricing.html): a month of usage, priced from the same table the
  relay charges from, and the top-up that covers it.
- [Route simulator](/tools/simulator.html): which transports a message would use, in what order,
  and at what cost. It runs the relay's planner in your browser, so it needs no account.
- In the [console](https://subetha.us/console/): **Copy as** curl, JavaScript, Python, Swift,
  Kotlin, or Dart for any message you compose; **Simulate route & cost** (a dry run against your
  real topic); a token builder; a trace viewer; and an **API explorer** that sends any operation
  in the [OpenAPI document](/openapi.yaml) with your credential.

## Next

- Deliver to phones, email, Slack, or SMS: [Transports](transports.md).
- Control cost and routing per message: [Publishing](publishing.md#routing-and-cost).
- Let an AI agent publish and wait for replies: [Agents](agents.md).
