Minimal Working Example

This is a complete, working integration of AutoProctor in a single HTML file. Save it, replace the two credentials with your own, open it on localhost, and you will have proctoring running.

The Full Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>AutoProctor: minimal example</title>
</head>
<body>
  <p id="status">Loading proctoring…</p>

  <!-- Your test. Hidden until monitoring has started. -->
  <div id="test-content" style="display: none">
    <h1>Sample test</h1>
    <p>What is 2 + 2?</p>
    <button id="submit-btn">Submit test</button>
  </div>

  <script>
    // ── 1. Credentials ──────────────────────────────────
    const CLIENT_ID = 'your-client-id'
    const CLIENT_SECRET = 'your-client-secret' // ⚠️ Backend-only in production!
    // Demo only: a fresh attempt on every page load. In production, use the
    // attempt's id from your database, so a reload resumes the same attempt.
    const TEST_ATTEMPT_ID = crypto.randomUUID()

    // HMAC-SHA256 of the message, base64-encoded (move this to your backend)
    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)))
    }

    // ── 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
      }
    }

    const statusEl = document.getElementById('status')
    const submitBtn = document.getElementById('submit-btn')

    // Report errors (camera blocked, unsupported browser, ...) to the candidate
    window.addEventListener('apErrorEvent', (e) => {
      statusEl.textContent = 'Proctoring error ' + e.detail.errorCode + ': ' + e.detail.errorDetails
    })

    // ── 3. Start Proctoring ─────────────────────────────
    async function startProctoring() {
      let apInst
      try {
        const hashedTestAttemptId = await computeHash(TEST_ATTEMPT_ID, CLIENT_SECRET)
        apInst = new AutoProctor({
          clientId: CLIENT_ID,
          testAttemptId: TEST_ATTEMPT_ID,
          hashedTestAttemptId: hashedTestAttemptId,
        })
        await apInst.setup(proctoringOptions)
      } catch (err) {
        statusEl.textContent = 'Could not set up proctoring: ' + err.message
        return
      }

      // Show the test only once monitoring has begun
      window.addEventListener('apMonitoringStarted', () => {
        statusEl.textContent = 'Proctoring is on.'
        document.getElementById('test-content').style.display = 'block'
      })

      // ── 4. Stop Proctoring (when the candidate submits) ─
      submitBtn.addEventListener('click', () => {
        submitBtn.disabled = true
        statusEl.textContent = 'Submitting…'
        apInst.stop()
      })

      // The final upload failed (e.g. the network dropped): let them retry
      window.addEventListener('apTestSubmissionFailed', () => {
        statusEl.textContent = 'Submission failed. Check your connection and submit again.'
        submitBtn.disabled = false
      })

      window.addEventListener('apMonitoringStopped', () => {
        statusEl.textContent = 'Test submitted.'
        document.getElementById('test-content').style.display = 'none'
        // In your app: redirect to a confirmation page here
      })

      apInst.start()
    }

    // The SDK loads asynchronously: wait for it before using AutoProctor
    if (window.AutoProctor) {
      startProctoring()
    } else {
      window.addEventListener('apLibReady', startProctoring, { once: true })
    }
  </script>

  <!-- Load the SDK after the listener above is registered. apVersionNum picks the
       version this example is written for; without it ap-entry.js loads its default. -->
  <script>var apVersionNum = '5.0.0'</script>
  <script src="https://cdn.autoproctor.co/ap-entry.js"></script>
</body>
</html>

Each numbered section corresponds to the sidebar navigation:

  1. Credentials → How Hashing Works, The Credentials Object
  2. Proctoring Options → Standard Proctoring and feature pages
  3. Start Proctoring → The Lifecycle
  4. Stop Proctoring → The Lifecycle
  5. Report → Viewing a Single Report. Render it on a separate page, with the same credentials:
const apInst = new AutoProctor(credentials)
apInst.showReport()

Interactive Playground

Toggle options interactively and see the code change in real time in the interactive Playground.