# Examples

> Runnable s3nd examples: a Next.js notes app that moves its IndexedDB between browsers with a code, and a Node script that round-trips a snapshot and proves the conditional writes. Plus the worked guides in the documentation.

Canonical: https://www.s3nd.sh/examples · Markdown: https://www.s3nd.sh/examples.md · Français: https://www.s3nd.sh/fr/examples

Two examples you can clone and run, and the worked guides in the documentation. Both examples point at a local MinIO by default, so the real code path runs on your laptop with nothing to sign up for.

**a bucket on your laptop**
```sh
docker run -p 9000:9000 -p 9001:9001 \
  -e MINIO_ROOT_USER=minioadmin -e MINIO_ROOT_PASSWORD=minioadmin \
  quay.io/minio/minio server /data --console-address ":9001"
```

## IndexedDB sync (examples/indexeddb-sync)
A notes app that keeps everything in IndexedDB and moves it to another device with a code. Open it in two browsers: the second one is empty until the code carries the database across.

- A complete exportDatabase / importDatabase pair against a real object store
- The code shown grouped in fours, and normalized on the server with or without the spaces
- Nothing imported before the user confirms what they are about to restore
- ifAbsent on write, a one-hour expiry, and the DELETE after a successful import
- A single transaction on import, so a failure halfway leaves the previous data intact

Source on GitHub: https://github.com/AbderrahmaneMouzoune/s3nd/tree/main/examples/indexeddb-sync

```sh
cp .env.example .env.local
bun install
bun run dev   # localhost:3200, in two different browsers
```

## Node round trip (examples/node-script)
A whole transfer in one file: write a snapshot under a code, read it back through a sloppily typed version of that code, prove the conditional writes work, then burn it. A smoke test for a new bucket or provider.

- The compression ratio on a realistic dump, printed as the headline
- A code typed in lowercase with a dash, resolved by codes.normalize()
- ifAbsent rejecting a second claim, ifMatch rejecting a stale write
- destroy() in a finally, so the script exits without waiting on keep-alive sockets

Source on GitHub: https://github.com/AbderrahmaneMouzoune/s3nd/tree/main/examples/node-script

```sh
cp .env.example .env
bun install
node --env-file=.env --import tsx src/round-trip.ts
```
**expected output**
```text
code      ZZWMBSTD
stored    1826 bytes gzipped, from 22991 raw (12.6x)
typed     "zzwm-bstd" → ZZWMBSTD
restored  200 notes from node-script, written 2026-08-27T14:29:20.442Z
identical true
claim     rejected as PRECONDITION_FAILED
ifMatch   rejected as PRECONDITION_FAILED
burned    gone
```

## Worked examples in the documentation.
Each guide is copy-pasteable end to end: the store, the routes, the client half, and the thing that goes wrong if you skip a step.

- [Move to a new device: Two routes, two calls, a confirmation step.](https://doc.s3nd.sh/docs/use-cases/new-device)
- [Continuous backup: One snapshot per account, conditional writes.](https://doc.s3nd.sh/docs/use-cases/continuous-backup)
- [End-to-end encrypted sync: WebCrypto in the browser, ciphertext in the bucket.](https://doc.s3nd.sh/docs/use-cases/encrypted-sync)
- [Attachments beside the data: Blobs as objects, references in the snapshot.](https://doc.s3nd.sh/docs/use-cases/attachments)
- [Without a server: The CLI straight to Cloudflare R2, end to end.](https://doc.s3nd.sh/docs/no-server)
- [Setting up a server: Routes, authorization, expiry, CORS, smoke test.](https://doc.s3nd.sh/docs/server)
- [Testing: A bucket in memory: no credentials, no network.](https://doc.s3nd.sh/docs/testing)

## No clone required.
1. Start MinIO with the command above and create a bucket.
2. Write a starter configuration and check it.
3. Move a file, and get it back with a sloppily typed code.

```sh
npx @s3nd/cli init --provider minio --bucket transfers
npx @s3nd/cli doctor
npx @s3nd/cli put ./anything.pdf
npx @s3nd/cli get k7qp-2m4x
```
