Skip to content

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.

  • A public key with the address where the page will live: how to issue one.
  • The page opened over http:// or https://, not from a file.

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.

goal.html
<!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>

Start a server in the folder with the file:

Terminal
python3 -m http.server 8080

Add http://localhost:8080 to the key addresses and open http://localhost:8080/goal.html.

localhost:8080/goal.html

New microphone: RUB 6,420.50 of RUB 15,000.00

Goal loaded
localhost:8080/goal.html

No active goals

No goals
localhost:8080/goal.html

Could not load the goal

Error: details in the console

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 the dnt_pk_... placeholder (invalid_token);
  • HTTP 503 — the API is unavailable, try again later (service_unavailable).