# Get Started

## With Agents

Paste a prompt like this into your agent:

Prompt for your agent (Polls):

```text
Build me a live polling app with Rindle. Use the skill https://www.rindle.sh/skill.md.

One page creates a poll with a question and 2–5 options
and gives me a share link. Anyone with the link sees the poll, votes once, and watches the
counts update live, without refreshing. Render the pages on the server so the poll shows up on
first load, then stays live.
```

Prompt for your agent (Chess):

```text
Build me multiplayer chess with Rindle. Use the skill https://www.rindle.sh/skill.md.

I create a game and get a link to send a friend, and we
each see the board update the moment the other moves. The server enforces the rules: it accepts a
move only if it is legal and it is that player's turn, so nobody can cheat by calling the API
directly. I can also choose to play against an AI instead, which moves through the same server
rules. Show check, checkmate, and the move list.
```

Prompt for your agent (Team chat):

```text
Build me a Slack-style team chat with Rindle. Use the skill https://www.rindle.sh/skill.md.

Channels, messages, threads, and emoji reactions,
all updating live, with an unread count on each channel. Private channels and direct messages are
visible only to their members, and the server checks membership on every read and write.
```

Prompt for your agent (Auction):

```text
Build me a live auction site with Rindle. Use the skill https://www.rindle.sh/skill.md.

Sellers list an item with a starting price and an end
time, and bidders watch the price and bid history update live. The server decides which bids
count: a bid must beat the current high bid and arrive before the auction closes, so two people
bidding at once can't both win.
```

Prompt for your agent (Trivia):

```text
Build me a live trivia game with Rindle,
like Kahoot. Use the skill https://www.rindle.sh/skill.md.

A host creates a quiz and shares a join
code, and players join from their phones. The host moves through the questions, everyone answers
against a countdown, and the leaderboard updates live. The server scores the answers, and players
never receive the correct answer before the question closes.
```

Agents can also read these docs as [plain Markdown](https://www.rindle.sh/agents.md).

## By Hand

First, create a database at [cloud.rindle.sh](https://cloud.rindle.sh). It gives you a URL and an
API token. Save both in `.env`:

```bash
RINDLE_URL=https://app-7f3k2q.rindle.cloud
RINDLE_API_TOKEN=3c9f0e…
```

## TypeScript

Rindle’s core API is [HTTP-based](#other-languages), but a thin helper library is provided for TypeScript. Install it
into your project:

```bash
npm install @rindle/api
```

Then save this as `app.ts`:

```typescript
import { createClient } from "@rindle/api";

const db = createClient({
  url: process.env.RINDLE_URL,
  apiToken: process.env.RINDLE_API_TOKEN,
});

// Create the tables. Skip this migration if it has already run.
await db.migrate("0001_init", `
  CREATE TABLE issue (
    id TEXT PRIMARY KEY,
    title TEXT,
    closed INTEGER
  );
  CREATE TABLE comment (
    id TEXT PRIMARY KEY,
    issue_id TEXT,
    body TEXT
  );
`);

// Get open issues with their 10 newest comments.
type Issue = {
  id: string;
  title: string;
  comments: { body: string }[];
};
const open = await db.query<Issue>(`
  SELECT * FROM issue WHERE closed = 0 ORDER BY id DESC
  WITH comments OF comment ON issue_id = id (ORDER BY id DESC LIMIT 10)
`);

// Print the results now and whenever they change.
open.subscribe((rows) => {
  console.clear();
  console.table(rows.map(({ id, title, comments }) => ({
    id, title, comments: comments.length, newest: comments[0]?.body,
  })));
});

// Insert an issue with a unique ID so multiple copies of this script
// can write to the same database.
const issueId = crypto.randomUUID();
await db.execute({
  sql: `INSERT INTO issue (id, title, closed) VALUES (?, ?, ?)`,
  params: [issueId, "first", 0],
});

const comments = Array.from({ length: 1000 }, (_, i) => ({
  sql: `INSERT INTO comment (id, issue_id, body) VALUES (?, ?, ?)`,
  params: [`${issueId}-${String(i).padStart(4, "0")}`, issueId, `comment ${i}`],
}));

// Insert 1,000 comments in batches of 20.
// The query still returns only the 10 newest comments per issue.
for (let i = 0; i < comments.length; i += 20) {
  await db.batch(comments.slice(i, i + 20));
}
```

Run it in two terminals, one after the other:

```bash
node --env-file=.env app.ts
```

The first copy's table updates live as the second one writes, and its view never holds more than ten comments.

## Other Languages

Any language that can make HTTP requests can use Rindle. Here’s an example using Bash. Save to
`app.sh`. You will need [jq](https://jqlang.org) and [websocat](https://github.com/vi/websocat).

```bash
#!/usr/bin/env bash
set -euo pipefail
source .env

# POST to a Rindle route with the API token.
api() {
  curl -sS -X POST "$RINDLE_URL/db/$1" \
    -H "Authorization: Bearer $RINDLE_API_TOKEN" \
    -H 'Content-Type: application/json' "${@:2}"
}

# Applied once, by name. Re-running is a no-op.
api migrate -d '{
  "id": "0001_init",
  "sql": "CREATE TABLE issue (id TEXT PRIMARY KEY, title TEXT, closed INTEGER); CREATE TABLE comment (id TEXT PRIMARY KEY, issue_id TEXT, body TEXT);"
}' > /dev/null

# A client is one consumer of results. Get it a stream token, then
# subscribe it to a query.
client="c_$RANDOM"
token=$(api stream-auth -H "Rindle-Client: $client" | jq -r .streamToken)

api subscribe -H "Rindle-Client: $client" -H "Rindle-Stream-Token: $token" -d '{
  "ops": [{
    "op": "add",
    "id": 1,
    "sql": "SELECT * FROM issue WHERE closed = ? ORDER BY id DESC",
    "params": [0]
  }]
}' > /dev/null

# Results arrive on the client's stream, a WebSocket: the whole result
# first, then a change after every commit that affects it. A background
# job's stdin is empty, so -n stops websocat closing the stream at EOF.
websocat -n "${RINDLE_URL/http/ws}/db/stream?streamToken=$token" &
trap 'kill $!' EXIT

# Plain SQL writes. Every copy writes to the same database, so give
# each row an id of its own.
for i in 1 2 3 4 5; do
  api execute -d '{
    "statement": {
      "sql": "INSERT INTO issue (id, title, closed) VALUES (?, ?, ?)",
      "args": ["'"$client-$i"'", "issue '"$i"'", 0]
    }
  }' > /dev/null
  sleep 1
done

wait
```

Run it in two terminals, one after the other:

```bash
bash app.sh
```

The first copy prints each row as the second one writes it. See the [HTTP API](https://www.rindle.sh/http-api.md) for
every route.
