# Output contract

The rules that make `btdl` scriptable. Learn these four and every
command behaves the way you expect it to.

## stdout: the payload, and nothing else

- **without `--json`** — a human view (aligned tables, label lines,
  report summaries). For people, not for parsers.
- **with `--json`** — the exact JSON payload the command is about:
  the library’s facade dict (`auth status`, `search`, `plan`, …),
  the model `to_dict` wire mirror (`metadata`, `tracks`, …), a plain
  list (`search-albums`, `genres`, `autocomplete`, `artist albums`,
  …), a single string (`artwork-url`), or `null` (`atmos` on a
  miss).

Key naming: dicts `btdl`/the library invent for the surface are
**snake_case** (`user_id`, `seconds_left`, `download_count`,
`video_count`, …); the Tidal v1 wire mirrors inside `metadata` /
`playback` / nested `subscription` stay **camelCase** — that is the
raw payload, spelled the way the wire spells it.

## stderr: everything a person should see

Progress events (per-item `stage`, `kind`, `index`, `total`,
`title`, `status`), the device-login instructions, the “cancelling”
notice, log lines (`--verbose` for `DEBUG`). A piped stdout is
**never** disturbed by any of it:

```console
$ btdl download "$ALBUM" --json 2> progress.log | jq '.ok'
```

`progress.log` carries the per-item progress the terminal would
show. (The human view — tables and report summaries — goes to
stdout, like the JSON; stderr stays the progress/error channel in
both modes.)

## Exit codes

| Code   | Meaning                                                                                                                                          |
|--------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| `0`    | success (including a run whose report contains failed items — the report says so; see below)                                                     |
| `1`    | a `TidalError`: auth, not-found, region lock, quality unavailable, … (one `btdl: error: …` line on stderr; auth errors add a `btdl: hint:` line) |
| `130`  | cancelled: a `CancelledError`, or the run’s SIGINT cancel path                                                                                   |
| `2`    | a usage error (bad flags, `stream --json` without `--out`, …) or an internal error — never expected                                              |

Per-item failure isolation: a collection download exits `0` when the
*run* completed, and the report’s `items[]` carry the per-item
`status` (`ok` | `failed` | `skipped`) + `reason`. A broken item is
**always** a report row, never a run failure — what `skip_broken`
(the global `--skip-broken/--no-skip-broken`; the per-run
`--fail-fast` overrides it) decides is what happens *after*: with it
on, the run keeps going (the bad item is one `failed` row among the
`ok`s); with it off, the run aborts after the first failure (the
report comes back `ok: false`, the remaining items `skipped` with
reason `aborted: previous item failed` — still exit `0`). The only
run-level failure is a link that cannot resolve: that exits `1` with
no report, with or without the flag.

## Streams to stdout

`btdl stream URL` (no `--out`) writes the track’s plaintext bytes
to **stdout** — the binary *is* the payload there. That is why
`--json` requires `--out` for `stream`: two payloads can’t share a
pipe.

```console
$ btdl stream "$TRACK" | ffplay -f wav -
$ btdl stream "$TRACK" -o track.flac        # the bytes to the file, a -> path (N bytes) line on stdout
```

## Secrets

Token values never reach any output. Login-style commands emit a
safe summary (`user_id`, `client_id`, `country_code`); the client
registry omits `client_secret`. Library error messages are redacted
at their boundary (signed URLs, API `userMessage` bodies) before
`btdl: error:` prints them.
