Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(ahrefs): complete backlink report requests

Rudy Celekli7c60d943a618
diff --git a/tests/clis/ahrefs-link-reports.test.cjs b/tests/clis/ahrefs-link-reports.test.cjs
new file mode 100644
index 0000000..9133b12
--- /dev/null
+++ b/tests/clis/ahrefs-link-reports.test.cjs
@@ -0,0 +1,42 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/ahrefs.js')
+function run(args, network = false) {
+ const code = `global.fetch = async (url, options) => {
+ if (!${network}) throw new Error('Unexpected request');
+ const parsed = new URL(url);
+ if (!['/v3/site-explorer/all-backlinks', '/v3/site-explorer/refdomains'].includes(parsed.pathname) || !parsed.searchParams.get('select')) throw new Error('Invalid link-report contract');
+ return new Response(JSON.stringify({url, method: options.method}));
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], {encoding: 'utf8', timeout: 5000, env: {...process.env, AHREFS_API_KEY: 'test-only-key'}})
+}
+for (const [command, route, selected] of [
+ ['backlinks', 'all-backlinks', 'url_from,url_to,anchor,is_dofollow'],
+ ['refdomains', 'refdomains', 'domain,domain_rating,links_to_target'],
+]) {
+ test(`${command} supplies required selected columns on the published route`, () => {
+ const r = run([command, 'list', '--target', 'example.com', '--limit', '5'], true)
+ assert.equal(r.status, 0, r.stderr)
+ const result = JSON.parse(r.stdout)
+ const parsed = new URL(result.url)
+ assert.equal(parsed.pathname, `/v3/site-explorer/${route}`)
+ assert.equal(parsed.searchParams.get('select'), selected)
+ assert.equal(parsed.searchParams.get('target'), 'example.com')
+ assert.equal(parsed.searchParams.get('limit'), '5')
+ assert.equal(result.method, 'GET')
+ })
+ test(`${command} respects explicit columns in previews without making a request`, () => {
+ const r = run([command, 'list', '--target', 'example.com', '--select', 'first_seen', '--dry-run'])
+ assert.equal(r.status, 0, r.stderr)
+ const result = JSON.parse(r.stdout)
+ assert.equal(new URL(result.url).searchParams.get('select'), 'first_seen')
+ assert.equal(result.headers.Authorization, '***')
+ })
+ test(`${command} rejects an incomplete column list before making a request`, () => {
+ const r = run([command, 'list', '--target', 'example.com', '--select', 'domain,'])
+ assert.notEqual(r.status, 0)
+ assert.match(JSON.parse(r.stderr).error, /select/)
+ })
+}
diff --git a/tools/clis/ahrefs.js b/tools/clis/ahrefs.js
index 5f49106..7634391 100755
--- a/tools/clis/ahrefs.js
+++ b/tools/clis/ahrefs.js
@@ -93,9 +93,9 @@ async function main() {
switch (sub) {
case 'list': {
if (!args.target) { result = { error: '--target required (domain or URL)' }; break }
- const params = new URLSearchParams({ target: args.target, mode })
+ const params = new URLSearchParams({ target: args.target, mode, select: reportSelect('url_from,url_to,anchor,is_dofollow') })
if (args.limit) params.set('limit', args.limit)
- result = await api('GET', `/site-explorer/backlinks?${params}`)
+ result = await api('GET', `/site-explorer/all-backlinks?${params}`)
break
}
default:
@@ -107,7 +107,7 @@ async function main() {
switch (sub) {
case 'list': {
if (!args.target) { result = { error: '--target required (domain or URL)' }; break }
- const params = new URLSearchParams({ target: args.target, mode })
+ const params = new URLSearchParams({ target: args.target, mode, select: reportSelect('domain,domain_rating,links_to_target') })
if (args.limit) params.set('limit', args.limit)
result = await api('GET', `/site-explorer/refdomains?${params}`)
break
@@ -192,8 +192,8 @@ async function main() {
error: 'Unknown command',
usage: {
'domain-rating': 'domain-rating get --target <domain> [--date <YYYY-MM-DD>]',
- 'backlinks': 'backlinks list --target <domain> [--mode <mode>] [--limit <n>]',
- 'refdomains': 'refdomains list --target <domain> [--mode <mode>] [--limit <n>]',
+ 'backlinks': 'backlinks list --target <domain> [--mode <mode>] [--select <columns>] [--limit <n>]',
+ 'refdomains': 'refdomains list --target <domain> [--mode <mode>] [--select <columns>] [--limit <n>]',
'keywords': 'keywords organic --target <domain> [--date <YYYY-MM-DD>] [--select <columns>] [--country <cc>] [--limit <n>]',
'top-pages': 'top-pages list --target <domain> [--date <YYYY-MM-DD>] [--select <columns>] [--country <cc>] [--limit <n>]',
'keyword-overview': 'keyword-overview get --keywords <kw1,kw2> [--country <cc>]',
diff --git a/tools/integrations/ahrefs.md b/tools/integrations/ahrefs.md
index 5db523c..944cc3b 100644
--- a/tools/integrations/ahrefs.md
+++ b/tools/integrations/ahrefs.md
@@ -38,7 +38,7 @@ Authorization: Bearer {api_token}
### Referring domains
```bash
-GET https://api.ahrefs.com/v3/site-explorer/refdomains?target=example.com&mode=domain&limit=100
+GET https://api.ahrefs.com/v3/site-explorer/refdomains?target=example.com&mode=domain&limit=100&select=domain,domain_rating,links_to_target
Authorization: Bearer {api_token}
```
@@ -46,7 +46,7 @@ Authorization: Bearer {api_token}
### Backlinks list
```bash
-GET https://api.ahrefs.com/v3/site-explorer/backlinks?target=example.com&mode=domain&limit=100
+GET https://api.ahrefs.com/v3/site-explorer/all-backlinks?target=example.com&mode=domain&limit=100&select=url_from,url_to,anchor,is_dofollow
Authorization: Bearer {api_token}
```
@@ -143,12 +143,23 @@ Authorization: Bearer {api_token}
## Dated reports in the CLI
-`domain-rating get`, `keywords organic`, and `top-pages list` require an explicit
-`--date YYYY-MM-DD`. Organic keywords and top pages also require `--select` with
-the columns you want to request. The CLI validates the calendar date and rejects
-missing or empty column lists before making a request; it does not guess paid
-columns. Choose fields from the provider response schema:
+`domain-rating get`, `keywords organic`, and `top-pages list` accept an explicit
+`--date YYYY-MM-DD`, defaulting to today's UTC date when omitted. Organic
+keywords and top pages supply default columns; `--select` overrides them.
+The CLI validates calendar dates and rejects empty column lists before a request.
+Choose fields from the provider response schema:
- [Domain rating](https://docs.ahrefs.com/en/api/reference/site-explorer/get-domain-rating)
- [Organic keywords](https://docs.ahrefs.com/en/api/reference/site-explorer/get-organic-keywords)
- [Top pages](https://docs.ahrefs.com/en/api/reference/site-explorer/get-top-pages)
+
+## Backlink report columns
+
+`backlinks list` calls `all-backlinks` and defaults to
+`url_from,url_to,anchor,is_dofollow`. `refdomains list` defaults to
+`domain,domain_rating,links_to_target`. Both APIs require `select`; use
+`--select` to choose other columns. These defaults omit columns marked with
+additional unit costs in the schemas. Requests still consume API units.
+
+- [Backlinks](https://docs.ahrefs.com/en/api/reference/site-explorer/get-all-backlinks)
+- [Refdomains](https://docs.ahrefs.com/en/api/reference/site-explorer/get-refdomains)