Minimal Working Example
This is a complete, working integration of AutoProctor. Copy it, replace the credentials with your own, and you will have proctoring running on your site.
This example computes the hash in the browser for demonstration purposes. In production, you MUST compute the hash on your backend server and never expose your CLIENT_SECRET in frontend code.
The Full Example
// ── 1. Credentials ──────────────────────────────────
// Replace with your actual credentials
const CLIENT_ID = 'your-client-id'
const CLIENT_SECRET = 'your-client-secret' // ⚠️ Backend-only in production!
const TEST_ATTEMPT_ID = 'unique-id-from-your-database'
// Compute HMAC-SHA256 hash (move this to your backend in production)
async function computeHash(message, secret) {
const enc = new TextEncoder()
const key = await crypto.subtle.importKey(
'raw', enc.encode(secret),
{ name: 'HMAC', hash: 'SHA-256' }, false, ['sign']
)
const sig = await crypto.subtle.sign('HMAC', key, enc.encode(message))
return btoa(String.fromCharCode(...new Uint8Array(sig)))
}
const hashedTestAttemptId = await computeHash(TEST_ATTEMPT_ID, CLIENT_SECRET)
const credentials = {
clientId: CLIENT_ID,
testAttemptId: TEST_ATTEMPT_ID,
hashedTestAttemptId: hashedTestAttemptId
}
// ── 2. Proctoring Options ────────────────────────────
const proctoringOptions = {
trackingOptions: {
audio: true, // Detect background noise
numHumans: true, // Detect multiple faces or no face
tabSwitch: true, // Detect tab/window switches
photosAtRandom: true, // Capture random photos
}
}
// ── 3. Start Proctoring ─────────────────────────────
let apInst = new AutoProctor(credentials)
await apInst.setup(proctoringOptions)
apInst.start()
// Show your test content once monitoring begins
window.addEventListener('apMonitoringStarted', () => {
document.getElementById('test-content').style.display = 'block'
})
// ── 4. Stop Proctoring (when candidate submits) ─────
function submitTest() {
apInst.stop()
}
window.addEventListener('apMonitoringStopped', () => {
// Redirect to results or confirmation page
window.location.href = '/test-complete'
})
// ── 5. Report (on a separate page) ──────────────────
// const apInst = new AutoProctor(credentials)
// apInst.showReport(reportOptions)
Each numbered section corresponds to the sidebar navigation:
- Credentials → How Hashing Works, The Credentials Object
- Proctoring Options → Default Options and feature pages
- Start Proctoring → The Lifecycle
- Stop Proctoring → The Lifecycle
- Report → Viewing a Single Report
Interactive Playground
Toggle options interactively and see the code change in real time in the interactive Playground.