# Commands

> Every Mynth CLI command and its main flags, with exit codes and environment variables. Images, tasks, models, balance, API keys, webhooks, destinations, and docs.

Login, logout, and `whoami` are on [logging in](https://mynth.io/docs/sdks/cli/logging-in.md).
Every command takes `--json` for machine-readable output, and
`npx @mynthio/cli <command> --help` lists every flag.

## Generate an image

```bash
npx @mynthio/cli image generate \
  -m black-forest-labs/flux.2-pro \
  -p "A lighthouse at dusk, film grain" \
  -s landscape \
  -o ./images
```

The command waits for the task, then prints the image URL. `-o` downloads the
files into that directory and creates it if needed.

| Flag                             | What it does                                                                    |
| -------------------------------- | ------------------------------------------------------------------------------- |
| `-m, --model`                    | Model id. Always pass one. See `models list`                                    |
| `-p, --prompt`                   | The prompt                                                                      |
| `-n, --negative`                 | Negative prompt                                                                 |
| `-s, --size`                     | A preset such as `landscape`, a ratio such as `16:9`, `16:9_4k`, or `auto`      |
| `-c, --count`                    | How many images, 1 to 20. Default 1                                             |
| `-f, --format`                   | `png`, `jpg`, or `webp`                                                         |
| `-i, --input`                    | Input image, repeatable, up to 20: a URL or a local file, with an optional role |
| `--magic-prompt`                 | Rewrite the prompt before generating                                            |
| `--content-rating`               | Rate each image sfw/nsfw                                                        |
| `-l, --level`                    | Custom rating level as `value=description`, repeatable, 2 to 7                  |
| `--levels-file`, `--levels-json` | Custom rating levels as a JSON array, from a file (or `-` for stdin) or inline  |
| `--metadata`                     | Inline JSON object stored on the task, up to 2 KB                               |
| `--destination`                  | Destination name. Defaults to `MYNTH_DESTINATION`                               |
| `--webhook-url`                  | Per-request webhook URL, repeatable, up to 5                                    |
| `--no-dashboard-webhooks`        | Skip your registered webhook endpoints for this task                            |
| `-o, --output-dir`               | Save the files into this directory. Ignored with `--async`                      |
| `--dry-run`                      | Print the estimated cost. Nothing is generated or uploaded                      |
| `--async`                        | Print the task id and its `pat_` token, and return without waiting              |
| `--detailed`                     | With `--json`, print the full task record                                       |

Omitting `-m` leaves `model` off the request, so the API uses `auto`, which is
experimental. See [the model field](https://mynth.io/docs/models.md#auto-is-experimental).

An input can carry a role: `-i source:./edit.png` or
`-i reference:https://example.com/a.png`. Roles are `source`, `reference`, and
`auto`. Local files are uploaded first, in one upload of at most 10 files.
[Generate images](https://mynth.io/docs/guides/generate-images.md) covers every field.

`--dry-run` calls `POST /image/generate/estimate`. For `auto` the line says
`(upper bound)`. [Estimate cost](https://mynth.io/docs/guides/estimate-cost.md) is the same call
over HTTP.

## Other image commands

These take one image, as a URL or a local file, and have no `--model` flag.

```bash
npx @mynthio/cli image rate ./shot.png
npx @mynthio/cli image alt ./shot.png
npx @mynthio/cli image review ./shot.png --effort low
npx @mynthio/cli image remove-background ./shot.png -f png -o ./out
npx @mynthio/cli image upload ./a.png ./b.webp
```

- `rate` uses sfw/nsfw unless you pass custom levels with `-l`,
  `--levels-file`, or `--levels-json`.
- `review` defaults to `--effort high`. `low` is the cheaper, faster panel.
- `remove-background` outputs `png` or `webp` with `-f`, and also takes
  `--async`, `-o`, `--metadata`, `--destination`, `--webhook-url`, and
  `--no-dashboard-webhooks`.
- `upload` sends up to 10 local JPEG, PNG, or WEBP files and prints a URL for
  each. The URLs are served for 1 day.

The CLI has no video command. Use the SDK or the REST API for video.

## Models

```bash
npx @mynthio/cli models list --type image --capability img2img --max-price 0.05
```

`models list` reads the public catalog and needs no account. The filters run
locally:

| Flag                         | Keeps                                                         |
| ---------------------------- | ------------------------------------------------------------- |
| `-s, --search`               | Fuzzy matches on the id and display name                      |
| `--org`                      | One vendor, fuzzy matched, so `bfl` finds `black-forest-labs` |
| `--type`                     | `image` or `video`                                            |
| `--capability`               | `txt2img`, `img2img`, `txt2vid`, or `img2vid`                 |
| `--4k`                       | Models with a 4k price                                        |
| `--max-price`, `--min-price` | The cheapest rate: per image, or per second for video         |

## Tasks

```bash
npx @mynthio/cli task list --limit 10
npx @mynthio/cli task get tsk_01KE7XWWEQ4MCGWKBQKJ1G47RP
npx @mynthio/cli task result tsk_01KE7XWWEQ4MCGWKBQKJ1G47RP
npx @mynthio/cli task wait tsk_01KE7XWWEQ4MCGWKBQKJ1G47RP --timeout 120
```

`list` is newest first. `--limit` is 1 to 100, default 20, and `--after` takes
a task id to page back from. `result` prints only the result as JSON. `wait`
blocks until the task settles, for up to 30 minutes unless you pass
`--timeout <seconds>`. `wait` takes `--detailed` to print the full record
instead of a summary.

## Balance

```bash
npx @mynthio/cli balance
```

Prints balance, reserved, and available, then the key's spending limit when it
has one. It needs a key with the `manage` scope, which the key from
`auth login` has. [Pricing](https://mynth.io/docs/pricing.md#balance) explains the numbers.

## API keys

```bash
npx @mynthio/cli api-key create my-app
npx @mynthio/cli api-key list
npx @mynthio/cli api-key delete ak_01KE7XWWEQ4MCGWKBQKJ1G47RP --yes
```

`create` prints a `mak_` key once, with the `generate` scope. The CLI
authenticates with an API key, and an API key can only create `generate` keys:
asking for `manage` or `keys` fails with `403 SCOPE_ESCALATION` and exit `3`.
Create wider keys in the [dashboard](https://mynth.io/dashboard/keys).

`list` shows each key's id, name, preview, and scopes, never the key itself.
Spending limits are set in the dashboard. `delete` needs `--yes`.

## Webhooks

```bash
npx @mynthio/cli webhook create --url https://example.com/api/mynth -e all
```

| Flag             | What it does                                                                   |
| ---------------- | ------------------------------------------------------------------------------ |
| `--url`          | Required. The endpoint URL                                                     |
| `-e, --event`    | Required, repeatable. An event name, `task.completed`, `task.failed`, or `all` |
| `--api-key-id`   | Repeatable. Deliver only tasks from these keys. Omit for every key             |
| `--oauth-events` | Also deliver tasks with no API key, such as playground runs                    |
| `--disabled`     | Create the endpoint disabled                                                   |

The `wbs_` signing secret prints once. Tasks from the CLI carry the key from
`auth login`, so they deliver without `--oauth-events`.

`webhook update <id>` replaces the whole configuration: pass `--url` and every
`-e` again. Leaving out `--oauth-events` turns it off, and leaving out
`--api-key-id` goes back to every key. `webhook delete <id>` needs `--yes`.
[Webhooks](https://mynth.io/docs/concepts/webhooks.md) covers the signature and the payload.

## Destinations

```bash
npx @mynthio/cli destination create bunny-prod \
  --provider bunny \
  --storage-zone my-zone \
  --region de \
  --path-template 'images/{id}' \
  --url-template 'https://cdn.example.com/{path}' \
  --secret -
```

The name is 1 to 64 characters of `a-z`, `0-9`, and `-`, and cannot change
later. Providers are `bunny`, `r2`, and `s3`. `--secret` takes a file path, or
`-` for stdin, never the secret itself. Bunny takes the storage password as
plain text. `r2` and `s3` take JSON with `access_key_id` and
`secret_access_key`. `--file` sends a whole JSON body instead of the typed
flags.

```bash
npx @mynthio/cli destination test dst_01KE7XWWEQ4MCGWKBQKJ1G47RP
npx @mynthio/cli image generate -m black-forest-labs/flux.2-pro -p "A lighthouse" --destination bunny-prod
```

`test` uploads a probe to a unique path, or to `--path`. `list` and `get` show
what is stored, without the secret. `update` replaces provider and config.
`delete` needs `--yes`. These commands need the `manage` scope.
[Destinations](https://mynth.io/docs/concepts/destinations.md) covers the templates.

## Config

```bash
npx @mynthio/cli config set api-key -
npx @mynthio/cli config unset api-key
```

`set` stores an existing key, read from stdin, in the credentials file.
`unset` deletes the file and does not revoke the key.
[Logging in](https://mynth.io/docs/sdks/cli/logging-in.md) says when to use these instead of
`auth login`.

## Docs

```bash
npx @mynthio/cli docs list
npx @mynthio/cli docs get concepts/tasks
npx @mynthio/cli docs get index
```

No account needed. `get` takes the page path without `.md`, and `index` is the
introduction. `list` prints [/llms.txt](https://mynth.io/llms.txt). Pages come from
`https://mynth.io/docs/<path>.md`. Set `MYNTH_DOCS_URL` to read another host.

## Exit codes

The message goes to stderr. Branch on the exit code, not the text.

| Exit | Meaning           | From                                                         |
| ---- | ----------------- | ------------------------------------------------------------ |
| 0    | Success           |                                                              |
| 1    | Any other failure |                                                              |
| 2    | Usage             | Bad flags, or `VALIDATION_ERROR`                             |
| 3    | Auth              | `UNAUTHORIZED`, `INSUFFICIENT_SCOPE`, any `401` or `403`     |
| 4    | Out of credit     | `INSUFFICIENT_BALANCE`, `SPENDING_LIMIT_EXCEEDED`, any `402` |
| 5    | Refused           | `RESTRICTED_CONTENT`                                         |
| 6    | Rate limited      | Any `429`                                                    |

How a task failure maps to an exit code depends on the command:

- `rate`, `alt`, `review`, and `remove-background` fail when the task fails:
  exit `5` for `RESTRICTED_CONTENT`, `1` otherwise.
- `task wait` prints the failed task and exits `5` if any code on the task or
  its images is `RESTRICTED_CONTENT`, `1` otherwise.
- `image generate` prints the result and exits `0` even when the task failed
  or an image was refused. Read `status` and `images[].error.code` in the
  `--json` output.

`MYNTH_DEBUG=1` adds the cause and stack trace to stderr.
[Errors](https://mynth.io/docs/api-reference/errors.md) has every code.

## Environment

| Variable            | Effect                                                    |
| ------------------- | --------------------------------------------------------- |
| `MYNTH_API_KEY`     | Sent instead of the stored key                            |
| `MYNTH_DESTINATION` | Default `--destination`                                   |
| `MYNTH_DEBUG=1`     | Error cause and stack trace on stderr                     |
| `MYNTH_DOCS_URL`    | Docs host for `docs get`. Default `https://mynth.io/docs` |
| `MYNTH_API_URL`     | API host. Default `https://api.mynth.io`                  |
| `XDG_CONFIG_HOME`   | Directory for the credentials file. Default `~/.config`   |

## Next steps

- [Logging in](https://mynth.io/docs/sdks/cli/logging-in.md): the stored key and which credential is sent.
- [Generate images](https://mynth.io/docs/guides/generate-images.md): the request fields behind the flags.
- [Errors](https://mynth.io/docs/api-reference/errors.md): request errors and task failures.
