Getting started
You need two keys from terrastamp for each host you run it on, for example www.brand.com:
| Key | Where it goes |
|---|---|
| Site key | In the script tag. It’s public and only works on pages served from https://<your host>. |
Binding key (tsb_…) |
On your server only, for example as TERRASTAMP_BINDING_KEY. Keep it secret. |
1. Add the script
Section titled “1. Add the script”Put this on the pages where you need the visitor’s location, usually your landing page:
<script src="https://api.terrastamp.dev/v1/stamp.js" data-site="YOUR_SITE_KEY" async></script>The script checks the visitor, then writes the terrastamp cookie on your domain. See
Add the script for the options.
2. Verify the cookie on your server
Section titled “2. Verify the cookie on your server”Copy the SDK, terrastamp.ts, into your backend (Node
18 or later, no dependencies) and verify the cookie on each request:
import { verifyStamp, jwksFromUrl } from './terrastamp'
const jwks = jwksFromUrl('https://api.terrastamp.dev/.well-known/jwks.json')
const r = await verifyStamp(req.cookies.terrastamp, { jwks, host: 'www.brand.com', bindingKey: process.env.TERRASTAMP_BINDING_KEY, ip: visitorIp,})3. Branch on the result
Section titled “3. Branch on the result”if (r.ok && r.stamp.status === 'consensus') { // r.stamp.country "US", r.stamp.subdivision "NY", r.stamp.region "US-NY" showContentFor(r.stamp.region)} else { // No stamp yet, an invalid one, or inconclusive: show your default. // The script on this page will check (or re-check) the visitor. showDefault()}On a visitor’s very first page view there is no cookie yet, because the script runs in the browser
after your server has answered. Either render a default and let page code update it, or add
data-reload="once" so the page reloads once when the first stamp arrives. See
Add the script.