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

Pass what you have; omit the rest.
AvailableNot available in the route handler
Method, path, cookies, user agent, accept-languageThe original header order
sec-fetch-* headersThe client TLS fingerprint
x-forwarded-for when the platform sets itMouse or typing; the collector has that

Failure modes

The route is a dependency; decide what it does when the dependency is slow.
FailureReasonable response
Manifest fetch failsReturn a degraded decision; do not block a user on a vendor outage.
Score times outFail open on low-risk pages, step up or fail closed on spend.
401Check the key; do not retry with the same key.
429Back 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/static after a build; it should return nothing.

Related

Free to start. 10,000 scores a month, one project, no credit card.

Get started free