This is a Frontend SDK method. It runs in the browser — import it in your React/Next.js component, not on the server.
Install
How it works
Two steps only:- Call your backend to get a
token - Call
AssessmentPortal.open({ token, apiKey })— the iframe opens and the candidate takes the test
onDone callback. The onDone / onClose callbacks are only for updating your UI (e.g. showing a toast message).
Signature
Parameters
The session token from your backend. This is
response?.data?.token from your assessmentApi.createSession() call.Never hardcode this — always fetch it fresh from your backend first.Your SmartAI public API key. This tells the portal which environment to load (live or test).Where to get it: Same key you use on the backend as
Setup — add this to your frontend Use it in code:
ASSESSMENT_API_KEY. The difference is how you expose it:| Variable | Used in | Safe to expose? |
|---|---|---|
ASSESSMENT_API_KEY | Backend .env | Server only — never sent to browser |
NEXT_PUBLIC_ASSESSMENT_API_KEY | Frontend .env.local | ✅ Safe — same key, public prefix |
.env.local:(result: { assessmentId: string }) => voidCalled when the candidate successfully submits the assessment. This is your signal to show a success message to the recruiter.() => voidCalled when the candidate closes the portal without submitting. The assessment is not complete — no result will appear in the webhook queue.(error: Error) => voidCalled when something goes wrong — invalid token, network failure, etc.CSS z-index of the overlay. Increase if your app has elements that appear on top of the iframe (sidebars, modals with very high z-index).
Why await import(...) instead of a regular import?
window and document, which don’t exist during Next.js server-side rendering. The dynamic import tells Next.js to load this package only when the code actually runs in the browser.
TypeScript type declaration
If your IDE shows “cannot find module” or type errors:Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Static import in Next.js | ReferenceError: window is not defined | Use await import(...) instead |
Forgetting apiKey | Portal uses wrong environment | Pass process.env.NEXT_PUBLIC_ASSESSMENT_API_KEY |
Calling open() before getting the token | token is undefined | Always await your session API call first |
Not handling onError | Silent failure, user confused | Always show a toast or alert in onError |
Calling open() twice | Two overlapping iframes | Check AssessmentPortal.isOpen() first |

