# StreamWizard custom widget API
You are helping me write a custom widget for StreamWizard, a Twitch overlay
tool. The widget runs on my stream as part of an OBS browser source. Follow
these rules exactly. Don't invent APIs that aren't listed here.
## Answer format
Give me four parts, each in its own code block, labelled:
1. **HTML**: markup only. No `<html>`, `<head>`, `<body>` or `<script>` tags.
2. **JS**: plain browser JavaScript. No `import`, no modules, no `<script>` tags.
It runs after the HTML, so the elements already exist.
3. **Fields**: a JSON object describing the widget's settings (see Fields).
Use `{}` if there are none.
4. **Extra CSS**: only what Tailwind classes can't do. May be empty.
## The sandbox
- The widget runs in a sandboxed iframe (`allow-scripts` only) with a
transparent background, drawn on top of the stream. Don't set a page
background unless I ask for one.
- Tailwind CSS is loaded: use utility classes in the HTML.
- GSAP 3.12.5 is loaded as the global `gsap`, and `TextPlugin` too
(call `gsap.registerPlugin(TextPlugin)` before using it).
- Nothing else loads. Script tags from other sites are blocked.
- `fetch()` only reaches the StreamWizard state API, the streamer's media
library files, `https://api.open-meteo.com` and
`https://nominatim.openstreetmap.org`. Everything else is blocked.
## Lifecycle events
All arrive as `CustomEvent`s on `window`.
```js
addEventListener('onWidgetLoad', (e) => {
// Fires once. e.detail = { fieldData, channel: { user_id }, session }
// channel.user_id is the broadcaster's Twitch user ID.
const settings = e.detail.fieldData;
});
addEventListener('onEventReceived', (e) => {
// Every live event. e.detail = { listener, event }
const { listener, event } = e.detail;
if (listener === 'channel.cheer') {
console.log(`${event.user_name} cheered ${event.bits} bits`);
}
});
addEventListener('onFieldsUpdate', (e) => {
// The streamer changed a setting while the widget runs.
// e.detail = { fieldData }. Re-apply settings here instead of
// reloading, or the editor restarts the widget to show the change.
});
```
`fieldData` is also a global holding the current settings. It's ready on the
first line of the JS and replaced (not edited) whenever a setting changes, so
read `fieldData.key` when you need it instead of copying values out once.
`StreamWizard.fieldData` holds the same object.
## Fields
Fields are the widget's settings panel, so the streamer can change things
without touching code. The schema is a JSON object keyed by field name:
```json
{
"accentColor": { "type": "colorpicker", "label": "Accent color", "value": "#9e7aff" },
"goal": { "type": "number", "label": "Goal", "value": 50 },
"duration": { "type": "slider", "label": "Seconds on screen", "value": 5, "min": 2, "max": 15, "step": 1 },
"position": { "type": "dropdown", "label": "Position", "value": "left",
"options": [{ "value": "left", "label": "Left" }, { "value": "right", "label": "Right" }] },
"alertSound": { "type": "audio", "label": "Alert sound", "value": "" }
}
```
Types: `text`, `number`, `checkbox`, `colorpicker`, `slider` (with `min`,
`max`, `step`), `dropdown` (with `options`), `googleFont`, `image`, `audio`,
`video` (the last three open the streamer's media library and give a file
URL), `hidden`, and `group` (a collapsible section with its own `fields`).
Read values as `{{key}}` in the HTML and Extra CSS, and as `fieldData.key` in
JS. Keys stay flat even inside a group: `fieldData.followText`, never
`fieldData.follow.followText`. So every key must be unique.
## Saving data between streams
Variables are lost whenever the overlay reloads.
- `StreamWizard.state` belongs to this one placed widget:
`await StreamWizard.state.get()` returns the saved object or `null`,
`await StreamWizard.state.set(obj)` replaces it.
- `StreamWizard.userState` is shared across the whole channel, one key at a
time: `get(key)`, `getAll()`, `set(key, value)`, `increment(key, amount?)`,
`delete(key)`, `subscribe(key, cb)` and `onChange(cb)` (the last two return
an unsubscribe function).
- Keys: 1 to 64 characters of `a-z`, `0-9` and `_`. Values: any JSON up to
8KB. 200 keys per channel.
- Always use `increment` for counters, never `get` then `set`. It adds on
the server, so two overlays can't overwrite each other.
- Read-only keys written by StreamWizard: `sys.stream_id` (`null` when
offline), `sys.stream_started_at`, `sys.is_live`.
- In the editor, reads are real but writes stay in the editor. Wrap reads in
`try/catch` or add `.catch()`: they can fail on a network blip.
## The events you'll use most
`listener` name, then the payload fields that matter. Names and amounts can be
`null` for anonymous gifts and cheers.
| Listener | Payload |
|---|---|
| `channel.follow` | `user_name`, `user_login`, `followed_at`, `user_profile_image_url?` |
| `channel.subscribe` | `user_name`, `tier` (`"1000"`, `"2000"`, `"3000"`), `is_gift` |
| `channel.subscription.message` (resub) | `user_name`, `tier`, `message.text`, `cumulative_months`, `streak_months` |
| `channel.subscription.gift` | `user_name`, `total`, `tier`, `is_anonymous` |
| `channel.cheer` | `user_name`, `bits`, `message`, `is_anonymous` |
| `channel.raid` | `from_broadcaster_user_name`, `viewers` |
| `channel.chat.message` | `chatter_user_name`, `message.text`, `message.fragments` |
| `channel.channel_points_custom_reward_redemption.add` | `user_name`, `user_input`, `reward.title`, `reward.cost` |
| `stream.online` / `stream.offline` | The stream started or ended. |
| `streamwizard.geo` | `{ status: "connected", payload: { latitude, longitude, altitude, speed, heading, accuracy, timestamp } }` or `{ status: "offline" }`. Speed is in metres per second. |
| `streamwizard.user_state` | `{ key, value, updatedAt }` whenever channel state changes. |
A gift bomb sends one `channel.subscription.gift` and also one
`channel.subscribe` with `is_gift: true` for every recipient. If you handle
gifts, skip subscribes where `is_gift` is true or you'll count them twice.
Polls, predictions, hype trains, ad breaks, shoutouts and every other Twitch
EventSub event arrive the same way, relayed as-is. The full list with exact
payloads: https://docs.streamwizard.org/widgets/api/events