Get a Discord or Slack ping when a game gets close late

A small Node.js poller that watches the live games of the leagues you pick and posts one line when a game is close in the last minutes, one when it goes to overtime, and the final score of games it alerted on. The rules are a table at the top of the file, so "close" can mean whatever you want.

Stack
Node.js 18+, Discord or Slack webhook
Plan
Free 7-day trial (no card) or a paid plan: live, in-progress scores are not on the Free plan

The live parts of this guide (games in progress) need the free 7-day Starter trial, no card, or a paid plan on Free keys created from November 9, 2026. Schedules, upcoming games and final scores work on Free. Start the trial

Updated 2026-11-23. Tested against the live API on that date. Data is aggregated from public sources and is typically 20-30 seconds behind live play (about 1 second after our source). There is no SLA.

1. Get a free API key

Sign up (no card) and copy the key from the dashboard. The Free plan gives you 1,000 calls in the first 30 days, then 125 a month, at 1 request per second. Every successful REST call counts as one call.

Check the key works with one call to the live-scores endpoint:

Terminal

export REALTIME_SPORTS_API_KEY=your_key
curl -H "Authorization: Bearer $REALTIME_SPORTS_API_KEY" \
  "https://www.realtimesportsapi.com/api/v1/sports/football/leagues/nfl/events/live"

2. Get the code

Everything below is in the close-game-alerts/ folder of the examples repo (MIT). Clone it and work from that folder.

Terminal

git clone https://github.com/ElcoDevRepos/realtime-sports-api-examples
cd realtime-sports-api-examples/close-game-alerts

3. Make a webhook

Discord: Server Settings, Integrations, Webhooks, New Webhook, Copy Webhook URL. Slack: an incoming webhook for the channel. Put it in .env as WEBHOOK_URL next to your API key; leave it empty to print the alerts instead.

Terminal

cp .env.example .env    # REALTIME_SPORTS_API_KEY, WEBHOOK_URL

4. The rules

NBA: 4th quarter or overtime, 5:00 or less left, within 5 points. NHL: 3rd period within one goal; overtime always alerts. NFL: 4th quarter or overtime, 5:00 or less left, within 8 points. stage() turns a live event into close, overtime, final or nothing, using status.period, status.clock and the two scores; each game alerts at most once per stage, so a back-and-forth finish does not flood the channel.

When a game you were alerted about drops out of /events/live, the script reads it once from /events/{eventId} and posts the final score. node --test close-game.test.mjs checks the rules with sample games.

close-game-alerts/close-game-alerts.mjs

#!/usr/bin/env node
// Close-game alerts: post to a Discord or Slack webhook when an NBA, NHL or NFL game gets tight late,
// goes to overtime, and when it ends. Node 18+, no dependencies.
//
// Rules (change them in RULES):
//   NBA  4th quarter or overtime, 5 minutes or less left, within 5 points
//   NHL  3rd period, within one goal (overtime always alerts)
//   NFL  4th quarter or overtime, 5 minutes or less left, within 8 points (one score)
// Each game alerts at most once per stage ("close", "overtime", "final"), so a back-and-forth finish
// doesn't flood the channel.
//
//   node close-game-alerts.mjs --league nba                # poll every 60 s
//   node close-game-alerts.mjs --league nhl,nba --every 30
//   node close-game-alerts.mjs --league nba --once --dry-run
//
// Env: REALTIME_SPORTS_API_KEY (required), WEBHOOK_URL (Discord or Slack incoming webhook; omit to print).
// Live, in-progress scores need the free 7-day trial (no card) or a paid plan: on the Free plan
// /events/live answers 403 LIVE_DATA_REQUIRES_TRIAL. One call per league per poll, only while games are on.
import { readFileSync, existsSync } from "node:fs";

const API = process.env.RSA_API_BASE || "https://www.realtimesportsapi.com/api/v1";
export const RULES = {
  nba: { sport: "basketball", latePeriod: 4, maxSecondsLeft: 300, margin: 5 },
  nhl: { sport: "hockey", latePeriod: 3, maxSecondsLeft: null, margin: 1 },
  nfl: { sport: "football", latePeriod: 4, maxSecondsLeft: 300, margin: 8 },
};
const OT_FROM = { nba: 5, nhl: 4, nfl: 5 };

function loadDotenv() {
  for (const f of [new URL("./.env", import.meta.url), `${process.cwd()}/.env`]) {
    if (!existsSync(f)) continue;
    for (const line of readFileSync(f, "utf8").split("\n")) {
      const m = /^\s*([A-Z_][A-Z0-9_]*)\s*=\s*(.*)\s*$/.exec(line);
      if (m && !(m[1] in process.env)) process.env[m[1]] = m[2].replace(/^['"]|['"]$/g, "");
    }
  }
}

/** "4:32" -> 272, "45.2" -> 45.2, anything else -> null */
export function secondsLeft(clock) {
  const s = String(clock ?? "").trim();
  let m = /^(\d+):(\d{1,2})(?:\.\d+)?$/.exec(s);
  if (m) return Number(m[1]) * 60 + Number(m[2]);
  m = /^(\d+(?:\.\d+)?)$/.exec(s);
  return m ? Number(m[1]) : null;
}

/** Which stage (if any) a game is in: "overtime" | "close" | "final" | null */
export function stage(league, ev) {
  const r = RULES[league], st = ev.status ?? {};
  const a = Number(ev.awayTeam?.score ?? 0), h = Number(ev.homeTeam?.score ?? 0);
  if (st.state === "post" || st.completed) return "final";
  if (st.state !== "in") return null;
  const period = Number(st.period ?? 0);
  if (period >= OT_FROM[league]) return "overtime";
  if (period < r.latePeriod || Math.abs(a - h) > r.margin) return null;
  if (r.maxSecondsLeft == null) return "close";
  const left = secondsLeft(st.clock);
  return left != null && left <= r.maxSecondsLeft ? "close" : null;
}

export function message(league, ev, kind) {
  const a = ev.awayTeam ?? {}, h = ev.homeTeam ?? {};
  const line = `${a.abbreviation ?? a.name} ${a.score ?? 0} – ${h.score ?? 0} ${h.abbreviation ?? h.name}`;
  const detail = ev.status?.detail ?? "";
  const icon = { close: "🔥", overtime: "⏱️", final: "🏁" }[kind];
  const label = { close: "Close game", overtime: "Overtime", final: "Final" }[kind];
  return `${icon} ${label} (${league.toUpperCase()}): ${line} · ${detail}`;
}

async function api(path, key) {
  const r = await fetch(API + path, { headers: { Authorization: `Bearer ${key}` } });
  const j = await r.json().catch(() => ({}));
  if (!r.ok) throw new Error(`HTTP ${r.status} ${j.error?.code ?? ""}: ${j.error?.message ?? ""}`);
  return j.data ?? [];
}

async function post(url, text) {
  const slack = /hooks\.slack\.com/.test(url);
  const r = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(slack ? { text } : { content: text }) });
  if (!r.ok) console.error(`webhook HTTP ${r.status}`);
}

async function main() {
  loadDotenv();
  const key = process.env.REALTIME_SPORTS_API_KEY;
  if (!key) throw new Error("Set REALTIME_SPORTS_API_KEY (or put it in .env). Free key + 7-day trial: https://www.realtimesportsapi.com/signup");
  const argv = process.argv.slice(2), opt = (k, d) => (argv.includes(k) ? argv[argv.indexOf(k) + 1] : d);
  const leagues = opt("--league", "nba").split(",");
  for (const l of leagues) if (!RULES[l]) throw new Error(`--league must be from ${Object.keys(RULES).join(", ")}`);
  const every = Number(opt("--every", 60)), once = argv.includes("--once"), dry = argv.includes("--dry-run") || !process.env.WEBHOOK_URL;
  const sent = new Map(); // "<league>:<eventId>" -> Set of stages already alerted
  const live = new Map(); // games seen live, so we can send their final once
  for (;;) {
    for (const league of leagues) {
      try {
        const events = await api(`/sports/${RULES[league].sport}/leagues/${league}/events/live`, key);
        const ids = new Set(events.map((e) => e.id));
        for (const ev of events) live.set(`${league}:${ev.id}`, ev);
        // A game that drops out of /events/live has ended: fetch it once for the final score.
        for (const [k, ev] of live) {
          if (!k.startsWith(league + ":") || ids.has(ev.id)) continue;
          live.delete(k);
          if (!sent.get(k)?.size) continue; // only report finals of games we alerted on
          const fin = (await api(`/sports/${RULES[league].sport}/leagues/${league}/events/${ev.id}`, key).catch(() => null)) ?? ev;
          events.push(Array.isArray(fin) ? fin[0] : fin);
        }
        for (const ev of events) {
          const kind = stage(league, ev), k = `${league}:${ev.id}`;
          if (!kind || sent.get(k)?.has(kind)) continue;
          if (kind === "final" && !sent.get(k)?.size) continue;
          sent.set(k, new Set([...(sent.get(k) ?? []), kind]));
          const text = message(league, ev, kind);
          if (dry) console.log(text); else await post(process.env.WEBHOOK_URL, text);
        }
        console.log(`${new Date().toISOString()} ${league}: ${events.length} live`);
      } catch (e) { console.error(`${league}: ${e.message}`); }
    }
    if (once) return;
    await new Promise((r) => setTimeout(r, every * 1000));
  }
}

if (import.meta.url === `file://${process.argv[1]}`) main().catch((e) => { console.error(e.message); process.exit(1); });

5. Run it

On 23 November 2026, during that evening's games, the live feed's status.clock counted down: Portland at Philadelphia went from 9:53 to 9:12 left in the 1st quarter in 75 seconds, and Columbus at New Jersey from 15:01 to 14:10 in the 1st period. That is the time left in the period, which is what the 5-minute rule reads. A one-off run with --once --dry-run found 2 live NBA games and 1 NHL game and, early in the first period, rightly sent nothing. The rules themselves are covered by close-game.test.mjs.

Terminal

node close-game-alerts.mjs --league nba                 # every 60 seconds
node close-game-alerts.mjs --league nhl,nba --every 30
node --test close-game.test.mjs

Endpoints used

  • GET /sports/{sport}/leagues/{league}/events/live
  • GET /sports/{sport}/leagues/{league}/events/{eventId}

Full reference: docs · OpenAPI · what each endpoint returns, by league

FAQ

Does it work on the Free plan?
No: live, in-progress scores need the free 7-day trial (no card) or a paid plan. On Free, /events/live answers 403 LIVE_DATA_REQUIRES_TRIAL.
How many API calls does it use?
One per league per poll, plus one per alerted game when it ends. At one poll a minute through a three-hour NBA night that is about 180 calls for the league.
Can I change what counts as close?
Yes. RULES at the top of the file holds the period, minutes left and margin for each league.

More guides