JavaScript API

AgentGate.init, execute, render, getResponse, reset and remove, their options and results, and the error codes execute() rejects with.

The SDK defines one global, window.AgentGate.

MemberPurpose
init(options)start the collectors at page load; set the default site key and endpoint
execute(options)verify now; resolves with a token
render(element, options)create a widget; returns its id
getResponse(id)the widget's current token, or ""
reset(id)return a widget to its first state
remove(id)take a widget out of the page
versionthe SDK version, for example "1.0.0"
errorsthe list of error codes
Errorthe error class (AgentGate.Error)
localesthe UI languages available

init(options)

JavaScript
AgentGate.init({ siteKey: "site_…", endpoint: "https://agentgate.example" });
OptionMeaning
siteKeydefault site key for execute() and render()
endpointthe AgentGate origin; defaults to the origin that served the script

Call it as early as possible: it starts document-wide interaction counters (key and pointer timings, never what is typed) that the check scores. It may be called more than once; later calls only change siteKey and endpoint. Declarative widgets call it for you.

Warning

execute() without an earlier init() still works, but the behaviour trace is missing. AgentGate labels such verifications agentgate:sdk:late_init and the built-in rule sdk_late_init asks for the visible check. Call init() at page load.

execute(options)

Runs a verification and resolves with a token. Use it for requests your code sends itself.

JavaScript
try {
  const { token, expiresAt, decisionId } = await AgentGate.execute({ action: "signup" });
  await fetch("/api/signup", { method: "POST", headers: { "X-AgentGate-Token": token }, body });
} catch (err) {
  if (err instanceof AgentGate.Error) console.warn(err.code, err.status);
}
OptionDefaultMeaning
actionrequired; [a-z0-9_]{1,64} and configured for the site
siteKeythe init() site keythe site key
endpointthe init() endpointthe AgentGate origin
signalan AbortSignal; aborting rejects with aborted
timeout30000milliseconds for the automatic part; rejects with timeout
checkTimeout300000milliseconds allowed once the visible check is shown (a person is acting)
modeinvisiblereported to the server; interactive always asks for the check
containera modal dialogan element to show the check in, if one is needed
langthe browser's languagesUI language of the check

The result:

FieldMeaning
tokenthe single-use token to send to your server
expiresAtwhen the token expires, Unix milliseconds (two minutes by default)
decisionIdthe decision's ID, for support and the dashboard
clearance, clearanceExpiresAtfor clearance actions only (clearances)

Call execute() just before sending the request, not at page load: the token is short-lived and single use.

Errors

execute() rejects, and errorCallback receives, an AgentGate.Error with a stable code (and status, the HTTP status, when the server answered):

CodeMeaningWhat to do
site_invalidunknown or missing site keycheck the site key
action_invalidthe action is malformed or not configured for the sitecheck action and the site's actions
origin_deniedthis page's origin is not allowed for the site keyadd the origin to the site
rate_limitedtoo many challenges or verifications from this addressretry after Retry-After
challenge_failedthe evidence was declined, or the check was not passedlet the person retry
challenge_expired, challenge_used, challenge_invalid, too_many_attemptsthe challenge cannot be used againcall execute() again
schema_unsupportedSDK and server disagree on the protocolload the SDK from the same AgentGate
payload_too_large, bad_requestthe request was malformedreport it: this is a bug
unavailableAgentGate had a dependency failureretry later
timeouttimeout (or checkTimeout) passedretry
abortedyour signal abortednone
canceledthe person closed the check (Escape or Cancel)none, or offer to retry
networkthe request failed (offline, DNS, CORS, blocked by an extension)retry; check CSP connect-src
erroranything elseretry; report if it persists

AgentGate.errors lists every code. The server-side meaning of each is on Error codes.

render(element, options)

Creates a widget in element (an element or a CSS selector) and returns its id. Options and callbacks are on Widget configuration. Rendering into an element that already holds a widget replaces it.

getResponse(id)

The widget's current token, or "" when it has none (not verified yet, consumed by a submission, or expired).

reset(id)

Cancels a verification in progress (without calling errorCallback), clears the token and returns the widget to its first state: the "Protected" chip, and the Verify button in interactive mode.

remove(id)

Cancels a verification in progress, then takes back everything the widget added: its submit handler, the agentgate_token field it created (a field your page provides is kept), its elements, timers and the attributes it set on the host. The form then submits as if AgentGate had never rendered there. Call it from your framework's unmount or cleanup hook.

id can be the id render() returned, the host element, or omitted (the most recently rendered widget). An id that no longer exists does nothing.

View as Markdown