semrush cli

Output and scripting

Parse JSON output, handle pagination, manage numeric identifiers, and check exit codes.

semrush-cli is designed for reliable execution in shell scripts, automation pipelines, and cron jobs.

JSON output format

Successful authenticated reports return a JSON object with authenticated: true, the snake_case report name, and a data field holding the result object or array. Pass --json to produce compact, single-line JSON suitable for Unix pipes:

semrush keyword-overview --keyword 'email marketing' --json > keyword.json
jq '.data.vol' keyword.json

Passing arguments in automation

  • Arrays and objects: Pass valid JSON strings.
    semrush keyword-gap --domains '["example.com","competitor.com"]' --json
    semrush position-keywords-add --campaign-id 456_789 --keywords '["typescript"]' --confirm
  • Booleans: Flags accept bare --confirm, --confirm true, or --confirm false.
  • Large identifiers: Identifiers outside JavaScript's safe integer range are preserved as decimal strings.

Pagination conventions

Different endpoints use distinct pagination models based on provider contracts:

  • 1-based page indexing: Used by domain-keywords, keyword-magic, keyword-lists, and onpage-pages.
  • 0-based page indexing: Used by backlink-links, backlink-referring-domains, audit-pages, and backlink-audit-disavow.
  • Limit and offset: Used by saved-keywords, domain-strategies, position-pages, and reporting-overview.

Inspect pagination parameters for any command with semrush <command> --help.

Exit codes and error handling

Automation scripts should check exit status codes:

  • 0: Success.
  • 1: Service, authentication or report failure, including timeouts and denied account permissions.
  • 2: Invalid arguments or flags. Diagnostics are written to standard error.
  • 130: Process terminated by SIGINT (Ctrl+C).

In Bash automation scripts, enforce strict error checking:

#!/usr/bin/env bash
set -euo pipefail

output=$(semrush keyword-overview --keyword typescript --json)
printf '%s\n' "$output" | jq '.data'

Commands never prompt for interactive terminal input. All mutation operations require the explicit --confirm flag to execute.

On this page