Skip to navigation

CLI

The official command-line interface (@spyglasshq/cli on npm, 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 install -g @spyglasshq/cli

Check the install:

spyglass --version

Authentication

  1. Get an API key from Settings → API in the dashboard.
  2. Export it as SPYGLASS_API_KEY:
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:

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

# 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

CommandEndpoint
spyglass brands searchPOST /brands
spyglass creators searchPOST /creators
spyglass media searchPOST /media
spyglass taxonomies searchGET /taxonomies
spyglass comments getPOST /comments
spyglass account getGET /account/{id}
spyglass account postsGET /account/posts
spyglass account insightsGET /account/{id}/insights
spyglass account campaignsGET /account/{id}/campaigns
spyglass account collabsGET /account/{id}/collabs
spyglass account landing-pagesGET /account/{id}/landing-pages
spyglass account indexPOST /account/index
spyglass account index-statusGET /account/{id}/index-status
spyglass agent createPOST /agent/runs
spyglass agent listGET /agent/runs
spyglass agent getGET /agent/runs/{id}
spyglass team credit-usageGET /team/credit-usage

Each command costs the same as the endpoint it calls. See 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:

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 fieldFlag
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 -:

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 expression before it prints. --format raw prints the response unchanged and ignores --query.

# 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.

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:

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:

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

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:
spyglass generate-skills --output-dir .cursor/skills

Agents that support MCP can also use the hosted MCP server 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.

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

# 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.