> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.spyglass.so/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.spyglass.so/_mcp/server.

# CLI

> The Spyglass CLI calls the Spyglass API from your terminal, scripts, CI, and coding agents.

The official command-line interface ([`@spyglasshq/cli`](https://www.npmjs.com/package/@spyglasshq/cli) on npm, [`spyglass-so/spyglass-cli`](https://github.com/spyglass-so/spyglass-cli) on GitHub) runs every Spyglass API endpoint as a `spyglass` command. It ships as a single native binary for macOS, Linux, and Windows, and it is generated from the same OpenAPI spec as the SDKs, so it stays in sync with the API.

## Installation

**`npm`**

```bash npm
npm install -g @spyglasshq/cli
```

**`npx`**

```bash npx
npx @spyglasshq/cli --help
```

**`Shell (macOS / Linux)`**

```bash Shell (macOS / Linux)
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/spyglass-so/spyglass-cli/releases/latest/download/spyglass-cli-installer.sh | sh
```

**`PowerShell (Windows)`**

```powershell PowerShell (Windows)
powershell -ExecutionPolicy ByPass -c "irm https://github.com/spyglass-so/spyglass-cli/releases/latest/download/spyglass-cli-installer.ps1 | iex"
```

Check the install:

```bash
spyglass --version
```

## Authentication

1. Get an API key from **Settings → API** in the [dashboard](https://app.spyglass.so).
2. Export it as `SPYGLASS_API_KEY`:

```bash
export SPYGLASS_API_KEY="sg_live_..."
```

To keep the key out of your shell profile, store it in the OS keychain instead. The API uses a bearer token and the CLI has no browser login, so pass `--with-token` and paste the key when stdin waits:

```bash
spyglass auth login --with-token   # paste the key, then press Enter
spyglass auth status               # show where the key is coming from
spyglass auth logout               # remove it from the keychain
```

`spyglass auth login` on its own exits with `No login flow declared for scheme bearerAuth`. If the key is already in the environment, `spyglass auth login --with-token --from-env` copies it into the keychain without a paste.

The CLI also reads `SPYGLASS_API_KEY` from a `.env` file in the working directory. In CI, load the key from a secret.

## Quick start

```bash
# 1. Resolve a brand by name
spyglass brands search --names Nike --limit 1

# 2. Search the creatives that brand is running
spyglass media search \
  --account-id e9cf96ee-7455-4b00-b692-80e2f923f79d \
  --query-param "marathon running shoes comfort" \
  --limit 10
```

Run `spyglass --help` for the full command tree, and `spyglass <command> <subcommand> --help` for every flag on one operation.

## Commands

| Command                          | Endpoint                          |
| -------------------------------- | --------------------------------- |
| `spyglass brands search`         | `POST /brands`                    |
| `spyglass creators search`       | `POST /creators`                  |
| `spyglass media search`          | `POST /media`                     |
| `spyglass taxonomies search`     | `GET /taxonomies`                 |
| `spyglass comments get`          | `POST /comments`                  |
| `spyglass account get`           | `GET /account/{id}`               |
| `spyglass account posts`         | `GET /account/posts`              |
| `spyglass account insights`      | `GET /account/{id}/insights`      |
| `spyglass account campaigns`     | `GET /account/{id}/campaigns`     |
| `spyglass account collabs`       | `GET /account/{id}/collabs`       |
| `spyglass account landing-pages` | `GET /account/{id}/landing-pages` |
| `spyglass account index`         | `POST /account/index`             |
| `spyglass account index-status`  | `GET /account/{id}/index-status`  |
| `spyglass agent create`          | `POST /agent/runs`                |
| `spyglass agent list`            | `GET /agent/runs`                 |
| `spyglass agent get`             | `GET /agent/runs/{id}`            |
| `spyglass team credit-usage`     | `GET /team/credit-usage`          |

Each command costs the same as the endpoint it calls. See [Billing](/documentation/get-started/billing).

## Passing parameters

Every request field is a kebab-case flag. `accountId` becomes `--account-id`, and `igUsername` becomes `--ig-username`. Repeat a flag for array fields, or pass a JSON array:

```bash
spyglass media search \
  --query-param "UGC founder story explaining product benefits" \
  --channel META_AD --channel TIKTOK \
  --status ACTIVE \
  --min-days-run 14 \
  --limit 20
```

Two fields have different flag names, because `--query` and `--format` are global CLI flags:

| API field                   | Flag             |
| --------------------------- | ---------------- |
| `query`                     | `--query-param`  |
| `format` (on `POST /media`) | `--format-param` |

For endpoints with a request body, you can also send the whole body as JSON, or pipe it from stdin with `--json -`:

```bash
spyglass brands search --json '{"query": "DTC electrolyte and hydration powder brands", "limit": 10}'

echo '{"websiteUrl": "https://www.on.com"}' | spyglass account index --json -
```

## Output

Output is a table in a terminal and JSON when piped, so scripts and agents get JSON without extra flags. Set it explicitly with `--format json|table|yaml|csv|jsonl|raw|http`, or with the `SPYGLASS_OUTPUT` environment variable.

Use `--query` to filter the response with a [JMESPath](https://jmespath.org) expression before it prints. `--format raw` prints the response unchanged and ignores `--query`.

```bash
# Just the hook and URL of each creative
spyglass media search --account-id "$BRAND_ID" --limit 10 \
  --query 'media[].{hook: hook, url: url}'

# Pipe to jq
spyglass brands search --names Nike --limit 1 --format json | jq -r '.brands[0].id'
```

## Examples

### Fetch an account's posts live

`spyglass account posts` doesn't need the account to be indexed. Send exactly one of `--ig-username`, `--tiktok-username`, or `--facebook-ad-library-id`. A call usually takes 10–30 seconds.

```bash
spyglass account posts --ig-username nike --post-type reels --limit 20
spyglass account posts --tiktok-username nike --sort-by popular --limit 20
spyglass account posts --facebook-ad-library-id 15087023444 --limit 20
```

### Creative insights, campaigns, and landing pages

`--id` accepts an account id, a website, or an Instagram or TikTok handle:

```bash
spyglass account insights --id nike.com --type HOOK --max-days-back 90 --limit 10
spyglass account campaigns --id nike.com --type CAMPAIGN --date-preset last-30-days
spyglass account landing-pages --id @nike --limit 20
```

### Index a new account and wait for it

Indexing is free. Poll `index-status` until `status` is `ready`:

```bash
BRAND_ID=$(spyglass account index --website-url https://www.on.com --query brandId --format json | tr -d '"')

until [ "$(spyglass account index-status --id "$BRAND_ID" --query status --format json | tr -d '"')" = "ready" ]; do
  sleep 3
done

spyglass account get --id "$BRAND_ID"
```

### Check remaining credits

```bash
spyglass team credit-usage --query remainingCredits
```

## Using the CLI from coding agents

The CLI is designed for agents such as Cursor, Claude Code, and Codex:

* Output is JSON whenever it isn't a terminal.
* `--dry-run` prints the exact HTTP request without sending it or spending credits.
* `spyglass --help --format json` (or `--schema`) prints a machine-readable catalog of every command and flag.
* `spyglass generate-skills` writes `SKILL.md` files that teach an agent the commands. Point `--output-dir` at your agent's skills folder:

```bash
spyglass generate-skills --output-dir .cursor/skills
```

Agents that support MCP can also use the [hosted MCP server](/documentation/get-started/mcp) without an API key.

## Errors and retries

Errors print to stderr as JSON with `code`, `reason`, and `message`, and the process exits non-zero. The API's error codes are listed in [Errors](/documentation/conventions/errors).

The CLI retries network errors and retryable statuses automatically. Use `--retries <N>` to change the count, or `--no-retry` to turn retries off. Add `--debug` to print the full HTTP request and response to stderr.

## Shell completion

```bash
# zsh
spyglass completion zsh > "${fpath[1]}/_spyglass"

# bash
spyglass completion bash > /etc/bash_completion.d/spyglass

# fish
spyglass completion fish > ~/.config/fish/completions/spyglass.fish
```

## Upgrading

Rerun the command you installed with, for example `npm install -g @spyglasshq/cli@latest`.