An API on another domain
Use this guide when your website and its API have different domains, for example example.com and api.example-backend.com, and the API may run on another server. You want three things:
- Bots can't call the API.
- A visitor who passed the check on the website is not checked again by the API.
- Your own app, for example a phone app, is never checked.
Why it needs a setup
After a check, the visitor's browser holds a pass in a cookie. A browser sends a cookie only to the domain that set it. So the pass from example.com never reaches api.example-backend.com, and the API can't know the visitor was already checked.
The API also can't show the checking page. Its callers are scripts and apps, not people looking at a page.
How it works
text
1. visitor ──► example.com checking page, solved once
(pass cookie for example.com)
2. page script ──► example.com/.tamamo/api/pass
◄── { "token": "...", "header": "X-Tamamo-Pass" }
3. page script ──► api.example-backend.com/v1/items
X-Tamamo-Pass: <token>
◄── the API's answer- The visitor opens the website and passes the check there, as always.
- Your page's script asks Tamamo for a copy of the pass. Tamamo gives one only to a visitor who has a valid pass. The copy lasts 15 minutes.
- The script sends the copy to the API in the
X-Tamamo-Passheader. Tamamo, in front of the API, sees a pass from a site it was told to trust and lets the request through. Your API never sees the header.
The copy is tied to the visitor's network and browser, like the pass itself. Someone who steals it can't use it from another place. It can't be used as a cookie, and a cookie can't be used in the header.
Your app doesn't take part in any of this. It sends a bypass header and is let in.
What you need
- One Tamamo that visitors of both domains go through. The API can be on any server: Tamamo forwards to it over the network. Two separate Tamamo installs can't do this, because the second one can't read passes the first one signed.
- The website protected by a check. A site that lets everyone in has no passes to share.
- A small change in your website's script: it has to fetch the copy and add the header.
1. Add the API as a site
In data/tamamo.yaml, add a site for the API's domain. target is where the API runs, as Tamamo reaches it.
yaml
sites:
- name: API
hosts: [api.example-backend.com]
target: https://backend-server.internal:8443
protection:
accept_passes_from: [default]accept_passes_from lists the sites whose visitors need no second check, by name. default is the default site. If your website is a site of its own named Website, write [Website].
In the web UI: add the site on Monitor > Sites, then open Configure > Protection, pick the API site at Editing, press Customize for this site, and fill in Accept passes from these sites in the Never checked card.
Then point the DNS record of api.example-backend.com at the server Tamamo runs on, and add the domain to your reverse proxy, the same way as the website's. See Behind a reverse proxy.
2. Send the pass from your website
Add this to the script of your website, and call your API through apiFetch:
js
let pass = null
async function tamamoPass() {
const now = Date.now() / 1000
if (!pass || pass.expires - now < 60) {
const r = await fetch('/.tamamo/api/pass')
if (!r.ok) throw new Error('no pass')
pass = await r.json()
}
return pass
}
async function apiFetch(url, options = {}) {
const p = await tamamoPass()
const headers = { ...options.headers, [p.header]: p.token }
let r = await fetch(url, { ...options, headers })
if (r.status === 403) {
// The copy ran out or the visitor's network changed. Get a new one once.
pass = null
const fresh = await tamamoPass().catch(() => null)
if (!fresh) {
location.reload() // no pass any more: the reload shows the check
return r
}
r = await fetch(url, { ...options, headers: { ...options.headers, [fresh.header]: fresh.token } })
}
return r
}
// Use it like fetch:
const items = await apiFetch('https://api.example-backend.com/v1/items').then((r) => r.json())/.tamamo/api/pass answers 401 when the visitor has no valid pass, and 404 when no site lists this one in accept_passes_from.
CORS
A browser asks the API for permission before it sends a script's request with an extra header to another domain. This question is an OPTIONS request, and it can't carry the pass.
- Tamamo lets that question through when it comes from a site in
accept_passes_from. - Your API must already answer it, as it does today for your website. Tamamo adds
X-Tamamo-Passto theAccess-Control-Allow-Headersof that answer, so you don't have to change the API. - When the pass is missing, Tamamo answers
403with{"error":"challenge_required"}and lets your website's script read it.
3. Let your app in
An app can't solve the check and has no website to get a pass from. Give it a secret header instead. Under the API site's protection:, add:
yaml
protection:
accept_passes_from: [default]
bypass_headers:
- name: Phone app
header: X-App-Key
value: put-a-long-random-value-hereThe app sends X-App-Key: put-a-long-random-value-here with every request. Tamamo lets those requests through without a check.
A secret inside an app is not very secret
Anyone who takes the app apart can read the header and use it. It keeps casual bots out, not a determined person. Turn on the rate limit for the API site too, and change the value in a new app version if it leaks. Your API still has to check who the user is, as it does today.
4. Check that it works
Replace the domains with yours.
Without a pass, a script request to the API is refused. This prints
403:bashcurl -s -o /dev/null -w '%{http_code}\n' -H 'Origin: https://example.com' https://api.example-backend.com/v1/itemsWith the app's header it gets through. This prints your API's own status, such as
200:bashcurl -s -o /dev/null -w '%{http_code}\n' -H 'X-App-Key: put-a-long-random-value-here' https://api.example-backend.com/v1/itemsOpen your website in a private browser window. After the check, the page's API calls work. In the web UI, Monitor > Activity shows them for the API site as passed.
Common problems
/.tamamo/api/passanswers404. No site lists this one inaccept_passes_from, or the name is spelled differently from the site's name./.tamamo/api/passanswers401. The visitor has no pass on the website. That happens when the website lets everyone in without a check. The website must usechallenge.- The API call fails with a CORS error in the browser console. Your API doesn't answer the browser's
OPTIONSquestion for your website's domain. That is the API's own CORS setting, not Tamamo's. - The API still answers
403with the header set. The copy is tied to the visitor's network and browser. Check that Tamamo sees the same visitor IP address for both domains: both must come through the same kind of proxy, withip_sourceandtrusted_proxiesset. - A rule for the API site asks for a higher difficulty than the website's. A pass only counts for checks up to the difficulty it was earned at.
Related
- Protection: bypass headers, the rate limit and the other checks.
- Sites: several domains behind one Tamamo.
- Endpoints: everything Tamamo serves under
/.tamamo/.
