{"_id":"@afflyin/sdk","name":"@afflyin/sdk","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@afflyin/sdk","version":"0.0.1","description":"Official Afflyin SDK — load the affiliate tracking pixel in the browser and post conversions from your server.","license":"MIT","type":"module","sideEffects":false,"exports":{".":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"require":{"types":"./dist/index.d.cts","default":"./dist/index.cjs"}},"./server":{"import":{"types":"./dist/server.d.ts","default":"./dist/server.js"},"require":{"types":"./dist/server.d.cts","default":"./dist/server.cjs"}},"./package.json":"./package.json"},"main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","engines":{"node":">=18"},"publishConfig":{"access":"public"},"scripts":{"build":"tsup","dev":"tsup --watch","typecheck":"tsc --noEmit","prepublishOnly":"npm run build"},"keywords":["afflyin","affiliate","tracking","attribution","conversion","referral","nextjs"],"devDependencies":{"@types/node":"^22.9.0","tsup":"^8.3.5","typescript":"^5.6.3"},"gitHead":"ab763abed06aacb806855a25409fd76c6cc2d0a8","_id":"@afflyin/sdk@0.0.1","_nodeVersion":"25.8.1","_npmVersion":"11.11.0","dist":{"integrity":"sha512-tofRonnfm9kdD2ssS48IKW+78R+UujwcOdU7t0i6qYP/Xlg9Z8C9SmdLrsY+KsHN7/ndmlk+neq6M3qDWDXQzQ==","shasum":"3b9f21b755e5a1997bf9f1570535f14467cf5a04","tarball":"https://registry.npmjs.org/@afflyin/sdk/-/sdk-0.0.1.tgz","fileCount":19,"unpackedSize":72461,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIC45ElsnfIqH+lrwfNHRczuqWt5BgcI4esFvCnqxWYrrAiEA3FEtbukF+4LHiksstyqx/Re5FCECp0kq3LjeZlLoyLw="}]},"_npmUser":{"name":"afflyin","email":"afflyin@gmail.com"},"directories":{},"maintainers":[{"name":"afflyin","email":"afflyin@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/sdk_0.0.1_1780421805607_0.5047139578230446"},"_hasShrinkwrap":false}},"time":{"created":"2026-06-02T17:36:45.466Z","0.0.1":"2026-06-02T17:36:45.753Z","modified":"2026-06-02T17:36:45.944Z"},"maintainers":[{"name":"afflyin","email":"afflyin@gmail.com"}],"description":"Official Afflyin SDK — load the affiliate tracking pixel in the browser and post conversions from your server.","keywords":["afflyin","affiliate","tracking","attribution","conversion","referral","nextjs"],"license":"MIT","readme":"# @afflyin/sdk\n\nOfficial SDK for [Afflyin](https://afflyin.com) affiliate tracking. Two thin pieces:\n\n- **Browser** — a loader that injects Afflyin's hosted tracking pixel (the pixel\n  is served from Afflyin's CDN, never bundled, so attribution fixes ship\n  centrally without you re-deploying).\n- **Server** — a typed client for posting conversions from your backend.\n\nWorks in any JS app; the examples below use Next.js.\n\n```bash\nnpm install @afflyin/sdk\n```\n\n## Browser: load the pixel\n\nThe pixel auto-captures `?ref=` from the landing URL and stores a first-party\nattribution cookie. Load it once, app-wide.\n\n```tsx\n// app/layout.tsx\n'use client';\nimport { useEffect } from 'react';\nimport { loadAfflyin } from '@afflyin/sdk';\n\nexport function AfflyinPixel() {\n  useEffect(() => {\n    loadAfflyin({ apiKey: process.env.NEXT_PUBLIC_AFFLYIN_API_KEY! });\n  }, []);\n  return null;\n}\n```\n\nAt checkout, read the tracking id (or referral code) and persist it on the\norder so you can attribute the conversion server-side:\n\n```ts\nimport { getTrackingId, getReferralCode } from '@afflyin/sdk';\n\nconst trackingId = getTrackingId() ?? getReferralCode();\n// save `trackingId` with the order you create\n```\n\n> **Tip:** if you only persist one thing, persist the referral code. Afflyin\n> attributes from the bare `?ref=` code even when cookies and click beacons were\n> blocked — so sending it back at conversion makes attribution survive almost\n> anything.\n\n## Server: post the conversion\n\nCall this from a route handler, server action, or payment webhook — at the\nmoment **your** business rule says the order converted. It's idempotent on\n`orderId`, so retries are safe.\n\n```ts\n// app/api/afflyin/route.ts\nimport { createAfflyinClient } from '@afflyin/sdk/server';\n\nconst afflyin = createAfflyinClient({\n  apiKey: process.env.AFFLYIN_API_KEY!, // server-only secret\n});\n\nexport async function POST(req: Request) {\n  const order = await req.json();\n\n  const result = await afflyin.trackConversion({\n    trackingId: order.afflyinRef, // the tracking id / ref code you persisted\n    orderId: order.id,\n    orderValue: order.total,\n    currency: order.currency,\n    customerEmail: order.email,\n  });\n\n  return Response.json({ attributed: result.success });\n}\n```\n\n### Signed postbacks (optional)\n\nIf your program enabled `require_signed_postbacks`, pass the signing secret and\nevery conversion is HMAC-signed automatically:\n\n```ts\nconst afflyin = createAfflyinClient({\n  apiKey: process.env.AFFLYIN_API_KEY!,\n  signingSecret: process.env.AFFLYIN_SIGNING_SECRET!,\n});\n```\n\nThe `/server` entry targets the Node runtime (uses `node:crypto` for signing).\nIn Next.js, ensure the route is not on the Edge runtime when signing.\n\n## API\n\n| Export | Where | Purpose |\n| --- | --- | --- |\n| `loadAfflyin(opts)` | browser | Inject the hosted pixel (idempotent, SSR-safe). |\n| `getTrackingId()` | browser | Read the `_aff_trk` / `afflyin_tracking_id` cookie. |\n| `getReferralCode()` | browser | Read the captured referral code. |\n| `getReferralCodeFromUrl(search?)` | browser | Parse `?ref=`/`?r=`/… from a query string. |\n| `createAfflyinClient(opts)` | server | `trackConversion()` + `trackClick()`. |\n\n## License\n\nMIT\n","readmeFilename":"README.md","_rev":"1-008528438464c99f96156f3a37ca1cd4"}