Skip to content

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

feat(customer-io): select workspace API region

Rudy Celekliac4f53e8340b
diff --git a/tests/clis/customerio-region.test.cjs b/tests/clis/customerio-region.test.cjs
new file mode 100644
index 0000000..629143a
--- /dev/null
+++ b/tests/clis/customerio-region.test.cjs
@@ -0,0 +1,48 @@
+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/customer-io.js')
+function run(args, region, oracle='') {
+ const fixture=`global.fetch=async(url,options)=>{const assert=require('node:assert/strict');${oracle}
+ return new Response(JSON.stringify({url,method:options.method,authorization:options.headers.Authorization,body:options.body?JSON.parse(options.body):undefined}),{status:200});
+ };process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ const env={...process.env,CUSTOMERIO_SITE_ID:'fixture-site',CUSTOMERIO_API_KEY:'fixture-track',CUSTOMERIO_APP_KEY:'fixture-app'}
+ delete env.CUSTOMERIO_REGION
+ if(region!==undefined)env.CUSTOMERIO_REGION=region
+ return spawnSync(process.execPath,['-e',fixture],{env,encoding:'utf8',timeout:10000})
+}
+function output(result){assert.equal(result.status,0,result.stderr);return JSON.parse(result.stdout)}
+test('EU identify uses regional Track host and preserves basic auth/body',()=>{
+ const result=output(run(['customers','identify','--id','owned-id','--email','[email protected]'],'eu'))
+ assert.equal(result.url,'https://track-eu.customer.io/api/v1/customers/owned-id');assert.equal(result.method,'PUT')
+ assert.equal(result.authorization,`Basic ${Buffer.from('fixture-site:fixture-track').toString('base64')}`);assert.deepEqual(result.body,{email:'[email protected]'})
+})
+test('EU customer reads use App host and bearer auth',()=>{
+ const result=output(run(['customers','get','--id','owned-id'],'eu'))
+ assert.equal(result.url,'https://api-eu.customer.io/v1/customers/owned-id/attributes');assert.equal(result.method,'GET');assert.equal(result.authorization,'Bearer fixture-app')
+})
+test('EU event requests retain event payload on regional Track host',()=>{
+ const result=output(run(['customers','track-event','--id','owned-id','--name','Trial Started','--data','{"plan":"pro"}'],'eu'))
+ assert.equal(result.url,'https://track-eu.customer.io/api/v1/customers/owned-id/events');assert.deepEqual(result.body,{name:'Trial Started',data:{plan:'pro'}})
+})
+test('EU transactional preview masks auth and makes no request',()=>{
+ const result=output(run(['send','email','--message-id','7','--to','[email protected]','--identifier-id','owned-id','--dry-run'],'eu',"throw new Error('unexpected request')"))
+ assert.equal(result.url,'https://api-eu.customer.io/v1/send/email');assert.equal(result.headers.Authorization,'***');assert.deepEqual(result.body,{transactional_message_id:'7',to:'[email protected]',identifiers:{id:'owned-id'}})
+})
+test('default region preserves US Track routing',()=>{
+ assert.equal(output(run(['customers','identify','--id','owned-id'])).url,'https://track.customer.io/api/v1/customers/owned-id')
+})
+test('explicit US preserves App routing',()=>{
+ assert.equal(output(run(['campaigns','list'],'us')).url,'https://api.customer.io/v1/campaigns')
+})
+test('unknown regions stop before sending or previewing',()=>{
+ for(const region of ['ap','EU','https://other.example'])for(const preview of [[],['--dry-run']]){
+ const result=run(['customers','identify','--id','owned-id',...preview],region,"throw new Error('unexpected request')")
+ assert.equal(result.status,1);assert.match(JSON.parse(result.stderr).error,/CUSTOMERIO_REGION/)
+ }
+})
+test('no-argument help remains available without credentials or valid region',()=>{
+ const result=spawnSync(process.execPath,[cli],{env:{CUSTOMERIO_REGION:'invalid'},encoding:'utf8'})
+ assert.equal(result.status,0);assert.ok(JSON.parse(result.stdout).usage)
+})
diff --git a/tools/clis/customer-io.js b/tools/clis/customer-io.js
index b72485f..7cb410a 100755
--- a/tools/clis/customer-io.js
+++ b/tools/clis/customer-io.js
@@ -5,8 +5,13 @@ const APP_KEY = process.env.CUSTOMERIO_APP_KEY
const SITE_ID = process.env.CUSTOMERIO_SITE_ID
const API_KEY = process.env.CUSTOMERIO_API_KEY
-const TRACK_URL = 'https://track.customer.io/api/v1'
-const APP_URL = 'https://api.customer.io/v1'
+const REGION = process.env.CUSTOMERIO_REGION || 'us'
+if (rawArgs.length > 0 && !['us', 'eu'].includes(REGION)) {
+ console.error(JSON.stringify({ error: 'CUSTOMERIO_REGION must be us or eu' }))
+ process.exit(1)
+}
+const TRACK_URL = REGION === 'eu' ? 'https://track-eu.customer.io/api/v1' : 'https://track.customer.io/api/v1'
+const APP_URL = REGION === 'eu' ? 'https://api-eu.customer.io/v1' : 'https://api.customer.io/v1'
const hasTrackAuth = SITE_ID && API_KEY
const hasAppAuth = APP_KEY
diff --git a/tools/integrations/customer-io.md b/tools/integrations/customer-io.md
index febf32d..8b4a7d8 100644
--- a/tools/integrations/customer-io.md
+++ b/tools/integrations/customer-io.md
@@ -185,3 +185,28 @@ _cio.page();
- emails
- onboarding
- analytics
+
+## CLI account region
+
+Set `CUSTOMERIO_REGION=eu` for a workspace hosted in Customer.io's EU region.
+The CLI then uses `track-eu.customer.io` for Track operations and
+`api-eu.customer.io` for App operations. Both request previews and actual
+requests use the selected region, retaining their existing Basic/Bearer
+authentication and request bodies.
+
+```bash
+export CUSTOMERIO_REGION=eu
+node tools/clis/customer-io.js customers get --id owned-customer-id --dry-run
+```
+
+The default is `us`; `CUSTOMERIO_REGION=us` explicitly retains the existing
+US hosts. Other values are rejected before requests. Check the workspace's
+region rather than guessing it from an email address or the user's location.
+This selects documented provider endpoints; it does not migrate a workspace
+or guarantee how the provider stores/processes data. Select scoped credentials
+for that workspace, and perform writes only when already authorized.
+
+Customer.io notes that sending EU Track requests to its US host can redirect
+but still pass through US servers. Use the regional host directly.
+See the official [Track server regions](https://docs.customer.io/integrations/api/track/#server-addresses-us-and-eu)
+and [App server regions](https://docs.customer.io/integrations/api/app/#server-addresses-us-and-eu).