AI agent-first command-line interface for the Ahrefs API v3.
This CLI was built with one primary mission: make the Ahrefs API maximally discoverable and usable for AI coding agents.
Why AI agents? Because if an AI can use your CLI without reading docs, humans definitely can too.
- Discoverability First - Every command is self-documenting
- Machine-Readable - All metadata available as structured JSON
- Validation Before Execution - Dry-run mode for every command
- Minimal Dependencies - Only Cobra + Go stdlib
- Structured Errors - Rich error messages with suggestions
# Install with Go
go install github.com/aminemat/ahrefs-cli@latest
# Or clone and build
git clone https://github.com/aminemat/ahrefs-cli
cd ahrefs-cli
make build# Save to config file (~/.ahrefsrc)
ahrefs config set-key YOUR_API_KEY_HERE
# Or use environment variable
export AHREFS_API_KEY=YOUR_API_KEY_HERE# Get domain rating for ahrefs.com (free test query)
ahrefs site-explorer domain-rating --target ahrefs.com --date 2024-01-01
# Output
{
"status": "success",
"data": {
"domain_rating": {
"domain_rating": 91
}
},
"meta": {
"response_time_ms": 472
}
}Foundation:
- β HTTP client with Bearer auth
- β Automatic retries with exponential backoff
- β Rate limiting support
- β
Config management (
~/.ahrefsrc) - β Multiple output formats (JSON, YAML, CSV, Table)
- β 87.7% test coverage on HTTP client
Site Explorer Endpoints:
- β
domain-rating- Get domain rating - β
backlinks-stats- Get backlink statistics - β
backlinks- List backlinks (partial)
AI Agent Features:
- β
--list-commands- Full command tree as JSON - β
--dry-run- Validate requests without executing - β
--verbose- Debug mode - β Structured error responses with suggestions
Site Explorer (Remaining):
- β³
refdomains- Referring domains - β³
anchors- Anchor text distribution - β³
organic-keywords- Organic keyword rankings - β³
top-pages- Top-performing pages - β³
outlinks-stats- Outbound link stats
Other API Categories:
- β³ Keywords Explorer
- β³ SERP Overview
- β³ Rank Tracker
- β³ Site Audit
- β³ Brand Radar
Advanced Features:
- β³
--describeflag (JSON schema for each endpoint) - β³
--list-fieldsflag (available fields per endpoint) - β³ Shell completions (bash/zsh/fish)
- β³ Pre-built binaries via GitHub Actions
- β³ Docker image
Step 1: Discover Available Commands
ahrefs --list-commands
# Returns complete command tree with all flags and examples as JSONStep 2: Validate Before Execution
ahrefs site-explorer domain-rating \
--target ahrefs.com \
--date 2024-01-01 \
--dry-run
# Output: β Valid request. Would call: GET https://api.ahrefs.com/v3/...Step 3: Execute & Parse Structured Output
ahrefs site-explorer domain-rating \
--target ahrefs.com \
--date 2024-01-01 \
--format json
# Always returns: {"status":"success|error", "data":{...}, "meta":{...}}Step 4: Handle Errors Programmatically
{
"status": "error",
"error": {
"code": "AUTH_ERROR",
"message": "Invalid API key",
"suggestion": "Run 'ahrefs config set-key <your-key>' to configure",
"docs_url": "https://docs.ahrefs.com/..."
}
}# Get help at any level
ahrefs --help
ahrefs site-explorer --help
ahrefs site-explorer domain-rating --help
# Each command includes detailed examples
ahrefs site-explorer backlinks --help
# Switch output formats
ahrefs site-explorer domain-rating --target ahrefs.com --date 2024-01-01 --format table
# Save output to file
ahrefs site-explorer domain-rating --target ahrefs.com --date 2024-01-01 -o output.json
# Use verbose mode for debugging
ahrefs site-explorer domain-rating --target ahrefs.com --date 2024-01-01 --verboseAhrefs provides free test queries for ahrefs.com and wordcount.com:
# These are FREE (no API units consumed)
ahrefs site-explorer domain-rating --target ahrefs.com --date 2024-01-01
ahrefs site-explorer domain-rating --target wordcount.com --date 2024-01-01
# Results are capped at 100 rows
ahrefs site-explorer backlinks-stats --target ahrefs.com --date 2024-01-01Note: Full API access requires an Ahrefs Enterprise plan.
ahrefs-cli/
βββ cmd/ # Command implementations
β βββ root.go # Root command + --list-commands
β βββ config/ # Config management
β βββ siteexplorer/ # Site Explorer endpoints
βββ pkg/
β βββ client/ # HTTP client (87.7% test coverage!)
β β βββ client.go
β β βββ client_test.go
β βββ models/ # API response structs
β βββ output/ # Multi-format output (JSON/YAML/CSV/Table)
β βββ schema/ # JSON schema generator (planned)
β βββ validator/ # Request validation (planned)
βββ internal/
β βββ config/ # Config file I/O (~/.ahrefsrc)
βββ main.go
βββ Makefile # make build, test, install
βββ README.md
Design Decisions:
- Minimal deps: Only Cobra for CLI framework
- Stdlib only: HTTP, JSON, CSV, testing - all native Go
- No external test deps: Pure
testingpackage withhttptest - Structured errors: Always return JSON-parseable errors
- Go 1.25+
- Make (optional, for convenience targets)
# Build
make build
# Run tests
make test
# Get test coverage
make test-coverage
# Format code
make fmt
# Run all checks
make all- Add model to
pkg/models/ - Create command in
cmd/<category>/ - Wire up in
main.go - Add tests
- Update README
Example: See cmd/siteexplorer/siteexplorer.go:newDomainRatingCmd()
| Metric | Value |
|---|---|
| Go Version | 1.25+ |
| Dependencies | 2 (cobra, pflag) |
| Test Coverage | 87.7% (client) |
| Lines of Code | ~1,500 |
| Endpoints | 3 (more coming!) |
| Output Formats | 4 (JSON, YAML, CSV, Table) |
- All remaining Site Explorer endpoints
- Field selection support (
--select) - Filtering support (
--where) - Pagination support (
--limit,--offset)
- Keywords overview
- Volume history
- Related terms
- Volume by country
- SERP Overview endpoints
- Rank Tracker endpoints
- Site Audit endpoints
- Brand Radar endpoints
- Full test coverage (>90%)
- Shell completions
- Pre-built binaries for all platforms
- Docker image
- Comprehensive documentation
-
--describeflag (JSON schema introspection) -
--list-fieldsflag per endpoint
Contributions are very welcome! This is an open-source project built for the community.
- Fork the repo
- Create a feature branch (
git checkout -b feature/amazing-endpoint) - Write tests for new code
- Run tests (
make test) - Commit changes (
git commit -m 'Add amazing endpoint') - Push to branch (
git push origin feature/amazing-endpoint) - Open a Pull Request
- π New API endpoints
- π Bug fixes
- π Documentation improvements
- β More tests
- π¨ Code quality improvements
- π‘ Feature ideas (open an issue first!)
MIT License - see LICENSE file for details.
- Ahrefs for providing the API
- Cobra for the excellent CLI framework
- Go team for the amazing standard library
- Ahrefs API Docs: https://docs.ahrefs.com/docs/api/reference/introduction
- API v3 Overview: https://help.ahrefs.com/en/articles/6559232-about-api-v3-for-enterprise-plan
- Free Test Queries: https://docs.ahrefs.com/docs/api/reference/free-test-queries
- π Bug reports: Open an issue
- π‘ Feature requests: Open an issue
- π¬ Questions: Start a discussion
Built with β€οΈ for AI agents and humans alike
Made by amine β’ Star this repo if you find it useful!