Skip to content

Install

Track clicks and signups on your server

For developers: record affiliate clicks in server middleware and bind signups to affiliates with the EzyPromote binding API and a secret key.

Two jobs your server can do

This guide is for developers. Your server can do two things for EzyPromote:

  1. Record clicks in middleware, instead of or next to the website tag. A server sees visits that end before the page loads and visitors whose browsers block tracking scripts. Its cookie is HttpOnly and set by your server, so Safari doesn’t cut it to 7 days the way it does for cookies set by scripts.
  2. Bind signups. When a referred visitor creates an account, your server tells EzyPromote. Every later payment for that user then credits the same affiliate. This is the “Sign up first, pay later” option in Match payments to affiliates.

Keep the website tag as well if you sell through Stripe payment links or run on several domains. Only the tag can add clicks to those links, because the server’s cookie can’t be read by scripts.

Keys and secrets

Name Looks like Used for Where it lives
Publishable key pk_live_…, pk_test_… Reporting clicks Safe to publish. Your website tag shows it too.
Secret key sk_live_…, sk_test_… Binding signups Your server only. Shown once.
Tracking secret a random string Signing click cookies, so visitors can’t change who gets credit Your server only

Every key belongs to one mode: test or live. The _test_ or _live_ in the key decides which data a request touches.

Create a secret key

  1. Open Settings → Developers and stay on the API keys tab.
  2. Turn the Test data switch on to create a test key, or off for a live key. The page says “Showing keys for test data” or “live data”.
  3. Choose Create secret key.
  4. In Name, type the system that uses it, such as “Signup server”, and choose Create key.
  5. Copy the key. It is shown only once.
  6. Switch mode and repeat, so you have both an sk_test_ and an sk_live_ key.

Use one secret key per system, so you can revoke one without breaking the others.

Get the tracking secret

On the same tab, under Tracking secret, choose Reveal and copy it. You can also choose Show tracking secret in the server tracking section under Website tag.

To rotate it, choose Rotate and deploy the new secret right away. Clicks signed with the old secret keep counting for your longest cookie window. If the old secret leaked, rotate twice to stop accepting it at once. A site that still signs with the old secret can set EZYPROMOTE_PREVIOUS_TRACKING_SECRET until it is updated.

Environment variables

The snippets read these. The app fills in your keys and API address when you copy them.

EZYPROMOTE_TRACKING_SECRET=YOUR_TRACKING_SECRET
EZYPROMOTE_PUBLISHABLE_KEY=pk_live_YOUR_KEY
EZYPROMOTE_TEST_PUBLISHABLE_KEY=pk_test_YOUR_KEY
EZYPROMOTE_SECRET_KEY=sk_live_YOUR_KEY
EZYPROMOTE_TEST_SECRET_KEY=sk_test_YOUR_KEY
EZYPROMOTE_API_URL=https://api.ezypromote.com

Set both test keys. A visit through your test link (?via=test) is then recorded and credited in test data, so you can test on your production site. Every other visit uses the live keys. If you turned on “Visitors you reached first stay yours” in Settings → Tracking and payments, also set EZYPROMOTE_FIRST_REACH=true, or pass firstReach: true as the snippets do.

Install the SDK

EzyPromote’s server SDK, @ezypromote/node, isn’t on npm yet. Install it from the file your EzyPromote API serves. Copy the exact command from the Install tab of the server tracking code in the app. It looks like this:

npm install https://api.ezypromote.com/sdk/node/ezypromote-node-VERSION.tgz

It needs a Node runtime: Node 20 or later, Bun, Deno, or Cloudflare Workers with nodejs_compat. It doesn’t run on Vercel’s Edge runtime.

Record clicks

  1. In Settings → Developers, under Website tag, open Record clicks on your server instead (for developers).
  2. Choose your framework: Next.js, Astro, TanStack Start or Node.
  3. Copy the files. They are filled in with your cookie domain and cookie lifetime.
  4. Choose Use server tracking.
  5. Deploy, then choose Check my site and open your test link.

The middleware reads ?via=, signs the click locally, sets an HttpOnly cookie and reports the click in the background. It never waits for EzyPromote and never throws, so your page always renders.

// proxy.ts (Next.js 16; middleware.ts before 16)
import { NextResponse, type NextFetchEvent, type NextRequest } from 'next/server'
import { trackClick } from '@ezypromote/node/next'

export function proxy(request: NextRequest, event: NextFetchEvent) {
  const response = NextResponse.next()
  trackClick(request, response, { cookieDomain: '.yourstore.com', cookieDays: 60 }, event)
  return response
}

export const config = {
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
}
// src/middleware.ts (Astro, pages rendered on request only)
import { trackClicks } from '@ezypromote/node/astro'

export const onRequest = trackClicks({ cookieDomain: '.yourstore.com', cookieDays: 60 })
// server.ts (Express)
import { trackClicks } from '@ezypromote/node/express'

app.use(trackClicks({ cookieDomain: '.yourstore.com', cookieDays: 60 }))

For TanStack Start, Hono and other servers built on web Request and Response, the app’s TanStack Start tab uses trackRequest from @ezypromote/node/fetch. Set cookieDays to your longest campaign window: the app’s snippet already does.

Bind signups

Call EzyPromote right after you create the user record:

import { EzyPromote, referralContext } from '@ezypromote/node/referrals'

// Reads EZYPROMOTE_SECRET_KEY, and EZYPROMOTE_TEST_SECRET_KEY for signups through your test link
const ezypromote = new EzyPromote()

const result = await ezypromote.referrals.create({
  externalUserId: user.id, // your user ID: the binding key
  email: user.email,
  clickTokens: referralContext(req), // signed clicks from the visitor's cookies
  couponCode: req.body.coupon, // optional
})
// { bound: true, status: 'bound', method: 'api', affiliate: { code: 'maya' }, … }
// or { bound: false, reason: 'no_valid_click' }
  • Safe to retry. The first binding for an externalUserId wins. Later calls return that same result.
  • Never blocks your signup. The SDK never throws and waits 8 seconds at most in total.
  • Then tell Stripe. Put the same ID in Stripe as metadata.ep_user_id on the Customer or Checkout Session.

Without the SDK

Send a POST to /api/v1/referrals with your secret key and the visitor’s Cookie header:

curl https://api.ezypromote.com/api/v1/referrals \
  -H "Authorization: Bearer sk_live_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"externalUserId":"user_42","email":"[email protected]","cookieHeader":"COOKIE_HEADER_FROM_THE_SIGNUP_REQUEST"}'

The answer is {"data":{"bound":true,…}} or {"data":{"bound":false,"reason":"…"}}. Reasons include no_valid_click, expired_click, bot_click, other_mode_click and affiliate_inactive. Instead of cookieHeader, you can send clickTokens: the values of the ep and ep_js cookies, newest first.

Use your sk_test_ key for signups that came through your test link, or they come back as other_mode_click. The SDK picks the key for you. The limit is 1,200 requests a minute per key.

Next: Use another billing system if you don’t use Stripe, or Test a purchase and go live.