---
slug: 80-cookbook
title: Cookbook
description: Copy-paste Synsema patterns — input validation with partial results, plus pointers to the full patterns (HTTP API, CRUD, LLM op, agent tools, secrets).
example_ids: [cookbook]
---

# Cookbook

Copy-paste patterns. Each is complete and doctested elsewhere — start here, then follow the link.

## Validate input, keep partial results

Validate a payload into a typed value; sum the valid ones and skip the bad ones with `try/recover`:

```synsema
-- Doc cookbook: validate a payload into a typed value, and sum valid ones with
-- partial results (try/recover skips the bad ones). A copy-paste-ready pattern.
intent: "doc cookbook: validate + partial results"

type Order
    id: number
    total: number

task validate_order(payload)
    when not contains(payload, "total")
        raise("missing total")
    when (payload["total"]) < 0
        raise("total must be >= 0")
    give Order(payload["id"], payload["total"])

task bad_order()
    give validate_order({"id": 2, "total": 0 - 5})

task safe_total(payloads)
    let total be 0
    each p in payloads
        try
            let o be validate_order(p)
            set total to total + (total of o)
        recover err
            log "skipped: " + err          -- partial results: skip the invalid ones
    give total

print("safe_total of mixed orders → " + text(safe_total([{"id": 1, "total": 10}, {"id": 2, "total": 0 - 1}, {"id": 3, "total": 5}])))

test "validate accepts good input and rejects bad"
    assert_eq(total of validate_order({"id": 1, "total": 100}), 100)
    assert_error(bad_order)

test "partial results: invalid orders are skipped"
    assert_eq(safe_total([{"id": 1, "total": 10}, {"id": 2, "total": 0 - 1}, {"id": 3, "total": 5}]), 15)
```

## The other patterns (each fully shown on its page)

- **A styled page from a layout + components** — `{ layout }`, named slots, `{ include … with {props} }`, inline CSS in a `{ raw }` block → **[Build a website](/en/0.6.x/41a-build-a-website)**.
- **A classic form (no JS)** — `<form method="post">` + `form of request` (urlencoded/multipart, file uploads as exact bytes) → **[Build a website](/en/0.6.x/41a-build-a-website)**.
- **Custom 404/500 pages with honest statuses** — `errors with <task>`; HTML for browsers, JSON for agents, `redirect` for a 401 → **[HTTP server](/en/0.6.x/40-serve)**.
- **Safe data into an inline `<script>`** — `{ raw json_for_script(x) }` (never `json_encode` there) → **[Frontend](/en/0.6.x/41-frontend)**.
- **Routes split into modules** — `export routes` + `mount alias.group at "/prefix"` → **[Modules](/en/0.6.x/14-modules)**.
- **HTTP API with auth + validation** — `auth with`, `expect body {…}`, the response helpers → **[HTTP server](/en/0.6.x/40-serve)**.
- **CRUD over SQL** — `db_open` / `sql_exec` / `sql` (SQLite in-memory in the doctest) → **[SQL, Mongo & Redis](/en/0.6.x/33-sql-mongo-redis)**.
- **One LLM operation** — `generate "…" given X`, branch on `llm_available()` → **[LLM primitives](/en/0.6.x/50-llm-primitives)**.
- **An agent with tools** — the `llm_step` + `call_tool` allow-list loop → **[Tool calling](/en/0.6.x/51-llm-tool-calling)**.
- **A secret in any header** — `{"x-api-key": secret("KEY")}` / `bearer(...)`, materialized only at the socket → **[Secrets](/en/0.6.x/21-secrets)**.
- **Read/write a file with capabilities** — `require file(...)` + `read_file`/`write_file` → **[Files & I/O](/en/0.6.x/30-io-files)**.
- **The on-chain loop** — read (nonce/fees/UTXOs) → build → `confirm` the numbers → sign (the one gated door) → send → bounded wait; EVM (incl. Base/Arbitrum/Optimism L2s) → **[Blockchain](/en/0.6.x/38-blockchain)**, Bitcoin (every satoshi accounted for) → **[Bitcoin](/en/0.6.x/38a-bitcoin)**.
- **Cold custody with PSBT** — the agent prepares and audits, a human signs on their hardware wallet, the agent finalizes and broadcasts (the key never touches the agent's machine) → **[Bitcoin](/en/0.6.x/38a-bitcoin)**.
- **A fund transfer behind a human gate** — `confirm "… fee X?" within 15m` BEFORE `secp256k1_sign`, composed under `serve` with cron monitoring and a negotiated `chart()` → **[Blockchain](/en/0.6.x/38-blockchain)** (the flagship route).
- **Thousands of live feeds in one thread** — the `while` + `ws_select` event loop, opt-in reconnect + keepalive, `parallel_map` fan-out → **[WebSocket](/en/0.6.x/39-websocket)**.

Every snippet on those pages is doctested against this version, so an LLM can copy one and it works.
