What You Need
Add every origin your pages are served from to your application’s allowed origins, and register an audience for each of their hosts. A page on any other origin has its records and checks refused. See Audiences and Allowed Origins.
A browser key is public: it ships in your page. Keep your API key on your server.
Install
baselayer.js is one ES module. Pick how to load it:
Load it with
import and call init once per page:
import or a top-level await, so it loads the module with a dynamic import() and waits on the promise:
await works. A module is loaded once per page, so importing it again returns the same running snippet.
Options
A malformed option throws a
TypeError. Calling init again with valid options replaces the running session.
No page is excluded by default: checkout and payment pages are recorded like any other unless you list them in excludePaths. A pattern ending in /* matches the pages below a path but not the path itself, so /your/page/* covers /your/page/details and you list /your/page too. A * right after text matches the rest of the path, so /cart/checkout* covers /cart/checkout and /cart/checkout-guest. Paths are matched ignoring case, a trailing slash, the query string and the fragment.
Content Security Policy
A page with a Content Security Policy must allow:script-src https://js.baselayer.comandhttps://<application id>.collect.baselayer.com;connect-src https://<application id>.collect.baselayer.com https://api.baselayer.com, where the snippet sends its records and checks.
window alone. To let other scripts on your page call it, assign it yourself: window.baselayer = baselayer.
What Your Visitors See
On each page load and single-page-app route change, the snippet asks Baselayer for the page’s decision under your rules. Nothing is checked on an excluded page or withconsent: false. The snippet adds no header to your page’s own requests.
Only a step in
enforce mode shows anything. A step in monitor mode records what it would have decided, and the page carries on.
The overlay and the block page are a modal dialog in your page’s own DOM, so an agent that reads the page or its accessibility tree finds them. The rest of the page can’t be used while one is up. The overlay says what the step requires, such as Level 2 · Verified Email, gives an AI agent the steps to get a KYA credential, and has a field where the agent submits its credential presentation. A presentation that meets the requirement removes the overlay. One that doesn’t shows why, with a fresh nonce to try again.
Once an agent’s credential passes, the snippet sets a first-party baselayer_recognition cookie, so later checks recognize the agent without a new presentation.
It fails open. A check with no detection token within 5 seconds, a check that gets no answer in time, a network error or an error status is an allow: a check never breaks your page. When the detection token arrives late, within 2 minutes, the snippet sends the check once more and shows its answer.
Enforcement is cooperative. baselayer.js runs in the browser, so an agent that drives a browser by script can remove the overlay or the snippet itself. To enforce a step on requests that matter, such as placing an order, check the credential on your server in your own code. See the Credential Format Reference.
To choose what a step-up asks for, see What Step Up Asks For.
Record a Page View Yourself
The snippet records page views, page exits and single-page-app route changes on its own.track("page_view") queues a page view when your app shows a new page without changing the URL. It returns true when the record was queued. Any other type returns false.
Stop the Snippet
destroy() sends the current page’s exit, sends what is queued, stops the detection script and removes the snippet’s history hooks. A single-page app calls it when it leaves the pages the snippet serves.
Pin a Release
/v1/baselayer.js is always the current release, cached for 5 minutes. To pin one release, load /v1/<version>/baselayer.js, for example https://js.baselayer.com/v1/<version>/baselayer.js. Your Baselayer team tells you which releases are available. Baselayer never republishes a pinned release, and it is cached for a year.
Test Against Staging
Baselayer’s staging environment serves its own build of the snippet. Use a staging application ID and a staging browser key, which your Baselayer team provides, and point both origins at staging:https://<staging application id>.collect.staging.live.baselayer.com, https://js.staging.baselayer.com and https://api.staging.baselayer.com in place of the production hosts.