Setup guides

How to add an affiliate program to a Next.js app with Stripe

Add Anderro's snippet to your root layout with next/script, send sign-ups from your server, put the visitor ID on the Stripe Checkout Session, and add one Stripe webhook. Payments, renewals, refunds and disputes are then tracked.

Next.js and Stripe steps last checked December 4, 2026.

Start your program Pricing

Next.js and Stripe at a glance

Snippetapp/layout.tsx with next/script
Secret keyEnvironment variables (on Vercel: Project › Settings › Environment Variables)
PaymentsStripe Checkout, Payment Links, pricing tables
Plan neededAny host

Set up in 5 steps

1. Create your program on Anderro

Sign up, add your product and choose what affiliates earn. Anderro then shows your tracking snippet with your public key filled in. It also gives you a secret key for your server.

The public key is safe in browser code. The secret key is not: keep it in your platform's secrets as ANDERRO_SECRET_KEY, never in the page or the chat.

2. Add the snippet to the root layout

In app/layout.tsx, load the snippet with next/script so it runs on every page. With the Pages Router, put it in pages/_app.tsx.

By default the snippet waits for the visitor's consent before it sets its cookie. Call window.anderro.optIn() when they accept cookies. Your setup screen in Anderro tells you if your program is set to track without waiting.

// app/layout.tsx
import Script from "next/script";

// inside <body> of the root layout:
<Script
  src="https://track.anderro.com/a.js"
  data-key="YOUR_PUBLIC_KEY"
  strategy="afterInteractive"
/>

3. Add your secret key and record sign-ups

Add ANDERRO_SECRET_KEY to your environment variables (on Vercel: Project, Settings, Environment Variables), then redeploy. Never start its name with NEXT_PUBLIC_: those reach the browser.

In the route handler or server action that creates the account, send the sign-up with the visitor ID the browser sent.

// on your server, after the account is created
await fetch("https://track.anderro.com/events", {
  method: "POST",
  headers: {
    "content-type": "application/json",
    "x-api-key": process.env.ANDERRO_SECRET_KEY,
  },
  body: JSON.stringify({
    type: "signup",
    customerEmail: email,
    visitorId, // sent up by the browser from window.anderro?.getVisitorId()
  }),
});

4. Connect Stripe and pass the visitor ID

In Anderro, open your product, choose Integrations, then Stripe, and copy your webhook URL. In Stripe, open Developers, then Webhooks, add an endpoint with that URL, select the seven events Anderro lists, and paste the endpoint's signing secret back into Anderro. Anderro keeps no access to your Stripe account.

Stripe Checkout: in the route handler or server action that calls stripe.checkout.sessions.create, set the visitor ID as below. For one-off payments, also set it in payment_intent_data.metadata.

Stripe Payment Links: add the visitor ID to the link as client_reference_id.

// where your server creates the Stripe Checkout Session
const session = await stripe.checkout.sessions.create({
  // ...your existing options
  ...(visitorId && {
    client_reference_id: visitorId,
    metadata: { anderro_vid: visitorId },
  }),
});

// Stripe Payment Link: add the visitor id before sending the buyer on
const url = new URL("https://buy.stripe.com/your-link");
const vid = window.anderro?.getVisitorId();
if (vid) url.searchParams.set("client_reference_id", vid);
window.location.href = url.toString();

5. Publish and test

Deploy.

Then open your site through your own test referral link from Anderro, sign up and buy. The click, the sign-up and the sale show on your Anderro dashboard. Stripe's test mode works too: test payments never earn a commission.

How a sale is tied to an affiliate

The snippet remembers which affiliate sent a visitor and gives that visitor an ID. Your checkout passes the ID to Stripe, Stripe sends the sale to Anderro, and the affiliate who sent the customer is credited. Renewals need no extra code: they are matched to the customer from their first payment.

A customer who already bought through one affiliate stays with that affiliate, and an affiliate buying through their own link earns nothing unless you allow it.

What to know on Next.js and Stripe

  • Every Vercel preview deploy gets its own address. Anderro asks which address is your real site the first time it sees your key.
  • Subscriptions created through Stripe's API without a Checkout Session need your own code to report the sign-up to Anderro.
  • If several of your products share one Stripe account, each payment must name its product. Report those from your own code, or use one Stripe account per product.
  • If your code already reports payments to Anderro, remove those calls so Stripe is the only source.
  • US dollars only. A payment in another currency is not recorded, and Anderro's Integrations page tells you when one arrives.
  • Commissions wait out a hold you choose (7 to 90 days), so a refund inside the hold never costs you a commission.

If something does not show up

The key is undefined after I added it

Environment variables reach a deployment only after a redeploy. Never prefix it with NEXT_PUBLIC_.

Payments are counted twice

Your code and the Stripe webhook both report them. Remove the payment calls from your code so Stripe is the only source.

My test click does not show up

The snippet waits for consent: accept cookies on your site, and check that your cookie banner calls window.anderro.optIn(). If your site sets a Content Security Policy, allow https://track.anderro.com in script-src and connect-src.

A sale shows up, but no affiliate is credited

The visitor ID did not reach the checkout. Open the payment in your billing dashboard and check it carries the ID, and buy in the same browser you opened the referral link in.

Questions

Does Anderro need access to my Stripe account?

No. You add a webhook in your own Stripe account and Stripe pushes the events to Anderro. Every delivery must carry Stripe's signature, so anything Stripe did not sign is refused.

Which Stripe events does Anderro use?

Seven: checkout.session.completed, checkout.session.async_payment_succeeded, charge.succeeded, charge.captured, charge.refunded, charge.dispute.created and charge.dispute.closed.

Does Anderro pay my affiliates?

No. Anderro tracks clicks, sign-ups and sales and works out what each affiliate earned. You pay affiliates yourself and mark the payout paid in Anderro.

Do my affiliates need an account on this platform?

No. They join your program on Anderro and get their link there. Their link is your site's address ending in ?ref= and their code.

Sources

Next.js and Stripe's menus and plans change; these are the pages we checked.

  1. Next.js docs: Script component, checked December 4, 2026
  2. Stripe docs: Checkout Session client_reference_id, checked December 4, 2026
  3. Stripe docs: Payment Links URL parameters, checked December 4, 2026

Next.js, Vercel and Stripe are trademarks of their owners. Anderro is not affiliated with or endorsed by them.

Other setup guides

All setup guides · How tracking works