# First streamed donation

Show new donations in your browser or OBS through the [live stream](https://developers.donatty.com/en/realtime.md). A single HTML file uses the [centrifuge-js](https://github.com/centrifugal/centrifuge-js) library; you do not need your own server. The page displays received donations according to the [moderation rules](https://developers.donatty.com/en/structures/donation.md#moderation-and-display). Delivery time depends on payment processing and the [source conditions](https://developers.donatty.com/en/conditions/sources.md).

## 1. Issue a secret key

In the «Секретные ключи» (Secret keys) section, click «Выпустить секретный ключ» (Issue secret key). Enter a name, tick only «Донаты» (Donations, `donations:read`), choose an expiration period and click «Выпустить ключ» (Issue key). Copy the key now. It is shown only once.

[Open Developers in your dashboard](https://streamer.donatty.com/developers)

> **Keep the file with the key on your own computer only.**
> The stream requires a secret key, which this example stores in the HTML file. Before using it, read [Keep the secret key on the server](https://developers.donatty.com/en/tokens.md#keep-the-secret-key-on-the-server).

## 2. Save the page

Save the code as `live.html` and replace `dnt_pat_...` with your key.

```html title="live.html"
<!doctype html>
<meta charset="utf-8">
<title>Live donations</title>
<style>body { font: 600 28px/1.3 system-ui, sans-serif; }</style>
<div id="donation">Waiting for a donation…</div>
<script src="https://unpkg.com/centrifuge@5.8.0/dist/centrifuge.js"
  integrity="sha384-Hc/rqOA60dxGHNnO+nCm7KQ1yc4I121B7eeuuZ9vJMHEq6vVV4onLLtrwG8L79xQ" crossorigin="anonymous"></script>
<script>
  const API = "https://api.donatty.com";
  const KEY = "dnt_pat_...";  // a secret key: keep this file on your own computer only

  const seen = new Set();     // event ids: one event can arrive twice
  const waiting = new Map();  // id → a donation moderation has not decided on yet

  function show(d) {
    // textContent: the donor's name and message stay text even if they contain markup
    document.getElementById("donation").textContent = `${d.donorName}, ${d.amount.value} ${d.amount.currency}: ${d.message}`;
  }

  const client = new Centrifuge(API.replace("http", "ws") + "/v1/realtime/websocket", {  // https → wss
    headers: { Authorization: `Bearer ${KEY}` },  // the key goes in the first connection message, not in the address
    data: { channels: ["donations"] },            // donations only
  });
  client.on("publication", ({ data: event }) => {
    if (seen.has(event.id)) return;
    seen.add(event.id);
    const d = event.data;
    if (event.type === "donation.created" || event.type === "donation.replayed") {
      const status = d.moderation.status;
      if (status === "NONE" || status === "PASSED") show(d);
      else if (status === "PENDING") waiting.set(d.id, d);  // shown after the moderation decision
    } else if (event.type === "donation.moderated") {
      const pending = waiting.get(d.id);
      waiting.delete(d.id);
      if (pending && d.moderation.status === "PASSED") show(pending);
    }
  });
  client.on("disconnected", ctx => console.error(`disconnected: ${ctx.code} ${ctx.reason}`));  // final refusal
  client.connect();
</script>
```

## 3. Open the page

Open `live.html` in your browser. For OBS, add a Browser source to your scene, tick "Local file" and choose `live.html`.

## 4. Check it

Press «Тест на стриме» (Test on stream) in your dashboard: the test donation appears on the page.

If the donation does not appear, open the browser console. `disconnected: 4501 invalid_token` means the key was copied incorrectly or the code still has the `dnt_pat_...` placeholder; `disconnected: 4504 insufficient_scope` means that you used a public key or that your secret key does not have the `donations:read` scope. Other codes are in [Stream close codes](https://developers.donatty.com/en/realtime/close-codes.md).

## What the page shows

The page shows `NONE` and `PASSED` donations right away, `PENDING` ones after `donation.moderated` with `PASSED`, and never shows `BLOCKED` or an unknown status: that is the [Moderation and display](https://developers.donatty.com/en/structures/donation.md#moderation-and-display) rule. A test donation from the dashboard arrives as an event with `isTest: true`: [Donation event formats](https://developers.donatty.com/en/structures/donation-events.md#donationcreated). All fields of a donation are in the [`Donation`](https://developers.donatty.com/en/structures/donation.md#donation) structure.

## Next

- A donation queue and catch-up after a long disconnection: [OBS donation overlay](https://developers.donatty.com/en/examples/realtime-obs.md).
- What to do after a dropped connection: [History and catch-up](https://developers.donatty.com/en/realtime/recovery.md).
- Goals, Twitch and YouTube events over the same connection: [Stream channels](https://developers.donatty.com/en/realtime/channels.md).
