app/ directory, Next.js 13+) and the Pages Router (pages/api/, Next.js 12 and below) are covered below.
Environment setup
Add your secret to.env.local. Next.js loads this file automatically and keeps its values server-side only:
App Router (Next.js 13+)
Create the two route handler files shown below. Each file exports a named HTTP-method function (GET or POST) — the App Router convention.
- challenge/route.ts
- verify/route.ts
createChallenge(5, 4, 60) generates 4 signed challenge tokens at difficulty 5, each valid for 60 seconds. The function returns an array of JWT strings; wrapping it in { challenges } matches the response contract the Ribaunt widget expects.Pages Router (Next.js 12 and below)
If you are using thepages/api/ directory, create the two handler files below. Each exports a default async function that receives NextApiRequest and NextApiResponse.
- pages/api/captcha/challenge.ts
- pages/api/captcha/verify.ts
Serverless and edge deployments
The default The
replayPrevention: 'local' mode stores used token IDs in an in-process Map. When your application runs as serverless functions or across multiple instances, each cold start begins with an empty store, so a token solved against one instance can be replayed against another.To prevent cross-instance replays, pass replayPrevention: 'remote' together with a replayStore adapter backed by an atomic distributed store (for example Redis with a SET NX operation):replayStore must implement a consume(jti: string, expiresAt: number): Promise<boolean> method that atomically returns true the first time a given jti is seen and false on any subsequent call.