> ## Documentation Index
> Fetch the complete documentation index at: https://docs.4mica.io/llms.txt
> Use this file to discover all available pages before exploring further.

# CLI

> Use @4mica/cli to scaffold a project, manage a wallet's collateral, and pay for or check 4Mica payments from a terminal.

# CLI

`@4mica/cli` puts 4Mica in your terminal. Use it to scaffold a project, create a wallet, deposit and withdraw collateral, pay for a 402-protected URL, and settle a cycle. Every command also works unattended, so agents and scripts can run it.

## Install

Node.js 22 or later is required.

```bash npx theme={null}
npx @4mica/cli --help
```

```bash npm theme={null}
npm install --global @4mica/cli
4mica --help
```

`4mica autocomplete` prints the steps to turn on tab completion in bash, zsh or PowerShell.

To uninstall: `npm uninstall --global @4mica/cli`. Settings and wallet keys stay in `~/.config/4mica` (`%LOCALAPPDATA%\4mica` on Windows). Those keys are the only copies: withdraw or back them up before deleting that directory.

## From nothing to a paid request

```bash theme={null}
4mica init my-buyer --template buyer-express   # a project, in demo mode
4mica wallets create                           # a wallet; prints its address
```

Mint test USDC to the wallet's address as [Get test tokens](https://docs.4mica.io/getting-started/test-tokens.md) describes. The new wallet needs no ETH: another wallet can send the mint, and the deposit is gasless. Then:

```bash theme={null}
4mica deposit 10 --asset USDC                  # collateral to pay from
4mica fetch https://api.example.com/report     # pays if the URL asks for payment
```

`4mica status` shows at any point whether the services answer, whether the wallet holds anything, and whether it has collateral.

The CLI uses the Base Sepolia testnet unless told otherwise, even though the SDKs default to mainnet. `--network ethereum-sepolia` selects the other testnet. Real funds move only after `--network base` or `4mica config set network base`. `4mica networks` lists every network the CLI knows; see [supported networks](https://docs.4mica.io/getting-started/supported-networks.md) for which are live.

## Commands

| Command | What it does |
| - | - |
| `init [dir]` | Scaffold a project. Ends with a checklist of what a real payment still needs. |
| `dev` | Run the current project's `dev` script with the package manager the project uses. |
| `auth login`, `auth status`, `auth logout` | Sign the CLI in to your 4Mica account through the browser, check the session, and sign out. |
| `account` | Show the account and the wallets linked to it. |
| `dashboard` | Open the 4Mica dashboard and print its address. `--no-open` only prints it. |
| `wallets`, `wallets create`, `wallets import`, `wallets use`, `wallets remove` | List, create, import, switch and delete wallets. |
| `wallets link`, `wallets unlink` | Link the wallet in use to your account by signing a one-time message, or remove it. |
| `whoami` | Show the wallet and network in use, and where each comes from. |
| `balance [asset]` | Collateral: total, locked and available. |
| `deposit <amount>` | Deposit collateral. |
| `withdraw <amount>`, `withdrawals`, `withdrawals finalize`, `withdrawals cancel` | Request a withdrawal, see pending ones, then pay out or cancel. |
| `fetch <url>` | Request a URL, paying a 402 within `--max`. |
| `cycles`, `cycles checkpoint`, `cycles positions`, `cycles pay`, `cycles claim` | The open cycle per asset; confirm payments; list the closed cycles this wallet owes or is owed in, by the id `pay` and `claim` take; pay or collect a closed cycle's net amount. |
| `payments` | The guarantees this wallet was paid with in one cycle: the open cycle of `--asset`, or the one `--cycle` names. One page at a time; `--after` takes the `nextCursor` of the last. |
| `guarantees verify <file>` | Check that a guarantee receipt was signed by 4Mica. Needs no wallet. |
| `networks`, `tokens`, `status` | Supported networks, accepted tokens, service and wallet health. |
| `config`, `config set`, `config get`, `config unset`, `config reset` | Settings, and where each comes from. `reset` keeps the wallet keys. |
| `version` | The CLI's version, Node and platform, for a bug report. |

`4mica help <command>` shows a command's examples and flags.

## Your account

A wallet key is enough to deposit, pay and withdraw. Signing in is separate: it connects the CLI to your 4Mica account, so wallets you hold can be linked to it and shown in the dashboard.

```bash theme={null}
4mica auth login       # shows a one-time code and opens the dashboard to approve it
4mica wallets link     # proves you hold the wallet in use, by signing a message
4mica account          # the account and its linked wallets
```

`auth login` never asks for a password in the terminal. You approve the code at `app.4mica.io/cli/authorize` while signed in, and the CLI receives a token of its own, kept in the system keychain where there is one. Over SSH, `--no-open` prints the address to open on another device. Every signed-in terminal is listed under Settings → Developer, where it can be revoked.

A token cannot move funds. It reads the account and links wallets; removing wallets has to be asked for with `--scopes`. Payments are still signed by the wallet key, which never leaves your machine. `auth logout` revokes a browser session, and leaves a dashboard token valid unless you pass `--revoke`.

In CI, create a token under Settings → Developer → CLI sessions and set it as `FOURMICA_TOKEN`.

## The wallet key

A wallet is a private key. The CLI takes it from the first of these that has one:

1. `--key-file <path>`, a file holding only the key. `-` reads it from standard input; typed at a terminal, it is masked as you type.
2. `FOURMICA_WALLET_PRIVATE_KEY`, exported in the shell. Meant for CI and containers: an exported key is visible to every program the shell starts. In an interactive shell prefer a key file.
3. A `.env` file in the current directory, read automatically. `--dotenv <path>` reads another file instead.
4. The key file named in the config file. `wallets create` and `wallets use` set it.

No flag takes the key itself, because flag values end up in shell history and in the process list. The key is never printed.

> [!WARNING]
>
> `4mica wallets remove` deletes the only copy of a key. Withdraw from the wallet first.

## Sending money safely

Commands that sign (`deposit`, the `withdrawals` actions, `cycles pay`, `cycles claim`, `cycles checkpoint`) show what they are about to do (network, wallet, token and its address, amount in base units, how gas is paid, the contract) and ask before sending.

* **`--dry-run`** (`-n`) checks that the command would work and sends nothing. It exits 1 when something blocks it, and the real command then refuses with `NOT_READY` without asking.
* **`--yes`** skips the question. Without a terminal and without `--yes`, the command exits instead of waiting.
* **`--gas`** on `deposit`, `cycles pay` and `cycles claim` picks who pays gas. `auto`, the default, uses a sponsored transaction when the network's facilitator offers one; `self` always sends the wallet's own transaction, which needs the network's native token for gas.
* **On mainnet**, or a core given by URL that is not on this machine, the question asks you to type the network's name (or the core's host), not `y`.
* **Once something is sent**, its transaction hash is printed straight away. A failure or Ctrl-C after that point is reported as `OUTCOME_UNKNOWN`, with where to check: the transaction may still land, so do not run the command again until `balance`, `withdrawals` or `cycles` shows what happened.

`fetch` does not ask: `--max` is the consent. On a testnet it defaults to 1 USDC a payment; anywhere that may be real funds it needs `--max`, or `--yes` to accept that default, and stops with `MAX_REQUIRED` before sending anything otherwise. A URL that charges nothing needs no wallet. `fetch --dry-run` shows what a URL charges without paying. It still sends the request once, unpaid, and a URL that charges nothing answers it for real.

After a deposit is mined, the CLI waits up to `--timeout` seconds for core to count it. If that runs out, the deposit is reported as `pending` and the command exits 0. Do not send it again.

## Scripting

* **Standard output has the result and nothing else.** Progress, questions, warnings and errors go to standard error. `--quiet` drops the progress.
* **`--json`** prints the result as one JSON document. Field names are camelCase. An amount is a number in a string with its unit beside it, never inside it: `"amount": "2.5"`, `"asset": "USDC"`, and `"amountRaw"` for the base units.
* **Commands are described as data too.** The package ships an `oclif.manifest.json` with every command, flag and allowed value.
* **`--no-input`** guarantees no question is asked.
* **Errors carry a code**, such as `NO_SIGNER`, `INSUFFICIENT_BALANCE`, `UNREACHABLE`, `OUTCOME_UNKNOWN` or `CHECKPOINT_REQUIRED`. With `--json` every failure, a mistyped command included, is printed on standard output as `{ "error": { "code", "message", "suggestions", "retryable", "details" } }`. `retryable` says whether running the same command again is safe.
* **`--debug`** (or `DEBUG=4mica`) prints each request and the stack of a failure to standard error, never a header, body or key.

| Exit code | Meaning |
| - | - |
| 0 | Done, including a deposit that is mined but still `pending`. |
| 1 | The operation failed: no response, a refusal by core, a reverted transaction, or a dry run that found a problem. |
| 2 | Usage: an unknown command or flag, a bad value, or a confirmation that could not be asked for. |
| 3 | Configuration: no wallet key, or an unknown network in the environment or config file. |
| 130 | Cancelled: Ctrl-C, or a question answered no. |

```bash theme={null}
available=$(4mica balance USDC --json | jq -r '.[0].available')
4mica deposit 5 --asset USDC --dry-run && 4mica deposit 5 --asset USDC --yes --json
```

## Settings

Each setting comes from the first of: a flag, an environment variable, the project's `.env`, the config file at `~/.config/4mica/config.json`, the built-in default. `4mica config` prints the result.

```bash theme={null}
4mica config set network base-sepolia
4mica config set asset USDC      # then `4mica deposit 5` needs no --asset
```

The environment variables are `FOURMICA_NETWORK`, `FOURMICA_RPC_URL`, `FOURMICA_FACILITATOR_URL`, `FOURMICA_ASSET`, `FOURMICA_WALLET_PRIVATE_KEY`, `FOURMICA_TOKEN`, `FOURMICA_REQUEST_TIMEOUT` and `FOURMICA_DEBUG`. Three more change how the CLI itself behaves: `FOURMICA_API_URL` points `auth` and `account` at another 4Mica platform API, `FOURMICA_CREDENTIAL_STORE` is `file` to keep the account token out of the system keychain or `keychain` to require it, and `FOURMICA_NO_COLOR` (like `NO_COLOR`) turns colour off. `--rpc-url` is a 4Mica core's address, not an Ethereum RPC endpoint, and must be `https` unless it is on this machine. Requests honour `HTTPS_PROXY`, `HTTP_PROXY` and `NO_PROXY`.

## Limits

* Wallet keys are plain files readable by your user only. They are not encrypted, and a wallet key cannot be kept in the system keychain or on a hardware wallet yet.
* The account token is stored separately from wallet keys: in the system keychain on macOS and on Linux with `secret-tool`, and elsewhere in a file readable by your user only.
* One pending withdrawal per asset.
* The CLI sends no telemetry and checks for no updates.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.