Add the script
<script src="https://api.terrastamp.dev/v1/stamp.js" data-site="YOUR_SITE_KEY" async></script>The script is small, has no dependencies, and loads asynchronously, so it doesn’t hold up your page. It only works on pages served over HTTPS from the host your site key was issued for.
Options
Section titled “Options”Options are set as attributes on the script tag. Only data-site is required.
| Attribute | |
|---|---|
data-site |
Required. Your site key. |
data-cookie-domain=".brand.com" |
Share the cookie across your subdomains. By default it belongs to the current host only. |
data-reload="once" |
Reload the page once after a new stamp, so server-rendered pages can branch on the very first view. |
What the script does
Section titled “What the script does”On each page view the script:
- Reads the
terrastampcookie. If it holds a recently checked, valid stamp, it stops there. - Otherwise it gathers the browser’s signals and, when a new stamp is needed, asks the browser for an approximate location. See Location.
- Sends them to terrastamp, which reuses the current stamp or issues a new one.
- Writes the stamp to the
terrastampcookie and fires theterrastamp:stampevent.
What visitors see
Section titled “What visitors see”Most of the time, visitors see nothing: a visitor with a valid stamp isn’t asked anything. When a new stamp is needed, they see the browser’s own location popup. Allowing it adds one more signal, and declining it is fine, because the other signals still decide.
terrastamp shows its own small dialog only when a check ends inconclusive, offering Try again. Its styles are isolated from your page’s, so neither affects the other.
A visitor who declines, or whose check doesn’t reach agreement, still gets a stamp, marked
inconclusive with a reason. The script checks again after a short while.
The first page view
Section titled “The first page view”The script runs in the browser, after your server has already answered the first request, so that request carries no cookie. Two ways to handle it:
-
Render a default and update in the page. Listen for the stamp in page code (display only; your server must still verify the cookie before trusting it):
document.addEventListener('terrastamp:stamp', (e) => showRegion(e.detail.region)) -
Reload once. With
data-reload="once", the script reloads the page a single time when a new stamp arrives, and your server sees the cookie on the reload. It never reloads twice for the same stamp.
Content Security Policy and Permissions Policy
Section titled “Content Security Policy and Permissions Policy”If your site sends a Content-Security-Policy, allow:
| Directive | Value |
|---|---|
script-src |
https://api.terrastamp.dev |
connect-src |
https://*.terrastamp.dev wss://*.terrastamp.dev |
If you send a Permissions-Policy header, it must allow geolocation for your own origin
(geolocation=(self)). With geolocation blocked, terrastamp can only use the signals that need no
permission.
See Embed reference for the page-code API.