Skip to content

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

fix(rewardful): use form-encoded mutations and the affiliate link resource

Rudy Celekli15188b6f074d
diff --git a/tests/clis/rewardful-mutations.test.cjs b/tests/clis/rewardful-mutations.test.cjs
new file mode 100644
index 0000000..a1dc798
--- /dev/null
+++ b/tests/clis/rewardful-mutations.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/rewardful.js')
+function run(args, { status = 200, network = true } = {}) {
+ const code = `global.fetch = async (url, options) => {
+ if (!${network}) throw new Error('Unexpected network request');
+ return { status: ${status}, ok: ${status >= 200 && status < 300}, text: async () => JSON.stringify({ url, ...options }) };
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', env: { ...process.env, REWARDFUL_API_KEY: 'dummy-secret' } })
+}
+function output(r) { assert.equal(r.status, 0, r.stderr); return JSON.parse(r.stdout) }
+test('affiliate update uses the documented form wire format', () => {
+ const p = output(run(['affiliates', 'update', 'affiliate', '--first-name', 'Zoë & Co', '--last-name', 'A+B', '--paypal-email', '[email protected]']))
+ assert.equal(p.method, 'PUT'); assert.equal(p.headers['Content-Type'], 'application/x-www-form-urlencoded')
+ assert.equal(p.headers.Authorization, 'Basic ' + Buffer.from('dummy-secret:').toString('base64'))
+ assert.deepEqual(Object.fromEntries(new URLSearchParams(p.body)), { first_name: 'Zoë & Co', last_name: 'A+B', paypal_email: '[email protected]' })
+})
+test('create link uses affiliate_links with required affiliate_id in the form', () => {
+ const p = output(run(['links', 'create', '--affiliate-id', 'affiliate', '--token', 'campaign-1']))
+ assert.equal(p.url, 'https://api.getrewardful.com/v1/affiliate_links'); assert.equal(p.method, 'POST')
+ assert.deepEqual(Object.fromEntries(new URLSearchParams(p.body)), { affiliate_id: 'affiliate', token: 'campaign-1' })
+})
+test('provider-generated link token remains optional', () => {
+ const p = output(run(['links', 'create', '--affiliate-id', 'affiliate']))
+ assert.equal(new URLSearchParams(p.body).get('affiliate_id'), 'affiliate')
+ assert.equal(new URLSearchParams(p.body).has('token'), false)
+})
+test('unsupported destination URL is rejected rather than sent or silently ignored', () => {
+ const p = output(run(['links', 'create', '--affiliate-id', 'affiliate', '--url', 'https://example.test'], { network: false }))
+ assert.match(p.error, /url.*not supported/)
+})
+test('mutation preview is the same encoded wire body with masked authentication', () => {
+ const args = ['affiliates', 'update', '--id', 'affiliate', '--first-name', 'A&B']
+ const p = output(run(args)); const d = output(run([...args, '--dry-run'], { network: false }))
+ assert.equal(d.body, p.body); assert.equal(d.headers['Content-Type'], p.headers['Content-Type'])
+ assert.equal(d.headers.Authorization, '***'); assert.ok(!JSON.stringify(d).includes('dummy-secret'))
+})
+test('existing reads keep their query and no request body', () => {
+ const p = output(run(['affiliates', 'search', '--email', '[email protected]']))
+ assert.equal(new URL(p.url).searchParams.get('email'), '[email protected]'); assert.equal(p.method, 'GET')
+ assert.equal(p.body, undefined)
+})
+test('mutation failures exit unsuccessfully on HTTP 422', () => {
+ const r = run(['links', 'create', '--affiliate-id', 'affiliate'], { status: 422 })
+ assert.equal(r.status, 1); assert.match(r.stderr, /422/)
+})
diff --git a/tests/clis/rewardful-update-id.test.cjs b/tests/clis/rewardful-update-id.test.cjs
index 49dcf93..e6a32c3 100644
--- a/tests/clis/rewardful-update-id.test.cjs
+++ b/tests/clis/rewardful-update-id.test.cjs
@@ -6,7 +6,7 @@ function run(vendor, args, fetch = true) {
const cli = path.resolve(__dirname, `../../tools/clis/${vendor}.js`)
const source = `global.fetch = async (url, options) => {
if (!${fetch}) throw new Error('Unexpected network request');
- return { status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
+ return { status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? Object.fromEntries(new URLSearchParams(options.body)) : null }) };
}; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
return spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', timeout: 5000, env: {
...process.env, REWARDFUL_API_KEY: 'fake-key', RESEND_API_KEY: 'fake-key', POSTMARK_API_KEY: 'fake-key', BEEHIIV_API_KEY: 'fake-key', KLAVIYO_API_KEY: 'fake-key',
diff --git a/tools/clis/rewardful.js b/tools/clis/rewardful.js
index 78738b2..cc0ced2 100755
--- a/tools/clis/rewardful.js
+++ b/tools/clis/rewardful.js
@@ -11,18 +11,20 @@ if ((!API_KEY) && rawArgs.length > 0) {
async function api(method, path, body) {
const auth = 'Basic ' + Buffer.from(`${API_KEY}:`).toString('base64')
+ const encodedBody = body ? new URLSearchParams(body).toString() : undefined
if (args['dry-run']) {
- return { _dry_run: true, method, url: `${BASE_URL}${path}`, headers: { Authorization: '***', 'Content-Type': 'application/json' }, body: body || undefined }
+ return { _dry_run: true, method, url: `${BASE_URL}${path}`, headers: { Authorization: '***', 'Content-Type': 'application/x-www-form-urlencoded' }, body: encodedBody }
}
const res = await fetch(`${BASE_URL}${path}`, {
method,
headers: {
'Authorization': auth,
- 'Content-Type': 'application/json',
+ 'Content-Type': 'application/x-www-form-urlencoded',
},
- body: body ? JSON.stringify(body) : undefined,
+ body: encodedBody,
})
const text = await res.text()
+ if (res.status >= 400) throw new Error(`Rewardful request failed (HTTP ${res.status}): ${text}`)
try {
return JSON.parse(text)
} catch {
@@ -133,10 +135,10 @@ async function main() {
switch (sub) {
case 'create': {
if (!args['affiliate-id']) { result = { error: '--affiliate-id required' }; break }
- const body = {}
+ if (args.url) { result = { error: '--url is not supported by the affiliate link API; configure the campaign destination in Rewardful' }; break }
+ const body = { affiliate_id: args['affiliate-id'] }
if (args.token) body.token = args.token
- if (args.url) body.url = args.url
- result = await api('POST', `/affiliates/${args['affiliate-id']}/links`, body)
+ result = await api('POST', '/affiliate_links', body)
break
}
default:
@@ -151,7 +153,7 @@ async function main() {
affiliates: 'affiliates [list|get|search|update] [id] [--email <email>] [--id <id>] [--first-name <name>] [--last-name <name>] [--paypal-email <email>]',
referrals: 'referrals [list|get] [--affiliate-id <id>] [--stripe-customer-id <id>]',
commissions: 'commissions [list|get] [id] [--affiliate-id <id>]',
- links: 'links [create] [--affiliate-id <id>] [--token <token>] [--url <url>]',
+ links: 'links [create] [--affiliate-id <id>] [--token <token>]',
}
}
}
diff --git a/tools/integrations/rewardful.md b/tools/integrations/rewardful.md
index aed864f..373f373 100644
--- a/tools/integrations/rewardful.md
+++ b/tools/integrations/rewardful.md
@@ -8,13 +8,14 @@ Affiliate and referral tracking for Stripe-based SaaS businesses.
|-------------|-----------|-------|
| API | ✓ | REST API for affiliates, referrals, commissions |
| MCP | - | Not available |
-| CLI | - | Not available |
+| CLI | ✓ | [rewardful.js](../clis/rewardful.js) |
| SDK | - | API-only, JavaScript snippet for tracking |
## Authentication
-- **Type**: API Key
-- **Header**: `Authorization: Bearer {api_secret}`
+- **Type**: HTTP Basic authentication
+- **Username**: API secret; leave the password empty
+- **Header**: `Authorization: Basic {base64(api_secret + ":")}`
- **Get key**: Settings > API in Rewardful dashboard
## Common Agent Operations
@@ -64,26 +65,27 @@ GET https://api.getrewardful.com/v1/commissions?affiliate_id={affiliate_id}
### Create affiliate link
```bash
-POST https://api.getrewardful.com/v1/affiliates/{affiliate_id}/links
+POST https://api.getrewardful.com/v1/affiliate_links
+Content-Type: application/x-www-form-urlencoded
-{
- "token": "custom-link-token",
- "url": "https://example.com/pricing"
-}
+affiliate_id={affiliate_id}&token=custom-link-token
```
### Update affiliate
```bash
PUT https://api.getrewardful.com/v1/affiliates/{affiliate_id}
+Content-Type: application/x-www-form-urlencoded
-{
- "first_name": "John",
- "last_name": "Doe",
- "paypal_email": "[email protected]"
-}
+first_name=John&last_name=Doe&paypal_email=john%40example.com
```
+Mutation requests use form encoding, including spaces, Unicode, `+`, `&`, and email addresses. `--dry-run` shows the exact encoded request body with masked authentication. HTTP errors exit unsuccessfully.
+
+`links create --affiliate-id <id> [--token <token>]` passes the affiliate ID in the form body. The token is optional; Rewardful can generate one. The API does not accept a destination `--url`; configure the campaign destination in Rewardful. Additional affiliate links require a Growth or Enterprise plan.
+
+Primary contracts: [request format and authentication](https://developers.rewardful.com/rest-api/overview), [affiliate update](https://developers.rewardful.com/rest-api/affiliates/update), and [affiliate link creation](https://developers.rewardful.com/rest-api/affiliate-links/create).
+
## JavaScript Tracking
### Install snippet