Skip to content

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

fix(similarweb): use the v4 referral report contract

Rudy Celekliee7c1dbf8004
diff --git a/tests/clis/similarweb-referrals.test.cjs b/tests/clis/similarweb-referrals.test.cjs
new file mode 100644
index 0000000..3e0d581
--- /dev/null
+++ b/tests/clis/similarweb-referrals.test.cjs
@@ -0,0 +1,16 @@
+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/similarweb.js')
+function run(args,key='fixture-key',oracle="throw new Error('unexpected fetch')") {
+ const code=`global.fetch=async(url,options)=>{const assert=require('node:assert/strict');const u=new URL(url);${oracle};return new Response(JSON.stringify({accepted:true}));};process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ const r=spawnSync(process.execPath,['-e',code],{encoding:'utf8',timeout:10000,env:{...process.env,SIMILARWEB_API_KEY:key}});assert.equal(r.status,0,r.stderr);return JSON.parse(r.stdout)
+}
+const args=['referrals','--domain','example.com','--start','2026-01','--end','2026-02']
+test('referrals use v4 and the required country while preserving dates',()=>assert.equal(run([...args,'--country','us'],'fixture-key',"assert.equal(u.pathname,'/v4/website/example.com/traffic-sources/referrals');assert.equal(u.searchParams.get('country'),'us');assert.equal(u.searchParams.get('start_date'),'2026-01');assert.equal(u.searchParams.get('end_date'),'2026-02');").accepted,true))
+test('referrals default to worldwide country scope',()=>assert.equal(run(args,'fixture-key',"assert.equal(u.searchParams.get('country'),'world');").accepted,true))
+test('referral preview uses v4 and masks only the key parameter',()=>{const p=run([...args,'--dry-run'],'example.com');const u=new URL(p.url);assert.equal(u.pathname,'/v4/website/example.com/traffic-sources/referrals');assert.equal(u.searchParams.get('api_key'),'***')})
+test('query authentication retains special characters without creating another parameter',()=>assert.equal(run([...args,'--country','us'],'fixture+/=&injected=yes',"assert.equal(u.searchParams.get('api_key'),'fixture+/=&injected=yes');assert.equal(u.searchParams.has('injected'),false);").accepted,true))
+test('encoded credentials are redacted in previews',()=>{const p=run([...args,'--dry-run'],'fixture+/=&');assert.equal(new URL(p.url).searchParams.get('api_key'),'***');assert.equal(p.url.includes('fixture'),false)})
+test('existing visits endpoint retains its v1 path and country',()=>assert.equal(run(['traffic','visits','--domain','example.com','--start','2026-01','--end','2026-02','--country','us'],'fixture-key',"assert.equal(u.pathname,'/v1/website/example.com/total-traffic-and-engagement/visits');assert.equal(u.searchParams.get('country'),'us');").accepted,true))
diff --git a/tools/clis/similarweb.js b/tools/clis/similarweb.js
index 9c1f9e8..24c80a7 100755
--- a/tools/clis/similarweb.js
+++ b/tools/clis/similarweb.js
@@ -2,18 +2,20 @@
const rawArgs = process.argv.slice(2)
const API_KEY = process.env.SIMILARWEB_API_KEY
-const BASE_URL = 'https://api.similarweb.com/v1'
+const BASE_URL = 'https://api.similarweb.com'
if ((!API_KEY) && rawArgs.length > 0) {
console.error(JSON.stringify({ error: 'SIMILARWEB_API_KEY environment variable required' }))
process.exit(1)
}
-async function api(method, path, body) {
- const separator = path.includes('?') ? '&' : '?'
- const url = `${BASE_URL}${path}${separator}api_key=${API_KEY}`
+async function api(method, path, body, version = 'v1') {
+ const url = new URL(`${BASE_URL}/${version}${path}`)
+ url.searchParams.set('api_key', API_KEY)
if (args['dry-run']) {
- return { _dry_run: true, method, url: url.replace(API_KEY, '***'), headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: body || undefined }
+ const previewUrl = new URL(url)
+ previewUrl.searchParams.set('api_key', '***')
+ return { _dry_run: true, method, url: previewUrl.toString(), headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: body || undefined }
}
const res = await fetch(url, {
method,
@@ -125,8 +127,8 @@ async function main() {
if (!args.start) { result = { error: '--start required (YYYY-MM)' }; break }
if (!args.end) { result = { error: '--end required (YYYY-MM)' }; break }
const params = new URLSearchParams({ start_date: args.start, end_date: args.end })
- if (args.country) params.set('country', args.country)
- result = await api('GET', `/website/${encodeURIComponent(domain)}/traffic-sources/referrals?${params.toString()}`)
+ params.set('country', args.country || 'world')
+ result = await api('GET', `/website/${encodeURIComponent(domain)}/traffic-sources/referrals?${params.toString()}`, undefined, 'v4')
break
}
diff --git a/tools/integrations/similarweb.md b/tools/integrations/similarweb.md
index b65c251..ac1f860 100644
--- a/tools/integrations/similarweb.md
+++ b/tools/integrations/similarweb.md
@@ -52,7 +52,7 @@ GET https://api.similarweb.com/v1/website/example.com/traffic-sources/overview?a
### Top Referral Sites
```bash
-GET https://api.similarweb.com/v1/website/example.com/traffic-sources/referrals?api_key={key}&start_date=2024-01&end_date=2024-03
+GET https://api.similarweb.com/v4/website/example.com/traffic-sources/referrals?api_key={key}&start_date=2024-01&end_date=2024-03&country=world
```
### Organic Keywords
@@ -148,3 +148,17 @@ GET https://api.similarweb.com/v1/website/example.com/geo/traffic-by-country?api
- competitors
- ads
- content-strategy
+
+## CLI referral reports
+
+Referral reports use the endpoint-specific `v4` route. The required country
+scope defaults to `world`; specify `--country us` (or another permitted country)
+to select a market. Other existing CLI routes retain their version.
+
+```bash
+node tools/clis/similarweb.js referrals --domain example.com --start 2026-01 --end 2026-02 --country us
+```
+
+API keys are encoded as a single query parameter and masked by parameter name
+in previews, even when their value matches part of the requested domain.
+Contract: [Referrals — Desktop](https://developers.similarweb.com/reference/referrals-desktop).