# News watch

A Markdown brief of the day's new articles on your topics, with each article listed once.

Available · Python 3.9+ · One file, nothing to install · Tested 2026-11-19

> **For agents.** Read the API key from the `MILLIONSHORT_API_KEY` environment variable. Never print it; where a client needs it in a settings file, use a file that is not committed.
> The MCP address is https://mcp.millionshort.com/mcp (streamable HTTP; send `Authorization: Bearer <key>`, or no header for the no-key mode).
> On `INSUFFICIENT_CREDITS` stop and ask a person; on `RATE_LIMITED` or `CAPACITY` wait the `Retry-After` seconds, then retry once.
> Set-up steps written for agents: https://millionshort.com/docs/auth.md. Every integration: https://millionshort.com/docs/integrations.md.

For each topic, one search of the `news` source over the last day (`time_range: day`), with the passage of each article that best matches the topic. Articles already in an earlier brief are skipped: their addresses are kept in `seen.json` beside the script.

## 1. Get the code

```python news_watch.py
"""News watch: a daily brief of new articles on your topics, from the news source.

    export MILLIONSHORT_API_KEY=ms-YOUR_API_KEY
    python news_watch.py "heat pumps" "community energy" > brief.md

No package to install: Python 3.9 or later. One search per topic (1 credit each). Articles already
in an earlier brief are skipped: their addresses are kept in seen.json beside this file. Run it once
a day, from cron or any scheduler.
"""
import datetime
import json
import os
import sys
import urllib.error
import urllib.request
from pathlib import Path

API = os.environ.get("MILLIONSHORT_BASE_URL", "https://api.millionshort.com").rstrip("/")
SEEN = Path(__file__).with_name("seen.json")


def news(topic):
    body = {"query": topic, "sources": ["news"], "time_range": "day", "num_results": 10, "max_highlights": 1}
    request = urllib.request.Request(
        f"{API}/v1/search",
        data=json.dumps(body).encode(),
        headers={"Authorization": "Bearer " + os.environ["MILLIONSHORT_API_KEY"], "Content-Type": "application/json"},
    )
    with urllib.request.urlopen(request, timeout=60) as response:
        return json.load(response)["data"]["results"]


def main(topics):
    seen = set(json.loads(SEEN.read_text())) if SEEN.exists() else set()
    print(f"# News brief, {datetime.date.today():%d %B %Y}\n")
    try:
        for topic in topics:
            print(f"## {topic}\n")
            try:
                fresh = [r for r in news(topic) if r["url"] not in seen]
            except urllib.error.HTTPError as failure:
                print(f"The search failed ({failure.code}); this topic is tried again next time.\n")
                if failure.code in (401, 429):  # no key, or no credits or calls left: the next topics would fail too
                    break
                continue
            if not fresh:
                print("Nothing new.\n")
            for r in fresh:
                passage = (r.get("highlights") or [r.get("snippet") or ""])[0]
                print(f"- [{r['title']}]({r['url']}) ({r['site']}, {r.get('published_date') or 'no date'})")
                if passage:
                    print(f"  {passage[:240]}")
                seen.add(r["url"])
            print()
    finally:
        SEEN.write_text(json.dumps(sorted(seen)))


if __name__ == "__main__":
    main(sys.argv[1:] or ["heat pumps"])
```

## 2. Run it

```bash
export MILLIONSHORT_API_KEY=ms-YOUR_API_KEY
python news_watch.py "home battery storage" "community energy" > brief.md
```

To get it every morning, run it from cron, for example `0 7 * * *` with the key in the job's environment.

## 3. What you get

```text brief.md (shortened)
# News brief, 19 November 2026

## home battery storage

- [US battery storage capacity exceeds 200 GWh — Electrek](https://ua.news/en/...) (ua.news, 2026-11-19)
  In the third quarter of 2026, 14.3 GWh of battery storage systems were installed in the United States ...
- [Why AI data centers need storage beyond conventional backup power](https://www.finanznachrichten.de/...) (finanznachrichten.de, 2026-11-19)
  AI data centers are changing the duty cycle expected of battery energy storage ...

## community energy

- [Why hip-hop moguls are pivoting to green energy investments](https://www.wokv.com/...) (wokv.com, 2026-11-18)
  However, community solar lets you benefit from green energy investments ...
```

Run it again the same day and each topic says "Nothing new." Broad topics also catch loose matches (a phone with a "massive battery"): quote a phrase, or narrow with `include_domains`.

## 4. Change it

| To | Change |
|---|---|
| A weekly brief | `"time_range": "week"`, and run it weekly |
| Only smaller outlets | Add `"remove_top_sites": 1000` |
| Discussions as well | `"sources": ["news", "discussions"]` (1 credit per source) |
| Narrower results | Quote the exact phrase in the topic, or name the sites with `include_domains` |

## Costs and limits

One search per topic a run: 1 credit each.

## When it fails

| What you see | Why | Fix |
|---|---|---|
| `401 AUTH_MISSING_KEY` or `AUTH_INVALID_KEY` | The key is missing or wrong | Set `MILLIONSHORT_API_KEY`; keys start with `ms-` |
| `400 INVALID_PARAM` | A field name or value the API does not take | The `message` names the field; the [search reference](https://millionshort.com/docs/search/parameters) lists them all |
| `429 RATE_LIMITED` or `CAPACITY` | Too many calls a minute, or no free capacity right now | Wait the `Retry-After` seconds, then retry |
| `429 INSUFFICIENT_CREDITS` | The plan's credits are used up | Stop and add credits; do not retry |
| The same story again under a new address | Outlets republish under new URLs | Match on titles too, or keep the brief to `include_domains` you trust |

## Related

- [News monitoring guide](https://millionshort.com/docs/monitor/guides/news-monitoring): longer patterns for watching a topic.
- [The news source](https://millionshort.com/docs/search/sources/news): what it holds and which filters it applies.
- [n8n](https://millionshort.com/docs/integrations/n8n): the same search as a scheduled workflow step.
