Documentation / Integrations · Next.js
Next.js App Router: keep the secret on the server.
The App Router makes the server boundary explicit. Put the relay in a route handler, keep the secret unprefixed, and send the collector snapshot through it.
Last updated
The route handler
Create app/api/human-check/route.ts. It fetches a session manifest with the public key, scores the evidence with the secret, and returns only the fields the browser needs.
app/api/human-check/route.tsServer-only
import { NextRequest, NextResponse } from 'next/server';
export const runtime = 'nodejs';
const SCORE_URL = 'https://app.volance.com/api/trace/score';
const SESSION_URL = 'https://app.volance.com/api/trace/session';
async function getSession() {
const response = await fetch(SESSION_URL, {
headers: { authorization: 'Bearer ' + process.env.VOLANCE_PUBLIC_KEY },
});
if (!response.ok) throw new Error('session manifest failed');
return response.json();
}
export async function POST(request: NextRequest) {
const evidence = await request.json();
const session = await getSession();
const forwarded = request.headers.get('x-forwarded-for') || '';
const response = await fetch(SCORE_URL, {
method: 'POST',
headers: {
authorization: 'Bearer ' + process.env.VOLANCE_SECRET_KEY,
'content-type': 'application/json',
},
body: JSON.stringify({
publicKey: process.env.VOLANCE_PUBLIC_KEY,
sid: session.sessionId,
manifest: session.manifest,
page: evidence.page || '/',
clientSignals: evidence.clientSignals,
events: evidence.events,
requestSignals: {
visitorIp: forwarded.split(',')[0].trim() || undefined,
cookiePresent: request.headers.has('cookie'),
visitorUserAgent: request.headers.get('user-agent') || undefined,
acceptLanguage: request.headers.get('accept-language') || undefined,
proxyHeaders: forwarded ? ['x-forwarded-for'] : [],
},
}),
signal: AbortSignal.timeout(800),
});
if (!response.ok) return NextResponse.json({ degraded: true, action: 'flag' });
const result = await response.json();
return NextResponse.json({
requestId: result.requestId,
score: result.score,
verdict: result.verdict,
action: result.action,
why: result.signals
.filter((signal: { fired: boolean }) => signal.fired)
.map((signal: { id: string }) => signal.id),
});
}
The browser side
Client componentEvidence only, no secret
'use client';
export async function submitEvidence(collector: { stop: () => unknown }) {
const evidence = collector.stop();
const response = await fetch('/api/human-check', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
...(evidence as object),
page: window.location.pathname,
}),
});
return response.json();
}
Environment
.env.localServer-only variables
VOLANCE_PUBLIC_KEY=pk_...
VOLANCE_SECRET_KEY=sk_...
Never use NEXT_PUBLIC_ for the secret. That prefix inlines the value into the client bundle. Only the public key may be exposed.
What the route can and cannot see
| Available | Not available in the route handler |
|---|---|
Method, path, cookies, user agent, accept-language | The original header order |
sec-fetch-* headers | The client TLS fingerprint |
x-forwarded-for when the platform sets it | Mouse or typing; the collector has that |
Failure modes
| Failure | Reasonable response |
|---|---|
| Manifest fetch fails | Return a degraded decision; do not block a user on a vendor outage. |
| Score times out | Fail open on low-risk pages, step up or fail closed on spend. |
| 401 | Check the key; do not retry with the same key. |
| 429 | Back off; a 429 is not a billable score. |
Verify it
- Run in monitor mode and compare the route’s decisions with the portal split.
- Log
requestId, score, verdict, and action — not the evidence payload. - Run
grep -r "sk_" .next/staticafter a build; it should return nothing.
Related
- Integrations — the generic relay
- Collector & consent — the browser half
- Score endpoint — the full field list
- From score to policy — the policy side of the decision