Skip to content

Affonso

github-actions[bot] edited this page Dec 2, 2026 · 2 revisions

Affonso

Typed Affonso affiliate-marketing API client for Deno, Bun, Node.js and Cloudflare Workers. Run your affiliate program from your backend: create and manage affiliates and sign them into the hosted portal, track clicks, signups and conversions server-side (with refunds), manage referrals, commissions, bonuses, payouts and coupons, read your program, and verify webhook signatures.

JSR JSR Score

Overview

Affonso is an affiliate platform for SaaS. Its pixel tracks visits in the browser; this connect covers the server side — the REST API at https://api.affonso.io/v1 and the signed webhooks it sends back.

Area Methods
Affiliates listAffiliates, listAllAffiliates, getAffiliate, createAffiliate, updateAffiliate, deleteAffiliate, createPortalToken
Tracking trackClick, recordSignup, createConversion, refundConversion
Referrals listReferrals, listAllReferrals, getReferral, createReferral, updateReferral, deleteReferral
Commissions listCommissions, listAllCommissions, getCommission, createCommission, createBonus, updateCommission, deleteCommission
Payouts listPayouts, listAllPayouts, getPayout, updatePayout
Coupons listCoupons, listAllCoupons, getCoupon, createCoupon, deleteCoupon
Program getProgram
Webhooks verifyWebhook

Things worth knowing up front:

  • One API key, one program. The key (sk_live_...) selects the program, and its permissions (read:affiliates, write:referrals, ...) decide what it can call — a missing one is FORBIDDEN. Affonso has no sandbox yet, so every write lands in your real program.
  • Amounts are decimals in major units — 99.00 is ninety-nine dollars.
  • Field names follow the wire. Mostly snake_case; payouts and clicks are camelCase. List pagination is normalized for you (Affonso spells it both ways), and listAll* iterators walk every page.
import { Affonso } from '@tundraconnect/affonso';

const client = new Affonso({
  auth: { type: 'BEARER', token: 'sk_live_your_api_key' },
});

const program = await client.getProgram();
console.log(program.name, program.plan.tier);

Testing your code

Your tests don't need to fake HTTP. Have your code take an Affonso instance and pass in a stand-in that returns the shapes from @tundraconnect/affonso/schemas or throws a real AffonsoError:

import { AffonsoError } from '@tundraconnect/affonso/errors';

const outage = new AffonsoError('SERVICE_UNAVAILABLE', { status: 503 });
console.log(outage.code, outage.transient);

To exercise the client itself against a fake transport, pass a fetch function in the constructor options. The client sends every request through it, so the same option fits a proxy, a tracing wrapper or an SSRF-pinned fetch.

Documentation

Topic Description
API Client configuration and endpoint methods
Errors Error codes and diagnostic metadata
Schemas Public Guardian schemas and inferred types

Upstream

Installation

Deno:

deno add jsr:@tundraconnect/affonso

Bun:

bunx jsr add @tundraconnect/affonso

Node.js:

npx jsr add @tundraconnect/affonso

Quick Start

1. Onboard an affiliate

const affiliate = await client.createAffiliate({
  name: 'Jane Smith',
  email: 'jane@example.com',
  tracking_id: 'jane-smith', // their ?ref= code; generated when omitted
  payout_method: 'paypal',
  payout_details: { email: 'jane@example.com' },
});

// Later: a one-click, 5-minute link into the hosted affiliate portal.
const { portalUrl } = await client.createPortalToken(affiliate.id);
console.log(portalUrl);

2. Track a referral without the pixel

// A visitor arrives on an affiliate link: record the click, keep its id.
const click = await client.trackClick({
  trackingId: 'jane-smith',
  referrer: 'https://janes-blog.example/review',
  utmSource: 'blog',
});

// They sign up: the click becomes a lead.
const lead = await client.recordSignup({
  click_id: click.id,
  email: 'customer@example.com',
  external_user_id: 'user_42',
});
if (lead.status === 'rejected') console.warn('blocked by a fraud rule');

3. Credit a sale

Affonso finds the referral, matches the incentive and calculates the commission. external_event_id makes retries safe — the same value returns the existing conversion instead of creating a second one.

const conversion = await client.createConversion({
  external_user_id: 'user_42',
  sale_amount: 99,
  sale_amount_currency: 'USD',
  external_event_id: 'in_2026_000145', // e.g. your invoice id
  is_subscription: true,
  interval: 'monthly',
});
console.log(conversion.commission_created, conversion.commission_amount);

// The customer is refunded half: the commission shrinks with it.
await client.refundConversion(conversion.id, {
  amount: 49.5,
  external_event_id: 're_2026_000017',
});

4. Pay affiliates

for await (const payout of client.listAllPayouts({ status: 'pending' })) {
  // ...send the money, then record it. Completing a payout marks every
  // commission in it paid.
  await client.updatePayout(payout.id, {
    status: 'processing',
  });
}

5. Verify webhooks

Pass the raw body — await req.text(), never re-serialized JSON — and the endpoint's signing secret, as-is:

async function handle(req: Request): Promise<Response> {
  const event = await client.verifyWebhook({
    payload: await req.text(),
    headers: req.headers,
    secret: 'your-64-char-hex-signing-secret',
  });
  if (event.type === 'referral.converted') {
    console.log('converted', event.data.referralId);
  }
  return Response.json({ received: true });
}
console.log(typeof handle);

Deliveries are retried for up to 48 hours, so dedupe on event.id.

License

MIT. Part of Tundra Connect, typed vendor API clients for Deno, Bun, Node.js and Cloudflare Workers.

Clone this wiki locally