# Scripting and exit codes

The CLI is built to be scripted. Its outputs are predictable and its exit codes mean something.

## Output streams

- **Standard output** holds the result only: a link URL, a new key, JSON, a table, or the bytes of `fuzzy get -o -`.
- **Standard error** holds notices, confirmations and errors. A progress line is drawn there only when it is a terminal.

So `URL=$(fuzzy put report.pdf --share)` captures just the URL.

## Exit codes

| Code | Meaning |
|---|---|
| `0` | Success |
| `1` | A request or runtime error. The server's own error text is shown as is |
| `2` | A usage error: unknown command or flag, wrong number of arguments, conflicting options. Options are checked before anything is uploaded |

## Patterns

```sh
# upload and get the link
URL=$(fuzzy put build.tar.gz --ttl 1d --share)

# a one-time secret
fuzzy put secret.txt --passphrase "$PASS" --max-downloads 1

# pipelines
pg_dump mydb | gzip | fuzzy put - --name mydb.sql.gz --ttl 3d
fuzzy get mydb.sql.gz -o - | gunzip | psql other

# CI: no login needed
FUZZY_URL=https://fuzzyshare.com FUZZY_KEY=$CI_KEY fuzzy put out.zip --share

# machine-readable lists
fuzzy ls --json | jq -r '.files[].name'
fuzzy links --json | jq -r '.links[] | select(.access=="public") | .id'
```

## Handle failures

```sh
if ! URL=$(fuzzy put report.pdf --ttl 1h --share); then
  echo "upload failed" >&2
  exit 1
fi
```

`fuzzy rm` on several names keeps going after a failure and exits `1` if any failed.

## Keep secrets out of the process list

A flag such as `--passphrase` is visible to other users in `ps`. Use `FUZZY_PASSPHRASE` with `--private` instead, and `FUZZY_KEY` instead of `--key`.

```sh
FUZZY_PASSPHRASE="$PASS" fuzzy put secret.txt --private --share
```

## JSON output

`whoami`, `ls`, `links`, `activity` and `keys ls` accept `--json` and print the server's answer unchanged. The shapes are the ones in the [HTTP API](api-overview.md).
