Goal on a website (HTML)
The page shows the progress of the active goal and refreshes it every 30 seconds. One HTML file with the public key.
What you need
Section titled “What you need”- A public key with the address where the page will live: how to issue one.
- The page opened over
http://orhttps://, not from a file.
1. Save the code
Section titled “1. Save the code”Save the code as goal.html and replace dnt_pk_... with your public key.
The example inserts the goal title with textContent: if the title contains markup, it stays plain text.
<!doctype html><meta charset="utf-8"><title>Goal</title><p id="goal">Loading the goal…</p><progress id="bar" hidden></progress><script> const API = "https://api.donatty.com"; const KEY = "dnt_pk_..."; // the public key can live in page code
async function load() { const text = document.getElementById("goal"); const bar = document.getElementById("bar"); try { const res = await fetch(`${API}/v1/goals?status=ACTIVE&limit=1`, { headers: { Authorization: `Bearer ${KEY}` }, }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const [goal] = (await res.json()).items; if (!goal) { text.textContent = "No active goals"; bar.hidden = true; return; } const money = new Intl.NumberFormat("en-US", { style: "currency", currency: goal.target.currency }); const raised = Number(goal.raised.value), target = Number(goal.target.value); text.textContent = `${goal.title}: ${money.format(raised)} of ${money.format(target)}`; bar.max = target; bar.value = Math.min(raised, target); bar.hidden = false; } catch (err) { text.textContent = "Could not load the goal"; console.error(err); // Failed to fetch: the page address is not among the key addresses, or no network; HTTP 503: the API is unavailable } }
load(); setInterval(load, 30000);</script>2. Open the page
Section titled “2. Open the page”Start a server in the folder with the file:
python3 -m http.server 8080npx serve -l 8080Add http://localhost:8080 to the key addresses and open http://localhost:8080/goal.html.
What you’ll see
Section titled “What you’ll see”localhost:8080/goal.html
New microphone: RUB 6,420.50 of RUB 15,000.00
localhost:8080/goal.html
No active goals
localhost:8080/goal.html
Could not load the goal
If the goal didn’t load
Section titled “If the goal didn’t load”If the page says “Could not load the goal”, look at the browser console:
Failed to fetch— the page address is not among the key addresses (origin_not_allowed), or there is no network;HTTP 401— the key was pasted with a mistake, or the code still has thednt_pk_...placeholder (invalid_token);HTTP 503— the API is unavailable, try again later (service_unavailable).