Scripts can combine live channel information with values you keep between runs. For example, you can read a viewer’s balance for a reply, remember a channel-wide count, or schedule a pipeline to run after a delay. The reference below covers viewers, balances, statistics, rewards, storage, web fetches and scheduling.
Each call needs its grant, and a failed call leaves its reason in nnz.lastError.
Script basics explains grants and errors; the worked example puts storage and chat together.
Overview
| Call | Gives | When it fails |
|---|---|---|
nnz.api.user.get(id) | A viewer’s public profile | null |
nnz.api.user.lookup(login) | A Twitch account | null |
nnz.api.economy.balance(userId) | A viewer’s balance | 0 |
nnz.api.stats.viewer(userIdOrLogin) | A viewer’s channel stats | Zeros |
nnz.api.stats.stream(options) | Metrics of one stream | null |
nnz.api.reward.get(rewardIdOrTitle) | A channel-point reward | null |
nnz.api.reward.update(rewardIdOrTitle, patch) | true when the reward changed | false |
nnz.api.storage.get(key) | The stored text | null |
nnz.api.storage.set(key, value) | true when the value is stored | false |
nnz.api.storage.delete(key) | true when the call succeeded | false |
nnz.api.storage.list(prefix) | The stored keys | An empty list |
nnz.api.http.fetch(url) | The response body as text | null |
nnz.api.schedule.pipeline(name, delaySeconds, variables, dedupeKey) | true when the run is scheduled | false |
A call that needs a value and gets none stops the script with an error, for example “storage.get needs a key”.
Viewers
Get a viewer
nnz.api.user.get(id) gives the public profile of a viewer of the channel.
Leave out id for the viewer who started the script.
The id may be the bot’s own viewer id, the Twitch id or the login.
| Field | Type | Meaning |
|---|---|---|
id | string | The viewer’s id in the bot. |
username | string | The login name. |
displayName | string | The name as chat shows it. |
avatarUrl | string, optional | The profile picture. Absent when there is none. |
paint | object, optional | The 7TV name paint the viewer wears. Absent when there is none. |
The paint has backgroundImage, color and textShadow as CSS text, each absent when the paint sets none, and isImageOnly.
No email or other private data leaves the bot.
A viewer the bot does not know gives null and the code not_found.
Look up a Twitch account
nnz.api.user.lookup(login) finds a Twitch account by login, also when that user never chatted in the channel.
A leading @ and capital letters are fine.
It gives id, login and displayName.
A login must be letters, digits and underscores, at most 25 characters.
One run may ask Twitch 3 times.
The fourth call gives null and the code limit_exceeded.
Points
nnz.api.economy.balance(userId) gives a viewer’s balance in the channel’s currency, as a number.
Leave out userId for the viewer who started the script.
The value may be an id or a login.
A viewer the bot does not know gives 0.
Currency and balances explains how viewers earn and spend it.
Stats
Viewer stats
nnz.api.stats.viewer(userIdOrLogin) gives a viewer’s totals in the channel.
Leave out the value for the viewer who started the script.
A viewer the bot never saw gets zeros and a firstSeen of null, never null itself.
| Field | Meaning |
|---|---|
messages | Chat messages sent. |
watchtimeSeconds | Time watched, in seconds. |
firstSeen | The first day the viewer was seen, as yyyy-MM-dd. |
redemptions | Rewards redeemed. |
songRequests | Songs requested. |
commands | Commands used. |
This script answers with the viewer’s own numbers:
const stats = nnz.api.stats.viewer();
const balance = nnz.api.economy.balance();
nnz.api.chat.send('Messages: ' + stats.messages + ', points: ' + balance);
The bot posts that line in chat in the channel’s one bot voice.
Stream stats
nnz.api.stats.stream(options) gives metrics of one stream.
options.metrics is the list of metric keys to read.
options.stream is a stream id; leave it out for the latest stream, live or ended.
The answer always has stream: its id, startedAt, endedAt (null while live), title and game.
Each requested metric is a field of the answer with the same name.
A metric you did not ask for is absent.
channel, peakViewers and hypeTrain are also absent when there is no data.
The other metrics are present, with zeros or an empty list.
| Key | What it gives |
|---|---|
bits | Total bits and the top cheerers. |
channel | The channel’s name and avatarUrl. |
chat | messages and uniqueChatters. |
commandsRun | Commands run and the most used ones. |
duration | Seconds from the start to the end, or to now while live. |
followers | New followers during the stream. |
gifters | Gifters by gift count; anonymous gifts count as “Anonymous”. |
hypeTrain | The highest level and the number of trains that ended. |
newFollowers | Names of the viewers who followed, oldest first. |
peakViewers | The highest viewer count recorded. |
raidsIn | Raids that arrived, largest first: name and viewers. |
redemptions | Redemptions and the top reward titles. |
selfSubs | Viewers who subscribed themselves; gifts are not counted. |
songRequesters | Song requests and the top requesters, never song titles. |
subs | new, resub, gift and total. |
topChatters | The chatters with the most messages. |
A total with a top list has total and top.
Each top entry has name and count, highest first.
The ranked top lists, topChatters, newFollowers, selfSubs and raidsIn hold at most 5 entries.
gifters keeps every counted gifter, sorted by gift count and then name.
An empty metrics list gives every metric.
An unknown key, or a channel with no such stream, gives null.
const stats = nnz.api.stats.stream({
metrics: ['chat', 'peakViewers'],
});
if (stats && stats.chat) {
nnz.api.chat.send('Chatters today: ' + stats.chat.uniqueChatters);
}
Where the numbers come from
Stream statistics read the bot’s stored records for the selected channel and stream window. They do not fetch a fresh total from a streaming platform each time you call them. Chat and command counts use the recorded messages and command uses; redemptions use recorded reward redemptions, and song requesters use song-request history. Follower, subscription, cheer, raid and hype-train statistics use recorded channel events and their original occurrence times. Peak viewers uses the larger of the stream’s saved peak and its recorded viewer-count samples.
People currently banned in the channel are excluded from the viewer-related records used by these metrics. The latest ban or unban in the moderation history determines that status, with the saved ban list used when there is no such history entry. A timeout does not count as a ban, and an unbanned viewer counts again. These are statistics recalculated from available records, so missing or retained history affects what can be counted. They count the events the bot ingested, rather than reconstructing a streaming platform’s complete history.
Gift counts use two related sources differently.
subs.gift sums recorded gift-batch counts; gifters compares each gifter’s batch total with their per-recipient notices and uses the larger, avoiding double-counting the same gifts.
The two values can therefore differ when one source is missing.
SDK types and payloads lists every result field and its exact type.
Hype trains that end during a live stream are not counted yet, because the bot does not save their end time.
A missing hypeTrain does not prove that no train ran.
The metric counts the records that carry the event time.
A record without a numeric level counts as a train at level 0.
Rewards
Read a reward
nnz.api.reward.get(rewardIdOrTitle) finds a channel-point reward by its id or its title.
A title matches without regard to capital letters.
| Field | Meaning |
|---|---|
id | The reward id. |
title | The title viewers see. |
cost | The cost in channel points. |
prompt | The text viewers see when they redeem it, or null. |
isEnabled | true when viewers can see the reward. |
isPaused | true when the reward is paused. |
Change a reward
nnz.api.reward.update(rewardIdOrTitle, patch) changes the fields that the patch sets.
The patch may set title, cost, prompt, isEnabled and isPaused.
A field you leave out keeps its value, and other fields are ignored.
Only a reward that the bot created can change.
Any other reward gives false and the code refused.
The change goes through the same path as the dashboard.
Channel-point rewards explains how the bot makes a reward.
const changed = nnz.api.reward.update('Hydrate', {
cost: 500,
isPaused: false,
});
Storage
Storage keeps text values for the channel between runs. Every script of the channel reads and writes the same keys.
Choose descriptive keys such as hugs.count so unrelated scripts do not overwrite each other’s values.
Storage is shared within the channel, rather than automatically private to one script or one viewer.
To keep separate viewer values, include the viewer’s identifier in the key you choose.
| Limit | Value |
|---|---|
| Key length | 1 to 128 characters |
| Value size | 65,536 bytes of UTF-8 text |
| Keys per channel | 200 |
Store a value
nnz.api.storage.set(key, value) stores the text under the key.
A key that has no value yet counts toward the 200-key limit.
Replacing an existing key does not consume another key, but its new value must still fit the value-size limit.
A value over the size limit, or a new key when the channel is full, gives false and stores nothing.
Store a number or an object as text, for example with nnz.json.stringify.
The byte limit is not a character limit: emoji and many non-English characters use more than one byte.
A read followed by a write is two separate operations. If two runs read the same count before either writes, one increment can overwrite the other; this pattern is not an atomic counter.
Read a value
nnz.api.storage.get(key) gives the stored text, or null when the key has no value.
A key longer than 128 characters also gives null, with no error code.
Delete a value
nnz.api.storage.delete(key) removes the key.
A key that has no value still gives true.
List the keys
nnz.api.storage.list(prefix) gives the stored keys, sorted by key.
With a prefix, it keeps only the keys that start with it.
const raw = nnz.api.storage.get('hugs.count');
const count = Number(raw || '0') + 1;
nnz.api.storage.set('hugs.count', String(count));
Fetch a web page
nnz.api.http.fetch(url) asks for an https address and gives the response body as text.
Any other address gives null and the code invalid_argument.
A server that does not answer with success gives null and the code upstream_failed.
A request that is blocked or fails before an answer does the same.
One run may send and receive at most 256 KB in total over all its fetches.
A fetch that would go over it gives null and the code limit_exceeded.
A test run still performs these web requests, even though it captures chat and storage writes.
In a test run, a write gives true when its key and value are valid.
A full storage or a missing pipeline shows only in a live run.
Use a read-only HTTPS endpoint when testing a fetch.
Schedule a pipeline
nnz.api.schedule.pipeline(name, delaySeconds, variables, dedupeKey) runs a saved pipeline of the channel once, after a delay.
The name matches without regard to capital letters, and the pipeline must be switched on.
A pipeline that is not found gives false and the code not_found.
| Value | Meaning |
|---|---|
delaySeconds | Rounded up to whole seconds, then held between 1 second and 24 hours. |
variables | Optional. Text values the pipeline gets when it runs. |
dedupeKey | Optional. A new schedule with the same key replaces the run that still waits. |
nnz.api.schedule.pipeline('voice-reset', 300, {
reason: 'timeout',
});