ProctorKit

Docs / Guides

Add proctoring to a Next.js quiz

This guide adds ProctorKit to a quiz built with Next.js and the App Router. The proctor runs in a client component. For paid use you add one route handler that creates each candidate's session with your secret server key. ProctorKit can be imported and created during server rendering; its methods only run in the browser after the candidate clicks Start.

1. Install

npm install proctorkit

2. Quickest start: a page key

Create an account, choose Record or Record + AI review, add your site's domain, and copy the pk_pub_ key into .env.local:

NEXT_PUBLIC_PROCTORKIT_KEY=pk_pub_...
// app/test/quiz.tsx
'use client';
import { useEffect, useState } from 'react';
import { createProctor } from 'proctorkit';

export default function Quiz({
  user,
  questions,
}: {
  user: { id: string; name: string; email?: string };
  questions: { id: string; text: string }[];
}) {
  // once per attempt; the dashboard shows the candidate by name
  const [proctor] = useState(() =>
    createProctor({
      key: process.env.NEXT_PUBLIC_PROCTORKIT_KEY,
      candidate: user,
      test: { id: 'round-1', name: 'Screening round 1' },
    }),
  );
  const [started, setStarted] = useState(false);
  const [i, setI] = useState(0);

  const begin = async () => {
    const check = await proctor.runChecks(); // system check and consent
    if (check.ok && proctor.start()) setStarted(true);
  };

  useEffect(() => {
    if (started) proctor.setContext({ questionId: questions[i].id });
  }, [started, i]);

  const submit = () => {
    proctor.stop(); // then save the answers
  };

  if (!started) return <button onClick={begin}>Start test</button>;
  return <div>{/* render questions[i]; call submit() at the end */}</div>;
}

Render <Quiz /> from any page. Results appear in your dashboard.

3. For paid use: create sessions on the server

A server key (pk_live_) is secret, so it stays in a route handler. The page asks your route for a session and passes the short-lived token to the proctor.

# .env.local (never NEXT_PUBLIC_)
PROCTORKIT_API_KEY=pk_live_...
// app/api/proctor-session/route.ts
import { NextResponse } from 'next/server';

export async function POST(req: Request) {
  // your own auth: { id, name, email }
  const user = await currentUser();
  const r = await fetch('https://proctorkit.com/api/v1/sessions', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.PROCTORKIT_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      candidate: { id: user.id, name: user.name, email: user.email },
      test: { id: 'round-1', name: 'Screening round 1' },
    }),
  });
  const s = await r.json();
  if (!r.ok) return NextResponse.json({ error: s.error }, { status: 502 });
  // keep s.sessionId and s.reportUrl on your side (database), never in the page
  return NextResponse.json({
    endpoint: s.endpoint,
    sessionToken: s.sessionToken,
    settings: s.settings,
    organisation: s.organisation,
    retentionDays: s.retentionDays,
  });
}
// in the client component, instead of the page key
const s = await fetch('/api/proctor-session', { method: 'POST' }).then((r) =>
  r.json(),
);
const proctor = createProctor({
  cloud: { endpoint: s.endpoint, sessionToken: s.sessionToken },
  settings: s.settings,
  consent: { organisation: s.organisation, retentionDays: s.retentionDays },
});

Create this proctor once per attempt, after the session response arrives: fetch the session in a parent component and mount the quiz only when it is there, so useState(() => createProctor(...)) runs exactly once (example in the docs). proctor.sessionId stays null here; your server kept the ID. Everything else (runChecks(), start(), setContext(), stop()) is the same. Pass endpoint to the page exactly as you received it. Read the result later with GET /v1/sessions/{sessionId}, or set a webhook to be told when it is ready.

Good to know

See the React guide for a timer example, and the full docs for every option, event and response.