Skip to content

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

feat(activecampaign): discover and set custom contact field values

Rudy Celekli7ef776e6f5c8
diff --git a/tests/clis/activecampaign-contact-fields.test.cjs b/tests/clis/activecampaign-contact-fields.test.cjs
new file mode 100644
index 0000000..2f1f968
--- /dev/null
+++ b/tests/clis/activecampaign-contact-fields.test.cjs
@@ -0,0 +1,78 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { createServer } = require('node:http')
+const { spawn } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/activecampaign.js')
+const values = [{ field: '12', value: 'Enterprise' }, { field: '13', value: '' }]
+
+async function run(args) {
+ const requests = []
+ const server = createServer(async (req, res) => {
+ let text = ''
+ for await (const chunk of req) text += chunk
+ requests.push({ method: req.method, url: new URL(req.url, 'http://fixture.test'),
+ body: text ? JSON.parse(text) : undefined, token: req.headers['api-token'] })
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ res.end(JSON.stringify({ fields: [{ id: '12', title: 'Plan', perstag: 'PLAN' }], contact: requests.at(-1).body?.contact }))
+ })
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve))
+ try {
+ const child = spawn(process.execPath, [cli, ...args], { env: { ...process.env,
+ ACTIVECAMPAIGN_API_KEY: 'owned-fixture-key', ACTIVECAMPAIGN_API_URL: `http://127.0.0.1:${server.address().port}` } })
+ let stdout = '', stderr = ''
+ child.stdout.on('data', chunk => { stdout += chunk })
+ child.stderr.on('data', chunk => { stderr += chunk })
+ const status = await new Promise(resolve => child.on('close', resolve))
+ return { status, payload: JSON.parse(stdout || stderr), requests }
+ } finally { await new Promise(resolve => server.close(resolve)) }
+}
+
+for (const sub of ['create', 'update', 'sync']) {
+ const args = ['contacts', sub, ...(sub === 'update' ? ['--id', '24'] : ['--email', '[email protected]'])]
+ test(`${sub} sends custom values inside the contact object`, async () => {
+ const result = await run([...args, '--field-values', JSON.stringify(values)])
+ assert.equal(result.status, 0)
+ assert.equal(result.requests.length, 1)
+ assert.equal(result.requests[0].method, sub === 'update' ? 'PUT' : 'POST')
+ assert.equal(result.requests[0].url.pathname, sub === 'sync' ? '/api/3/contact/sync' : sub === 'update' ? '/api/3/contacts/24' : '/api/3/contacts')
+ assert.equal(result.requests[0].token, 'owned-fixture-key')
+ assert.deepEqual(result.payload.contact.fieldValues, values)
+ })
+ test(`${sub} preview includes custom values without a request`, async () => {
+ const result = await run([...args, '--field-values', JSON.stringify(values), '--dry-run'])
+ assert.equal(result.status, 0)
+ assert.equal(result.requests.length, 0)
+ assert.deepEqual(result.payload.body.contact.fieldValues, values)
+ assert.equal(result.payload.headers['Api-Token'], '***')
+ })
+ test(`${sub} preserves the request when fields are omitted`, async () => {
+ const result = await run(args)
+ assert.equal(result.status, 0)
+ assert.deepEqual(result.payload.contact, sub === 'update' ? {} : { email: '[email protected]' })
+ })
+}
+test('field discovery preserves provider definitions and paginates', async () => {
+ const result = await run(['fields', 'list', '--limit', '10', '--offset', '20', '--search', 'Customer plan'])
+ assert.equal(result.status, 0)
+ assert.deepEqual(result.payload.fields, [{ id: '12', title: 'Plan', perstag: 'PLAN' }])
+ const url = result.requests[0].url
+ assert.equal(url.pathname, '/api/3/fields')
+ assert.equal(url.searchParams.get('limit'), '10')
+ assert.equal(url.searchParams.get('offset'), '20')
+ assert.equal(url.searchParams.get('search'), 'Customer plan')
+})
+test('field discovery preview makes no request', async () => {
+ const result = await run(['fields', 'list', '--dry-run'])
+ assert.equal(result.status, 0)
+ assert.equal(result.requests.length, 0)
+ assert.equal(new URL(result.payload.url).pathname, '/api/3/fields')
+})
+test('invalid field-values fail before changing a contact', async () => {
+ for (const input of ['[bad', '{}', 'null', '[null]', '[{"field":"12"}]', '[{"value":"x"}]']) {
+ const result = await run(['contacts', 'sync', '--email', '[email protected]', '--field-values', input])
+ assert.equal(result.status, 1)
+ assert.match(result.payload.error, /--field-values/)
+ assert.equal(result.requests.length, 0)
+ }
+})
diff --git a/tools/clis/activecampaign.js b/tools/clis/activecampaign.js
index 98c77b3..b4a3b39 100755
--- a/tools/clis/activecampaign.js
+++ b/tools/clis/activecampaign.js
@@ -60,6 +60,18 @@ function parseArgs(args) {
const args = parseArgs(rawArgs)
const [cmd, sub, ...rest] = args._
+function contactFieldValues() {
+ let values
+ try { values = JSON.parse(args['field-values']) } catch {
+ throw new Error('--field-values must be a JSON array of {field, value} objects')
+ }
+ if (!Array.isArray(values) || values.some(item => !item || typeof item !== 'object' || Array.isArray(item)
+ || !Object.hasOwn(item, 'field') || !Object.hasOwn(item, 'value'))) {
+ throw new Error('--field-values must be a JSON array of {field, value} objects')
+ }
+ return values
+}
+
async function main() {
let result
const limit = args.limit ? Number(args.limit) : 20
@@ -92,6 +104,7 @@ async function main() {
if (args['first-name']) contact.firstName = args['first-name']
if (args['last-name']) contact.lastName = args['last-name']
if (args.phone) contact.phone = args.phone
+ if (args['field-values'] !== undefined) contact.fieldValues = contactFieldValues()
result = await api('POST', '/contacts', { contact })
break
}
@@ -103,6 +116,7 @@ async function main() {
if (args['first-name']) contact.firstName = args['first-name']
if (args['last-name']) contact.lastName = args['last-name']
if (args.phone) contact.phone = args.phone
+ if (args['field-values'] !== undefined) contact.fieldValues = contactFieldValues()
result = await api('PUT', `/contacts/${id}`, { contact })
break
}
@@ -119,6 +133,7 @@ async function main() {
if (args['first-name']) contact.firstName = args['first-name']
if (args['last-name']) contact.lastName = args['last-name']
if (args.phone) contact.phone = args.phone
+ if (args['field-values'] !== undefined) contact.fieldValues = contactFieldValues()
result = await api('POST', '/contact/sync', { contact })
break
}
@@ -127,6 +142,15 @@ async function main() {
}
break
+ case 'fields':
+ if (sub !== 'list') { result = { error: 'Unknown fields subcommand. Use: list' }; break }
+ {
+ const params = new URLSearchParams({ limit: String(limit), offset: String(offset) })
+ if (args.search) params.set('search', args.search)
+ result = await api('GET', `/fields?${params}`)
+ }
+ break
+
case 'lists':
switch (sub) {
case 'list': {
@@ -413,7 +437,8 @@ async function main() {
result = {
error: 'Unknown command',
usage: {
- contacts: 'contacts [list | get --id <id> | create --email <email> | update --id <id> | delete --id <id> | sync --email <email>]',
+ contacts: 'contacts [list | get --id <id> | create --email <email> | update --id <id> | delete --id <id> | sync --email <email>]; create/update/sync: [--field-values <JSON array of {field,value}>]',
+ fields: 'fields list [--search <title>] [--limit <n>] [--offset <n>]',
lists: 'lists [list | get --id <id> | create --name <name> | delete --id <id> | subscribe --list-id <lid> --contact-id <cid> | unsubscribe --list-id <lid> --contact-id <cid>]',
campaigns: 'campaigns [list | get --id <id>]',
deals: 'deals [list | get --id <id> | create --title <title> | update --id <id> | delete --id <id>]',
diff --git a/tools/integrations/activecampaign.md b/tools/integrations/activecampaign.md
index 6b8dbe6..6d9869b 100644
--- a/tools/integrations/activecampaign.md
+++ b/tools/integrations/activecampaign.md
@@ -2,6 +2,38 @@
Email marketing automation platform with CRM, contacts, deals pipeline, tags, automations, and campaign management.
+## Custom contact fields from the CLI
+
+Discover the account's existing contact field IDs before assigning values:
+
+```bash
+node tools/clis/activecampaign.js fields list --search 'Plan' --limit 20 --offset 0
+node tools/clis/activecampaign.js contacts sync --email [email protected] \
+ --field-values '[{"field":"12","value":"Enterprise"}]' --dry-run
+```
+
+Use the field definition's `id`, rather than its title, personalization tag,
+or a field option ID. `fields list` returns one page of provider definitions,
+including side-loaded options and relationships; use its `meta.total` and
+`--offset` to continue. Search is a partial title match.
+
+`--field-values` is available for contact `create`, `update`, and `sync`.
+It preserves the supplied array under `contact.fieldValues`. Format each
+value for the selected field type; the provider validates IDs, types,
+and permissions. Omitting the option preserves the existing request.
+
+Choose `sync` to create or update by email, `update --id` for an existing ID,
+and `create` when an existing email should be rejected. Custom fields do not
+subscribe a contact to a list or record marketing consent. Use the existing
+list operations only when the user has authorized the subscription.
+Preview with `--dry-run` before a user-authorized write; the preview masks
+the API token and makes no request.
+
+See the official [contact creation contract](https://developers.activecampaign.com/reference/create-a-new-contact),
+[contact update contract](https://developers.activecampaign.com/reference/update-a-contact-new),
+[email upsert contract](https://developers.activecampaign.com/reference/sync-a-contacts-data),
+and [field discovery contract](https://developers.activecampaign.com/reference/retrieve-fields).
+
## Capabilities
| Integration | Available | Notes |