Build a Daily Digest#

Collect items from an API, format a short summary, and deliver it to a channel on a daily schedule. This combines fetching, shaping data, and notifying in the pattern most digest jobs follow.

The Script#

// digest.js
import env from "env";

const res = await fetch("https://api.github.com/repos/whatwg/fetch/issues");
if (!res.ok) {
	throw new Error(`GitHub request failed: ${res.status}`);
}

const issues = await res.json();
const open = issues.filter((issue) => issue.state === "open").slice(0, 5);

const lines = open.map((issue) => `- #${issue.number} ${issue.title}`);
const text = ["*Open issues*", ...lines].join("\n");

await fetch(env.get("DIGEST_WEBHOOK_URL"), {
	method: "POST",
	headers: { "Content-Type": "application/json" },
	body: JSON.stringify({ text }),
});

console.log(`Digest sent with ${open.length} item(s)`);

How It Works#

  • The API returns an array. filter and slice narrow it to the few items worth showing, keeping the message short.
  • The digest is built as an array of lines and joined once, so adding a header or footer later is trivial.
  • The webhook body uses a text field, which Slack and many other services accept. Adjust the body for other receivers.

Setup#

Create a secret named DIGEST_WEBHOOK_URL with your notification webhook. Schedule the job daily, for example 0 8 * * * for an 08:00 digest.