Skip to content

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

fix(trustpilot): use the invitation API host and payload

Rudy Celekli177e057675de
diff --git a/tests/clis/trustpilot-invitations.test.cjs b/tests/clis/trustpilot-invitations.test.cjs
new file mode 100644
index 0000000..89d6e64
--- /dev/null
+++ b/tests/clis/trustpilot-invitations.test.cjs
@@ -0,0 +1,15 @@
+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/trustpilot.js')
+function run(args,oracle="throw new Error('unexpected fetch')") {
+ const code=`global.fetch=async(url,options)=>{const assert=require('node:assert/strict');if(${JSON.stringify(args.includes('--dry-run'))})throw new Error('unexpected preview fetch');const u=new URL(url);if(u.pathname.includes('/oauth/')){assert.equal(u.host,'api.trustpilot.com');assert.equal(options.body,'grant_type=client_credentials');return new Response(JSON.stringify({access_token:'fixture-token'}));}const body=options.body?JSON.parse(options.body):undefined;${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,TRUSTPILOT_API_KEY:'fixture-key',TRUSTPILOT_API_SECRET:'fixture-secret',TRUSTPILOT_BUSINESS_UNIT_ID:'business1'}});assert.equal(r.status,0,r.stderr);return JSON.parse(r.stdout)
+}
+const emailArgs=['invitations','create','--email','[email protected]','--name','Jane','--template','t1','--sender-email','[email protected]','--sender-name','Team','--reply-to','[email protected]','--redirect-uri','https://example.com/thanks','--business-user-id','user1']
+test('email invitations use the invitation host and nested service review payload',()=>assert.equal(run(emailArgs,"assert.equal(u.href,'https://invitations-api.trustpilot.com/v1/private/business-units/business1/email-invitations');assert.equal(options.method,'POST');assert.equal(options.headers.Authorization,'Bearer fixture-token');assert.equal(options.headers['x-business-user-id'],'user1');assert.deepEqual(body,{consumerEmail:'[email protected]',consumerName:'Jane',referenceNumber:'',senderEmail:'[email protected]',senderName:'Team',replyTo:'[email protected]',type:'email',serviceReviewInvitation:{templateId:'t1',redirectUri:'https://example.com/thanks'}});").accepted,true))
+test('invitation link retains its distinct payload on the invitation host',()=>assert.equal(run(['invitations','link','--email','[email protected]','--name','Jane'],"assert.equal(u.host,'invitations-api.trustpilot.com');assert.equal(u.pathname,'/v1/private/business-units/business1/invitation-links');assert.deepEqual(body,{email:'[email protected]',name:'Jane',referenceId:'',redirectUri:'https://trustpilot.com'});").accepted,true))
+test('invitation templates use the invitation host',()=>assert.equal(run(['invitations','templates'],"assert.equal(u.href,'https://invitations-api.trustpilot.com/v1/private/business-units/business1/templates');assert.equal(options.method,'GET');").accepted,true))
+test('ordinary review reads still use the core API and key authentication',()=>assert.equal(run(['reviews','get','--id','r1'],"assert.equal(u.href,'https://api.trustpilot.com/v1/reviews/r1');assert.equal(options.headers.apikey,'fixture-key');assert.equal(options.headers.Authorization,undefined);").accepted,true))
+test('invitation preview retains nested fields and sends neither OAuth nor invitations',()=>{const p=run([...emailArgs,'--dry-run']);assert.equal(new URL(p.url).host,'invitations-api.trustpilot.com');assert.equal(p.body.serviceReviewInvitation.templateId,'t1');assert.equal(p.headers.Authorization,'***');assert.equal(p.headers['x-business-user-id'],'user1')})
diff --git a/tools/clis/trustpilot.js b/tools/clis/trustpilot.js
index b00aca3..5870ea5 100755
--- a/tools/clis/trustpilot.js
+++ b/tools/clis/trustpilot.js
@@ -5,6 +5,7 @@ const API_KEY = process.env.TRUSTPILOT_API_KEY
const API_SECRET = process.env.TRUSTPILOT_API_SECRET
const BUSINESS_UNIT_ID = process.env.TRUSTPILOT_BUSINESS_UNIT_ID
const BASE_URL = 'https://api.trustpilot.com/v1'
+const INVITATIONS_URL = 'https://invitations-api.trustpilot.com/v1'
if ((!API_KEY) && rawArgs.length > 0) {
console.error(JSON.stringify({ error: 'TRUSTPILOT_API_KEY environment variable required' }))
@@ -32,7 +33,7 @@ async function getAccessToken() {
return null
}
-async function api(method, path, body, auth = 'apikey') {
+async function api(method, path, body, auth = 'apikey', baseUrl = BASE_URL) {
if (args['dry-run']) {
const maskedHeaders = { 'Content-Type': 'application/json', 'Accept': 'application/json' }
if (auth === 'bearer') {
@@ -40,7 +41,8 @@ async function api(method, path, body, auth = 'apikey') {
} else {
maskedHeaders['apikey'] = '***'
}
- return { _dry_run: true, method, url: `${BASE_URL}${path}`, headers: maskedHeaders, body: body || undefined }
+ if (baseUrl === INVITATIONS_URL && args['business-user-id']) maskedHeaders['x-business-user-id'] = args['business-user-id']
+ return { _dry_run: true, method, url: `${baseUrl}${path}`, headers: maskedHeaders, body: body || undefined }
}
const headers = {
'Content-Type': 'application/json',
@@ -55,7 +57,8 @@ async function api(method, path, body, auth = 'apikey') {
} else {
headers['apikey'] = API_KEY
}
- const res = await fetch(`${BASE_URL}${path}`, {
+ if (baseUrl === INVITATIONS_URL && args['business-user-id']) headers['x-business-user-id'] = args['business-user-id']
+ const res = await fetch(`${baseUrl}${path}`, {
method,
headers,
body: body ? JSON.stringify(body) : undefined,
@@ -191,11 +194,15 @@ async function main() {
consumerName: name,
referenceNumber: args.reference || '',
senderEmail: args['sender-email'] || undefined,
+ senderName: args['sender-name'] || undefined,
replyTo: args['reply-to'] || undefined,
- templateId: templateId || undefined,
- redirectUri,
+ type: 'email',
+ serviceReviewInvitation: {
+ templateId: templateId || undefined,
+ redirectUri,
+ },
}
- result = await api('POST', `/private/business-units/${businessUnitId}/email-invitations`, payload, 'bearer')
+ result = await api('POST', `/private/business-units/${businessUnitId}/email-invitations`, payload, 'bearer', INVITATIONS_URL)
break
}
case 'link': {
@@ -209,12 +216,12 @@ async function main() {
name,
referenceId: args.reference || '',
redirectUri: args['redirect-uri'] || 'https://trustpilot.com',
- }, 'bearer')
+ }, 'bearer', INVITATIONS_URL)
break
}
case 'templates': {
if (!businessUnitId) { result = { error: '--business-unit or TRUSTPILOT_BUSINESS_UNIT_ID required' }; break }
- result = await api('GET', `/private/business-units/${businessUnitId}/templates`, null, 'bearer')
+ result = await api('GET', `/private/business-units/${businessUnitId}/templates`, null, 'bearer', INVITATIONS_URL)
break
}
default:
diff --git a/tools/integrations/trustpilot.md b/tools/integrations/trustpilot.md
index cd28dc2..9bf07a7 100644
--- a/tools/integrations/trustpilot.md
+++ b/tools/integrations/trustpilot.md
@@ -83,7 +83,7 @@ Headers:
### Send email invitation
```bash
-POST https://api.trustpilot.com/v1/private/business-units/{businessUnitId}/email-invitations
+POST https://invitations-api.trustpilot.com/v1/private/business-units/{businessUnitId}/email-invitations
Headers:
Authorization: Bearer {access_token}
@@ -92,14 +92,21 @@ Headers:
"consumerEmail": "[email protected]",
"consumerName": "Jane Doe",
"referenceNumber": "order-123",
- "redirectUri": "https://example.com/thanks"
+ "senderEmail": "[email protected]",
+ "senderName": "Team",
+ "replyTo": "[email protected]",
+ "type": "email",
+ "serviceReviewInvitation": {
+ "templateId": "<template_id>",
+ "redirectUri": "https://example.com/thanks"
+ }
}
```
### Generate review invitation link
```bash
-POST https://api.trustpilot.com/v1/private/business-units/{businessUnitId}/invitation-links
+POST https://invitations-api.trustpilot.com/v1/private/business-units/{businessUnitId}/invitation-links
Headers:
Authorization: Bearer {access_token}
@@ -115,7 +122,7 @@ Headers:
### List invitation templates
```bash
-GET https://api.trustpilot.com/v1/private/business-units/{businessUnitId}/templates
+GET https://invitations-api.trustpilot.com/v1/private/business-units/{businessUnitId}/templates
Headers:
Authorization: Bearer {access_token}
@@ -189,3 +196,17 @@ Headers:
- review-generation
- social-proof
- post-purchase-flow
+
+## CLI invitation requests
+
+Invitation creation, links, and templates use `invitations-api.trustpilot.com`;
+OAuth and review operations retain `api.trustpilot.com`. Email invitations put
+`templateId` and `redirectUri` inside `serviceReviewInvitation`, rather than at
+the top level. Links retain their separate `email`/`name`/`referenceId` payload.
+
+Supply `--business-user-id` for the `x-business-user-id` header when using the
+CLI's client credentials grant on behalf of an Admin or Manager. Use
+`--sender-name`, `--sender-email`, and `--reply-to` for email sender fields.
+No real invitation is sent by `--dry-run`.
+
+Contract: [Trustpilot Invitation API](https://developers.trustpilot.com/invitation-api).