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

# CLI

> Use the QFEX CLI for agentic trading and JSON-native command-line workflows.

The [QFEX CLI](https://github.com/QFEX-org/cli) provides JSON-only command-line access to QFEX for both human operators and AI agents.

It is designed for agentic trading workflows where you want a stateless command interface, while a background daemon manages the underlying WebSocket connections to QFEX.

## Installation

```sh theme={null}
brew install QFEX-org/tap/qfex
```

## AI Agent Setup

Run this once to configure QFEX CLI for agent workflows:

```sh theme={null}
# Global setup for Claude Code and Codex
qfex agents init

# Project-local setup for the current directory
qfex agents init --local
```

`qfex agents init` updates Claude Code and Codex so `qfex` can be used without repeated permission prompts. The global setup writes to `~/.claude/` and `~/.codex/`. The `--local` mode writes `CLAUDE.md` and `AGENTS.md` into the current project, which is useful when you want project-specific agent instructions.

## Quick Start

```sh theme={null}
# Start the background daemon
qfex daemon start

# Query market data (no credentials required)
qfex market bbo AAPL-USD

# Log in to trade or access account data
qfex login
qfex daemon restart

# Place an order
qfex order place --symbol AAPL-USD --side BUY --type LIMIT --tif GTC --qty 1 --price 200

# Stop the daemon when finished
qfex daemon stop
```

## Why Use The CLI

* Built for both humans and AI agents.
* All command output is JSON, which makes it easy to pipe into `jq` or parse in code.
* The daemon keeps persistent connections to market data and trade WebSockets, so your scripts do not need to manage subscriptions, authentication, or socket lifecycle directly.
* Market data commands work without credentials.
* Trading commands use the same API keys described in the [API introduction](/api-reference/introduction).

## Agentic Trading Workflow

For an automated or agentic trading setup, the normal flow is:

1. Run `qfex agents init` once so your agent can call `qfex` cleanly.
2. Start the daemon with `qfex daemon start`.
3. Use market data commands immediately, or generate API keys and run `qfex login` for trading.
4. Run `qfex daemon restart` after login so credentials are applied.
5. Confirm readiness with `qfex daemon status`.
6. Issue market data, order, position, and account commands as needed.

The daemon must be running before most CLI commands will work.

## Common Commands

```sh theme={null}
# Market data
qfex market bbo AAPL-USD
qfex market orderbook AAPL-USD --depth 5
qfex market trades AAPL-USD --limit 50

# Orders
qfex order place --symbol AAPL-USD --side BUY --type MARKET --tif IOC --qty 1
qfex order list
qfex order cancel --symbol AAPL-USD --order-id <order-id>

# Positions and account state
qfex position list
qfex account balance
qfex account leverage get

# Live streams
qfex watch bbo AAPL-USD
qfex watch orders
qfex watch fills
```

## Environments

The CLI supports both QFEX production and UAT environments:

* `prod`: Connects to `qfex.com` with real funds.
* `uat`: Connects to `qfex.io` for testing strategies and integrations without real funds.

You can select the environment during `qfex login` or by editing `~/.config/qfex/config.yaml`, then restarting the daemon.

## Subaccounts

If your account has subaccounts, `qfex login` will prompt you to choose which account should be active by default. The selected value is stored in `~/.config/qfex/config.yaml` as `selected_subaccount`.

Use these commands to inspect or change it later:

```sh theme={null}
qfex account subaccounts current
qfex account subaccounts select <account-id>
qfex account subaccounts list
```

Selecting a different subaccount updates the config and restarts the daemon so authenticated trading and account requests use the **new account** context.

You can also transfer money very easily, between subaccounts using the following command

```sh theme={null}
qfex account subaccounts transfer
```

This command accepts `--from`, `--to`, and `--amount` (in USD). You can set either `--from` or `--to` to `"primary"` to refer to your main account.
The other account should be a UUID returned by `qfex account subaccounts list`.

## Links

* [QFEX CLI repository](https://github.com/QFEX-org/cli)
* [API key setup instructions](/api-reference/introduction)
* [WebSocket overview](/websocket/main)
