# 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

- A public key with the address where the page will live: [how to issue one](https://developers.donatty.com/en/quickstart/website.md#1-issue-a-public-key).
- The page opened over `http://` or `https://`, not from a file.

## 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.

```html title="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>
```

## 2. Open the page

Start a server in the folder with the file:


**Python:**

```sh
python3 -m http.server 8080
```
**Node.js:**

```sh
npx serve -l 8080
```


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

## What you'll see

- Goal loaded: New microphone: RUB 6,420.50 of RUB 15,000.00
- No goals: No active goals
- Error: details in the console: Could not load the goal

## 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](https://developers.donatty.com/en/errors/origin_not_allowed.md)), 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](https://developers.donatty.com/en/errors/invalid_token.md));
- `HTTP 503` — the API is unavailable, try again later ([service_unavailable](https://developers.donatty.com/en/errors/service_unavailable.md)).
