Use with AI Assistants

If you use an AI coding assistant (Claude, GPT, Copilot, etc.), copy the following block and paste it into your assistant's context. It contains everything needed to get a basic integration working.

# AutoProctor SDK — Integration Context

AutoProctor is a JavaScript SDK that monitors candidates during online exams via camera, microphone, and screen share. It runs entirely in the browser.

## Loading the SDK

Add these script tags to your HTML, after the code that listens for `apLibReady`. `apVersionNum` pins SDK 5.0.0, which this context describes.

```html
<script>var apVersionNum = '5.0.0'</script>
<script src="https://cdn.autoproctor.co/ap-entry.js"></script>
```

## Authentication

- You receive a CLIENT_ID and CLIENT_SECRET from AutoProctor.
- NEVER expose CLIENT_SECRET in frontend code. Compute hashes on your backend.
- For each test attempt, compute an HMAC-SHA256 of the testAttemptId using CLIENT_SECRET as the key, and base64-encode the raw digest (not hex). In Node.js: `crypto.createHmac('sha256', CLIENT_SECRET).update(testAttemptId).digest('base64')`.

## The Integration

```js
// 1. Build credentials (hash must be computed server-side in production)
const credentials = {
    clientId: 'your-client-id',
    testAttemptId: 'unique-id-from-your-database', // max 40 chars
    hashedTestAttemptId: 'base64-hmac-sha256-of-testAttemptId-using-CLIENT_SECRET'
}

// 2. Choose what to monitor
const proctoringOptions = {
    trackingOptions: {
        audio: true,           // Background noise detection
        numHumans: true,       // Face count monitoring
        tabSwitch: true,       // Tab/window switch detection
        photosAtRandom: true,  // Random photo capture
    }
}

// 3. Wait for the SDK to load, then set up and start
async function startProctoring() {
    const apInst = new AutoProctor(credentials)
    try {
        await apInst.setup(proctoringOptions) // throws on invalid options
    } catch (err) {
        console.error(err)
        return
    }
    window.addEventListener('apMonitoringStarted', () => { /* show the test */ })
    apInst.start()

    // 4. When the candidate submits their test
    document.getElementById('submit-btn').addEventListener('click', () => apInst.stop())
    window.addEventListener('apTestSubmissionFailed', () => { /* let them retry stop() */ })
    window.addEventListener('apMonitoringStopped', () => { /* redirect */ })
}
if (window.AutoProctor) startProctoring()
else window.addEventListener('apLibReady', startProctoring, { once: true })

// 5. On a separate page (same credentials), show the proctoring report
// const reportInst = new AutoProctor(credentials)
// reportInst.showReport()
```

## Key Events (window.addEventListener)

- `apLibReady` — the SDK script finished loading; `window.AutoProctor` is now defined
- `apSetupCompleted` — .setup() finished, ready to start
- `apMonitoringStarted` — .start() finished, monitoring is active
- `apMonitoringStopped` — .stop() finished, test finalized
- `apTestSubmissionFailed` — the final submit failed; call .stop() again to retry
- `apErrorEvent` — something went wrong; `e.detail.errorCode` and `e.detail.errorDetails`

## Resuming

If .stop() has NOT been called, calling .start() with the same testAttemptId automatically resumes the session.

## Common Pitfalls

1. Never expose CLIENT_SECRET in browser code. Compute hashes server-side.
2. After .stop(), the attempt is finished: .start() with the same testAttemptId shows the candidate an "already completed" dialog.
3. The testAttemptId max length is 40 characters.
4. AutoProctor is loaded asynchronously by ap-entry.js. Don't call `new AutoProctor()` before `apLibReady` (or check `window.AutoProctor` first). Keep `var apVersionNum = '5.0.0'` before the ap-entry.js tag.
5. Unknown option keys throw. Use exactly the keys from the docs (e.g. `trackingOptions`, not `track`).