# Hacker News API - Stories, Points & Comments (`stas_k/hacker-news-api`) Actor

Hacker News has no keyword search and answers one item per request, so building a feed by hand costs hundreds of calls. Send a query, a story id or an item link and get clean JSON: title, url, points, comments, author and dates.

- **URL**: https://apify.com/stas_k/hacker-news-api.md
- **Developed by:** [Stas K](https://apify.com/stas_k) (community)
- **Categories:** Developer tools, Automation, SEO tools
- **Features:** Limited permissions
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 1 bookmarks
- **User rating**: No ratings yet

## Pricing

$0.90 / 1,000 stories

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## Hacker News API

Hacker News has no keyword search and its public API answers one item per request, so building a
feed of stories by hand costs hundreds of calls and a lot of glue. This actor turns a search query,
a story id or an item link into clean JSON rows: title, url, points, comments, author and dates.

### What you send

The `queries` field takes lines of two shapes, and they are told apart by shape, never by guessing:

- bare digits (`45751400`) or an item link (`https://news.ycombinator.com/item?id=1`) are read as
  that one story;
- anything else is a search query over stories (`python`, `show hn`).

### What you get

One row per story with `storyId`, `hnUrl`, `title`, `url`, `author`, `points`, `comments`,
`createdAt` (ISO 8601 UTC), `itemType`, `query`, `matchesTotal`, `source` and `billable`. A value
the source does not publish stays `null`; nothing is invented.

`comments` is the comment count the **search** endpoint publishes. When a row comes from a story
id or an item link the source answers with the comment tree instead of a count, so `comments` is
`null` on that row: the same story found by a query and by its id therefore carries a number in
one row and `null` in the other. That is the source, not a lost field.

### What costs money

One delivered story is one paid event. The price is on the actor card.

These come back FREE, each with a reason in the `reason` field and no story fields:

- `NOT_FOUND` - the source does not know that story id;
- `EMPTY` - the query matched nothing;
- `INVALID_INPUT` - the line is empty or a one-character query;
- `DUPLICATE` - the story was already delivered by an earlier line in the same run (a query and a
  direct id, or two spellings of one link, are one story and one sale);
- `NOT_CHECKED_CAP` - the line was cut by `maxQueries` or by the spending limit;
- `NOT_CHECKED_TIME` - the run ran out of time before that line;
- `NOT_CHECKED_BILLING` - the price list of the platform could not be read, so nothing was charged;
- `NOT_CHECKED_ERROR` - the source refused or answered something unreadable. This is a statement
  about the source, not about your story: re-run later.

### Limits

- `maxStories` (default 30, max 200) cuts one search query. A query that fills the number exactly is
  counted in `queriesCutByStoryCap`, so a cut is visible and never silent.
- `maxQueries` (default 25, max 200) cuts the whole run; lines beyond it get a free `NOT_CHECKED_CAP`.
- One line costs one request to `hn.algolia.com`, and the remaining deadline is asked again before
  every request. A 429 from the source is retried once with a pause, and the retry reads
  `Retry-After` when the source sends it.

### What this actor does not do

- It does not read comment threads: a story row carries the comment count, not the comments. The
  comment endpoint answers a nested tree that can be hundreds of kilobytes, and selling a cut tree
  would be a promise this actor does not keep. An id that belongs to a comment or a poll option is
  refused as a free `NOT_FOUND` with the item type named, not sold as a story with a null title;
  a job and a poll do carry a title, so they are sold as story-shaped rows.
- It does not log in, vote, post or use your Hacker News account.
- It does not promise "all stories ever": the source reports `matchesTotal` (how many stories the
  query matched), and this actor delivers at most `maxStories` of them.

### Run summary

The run writes `RUN_SUMMARY` into the default key-value store of the run, plus a copy under
`RUN_SUMMARY-<runId>` in the named store `hacker-news-summary`, so two parallel runs do not overwrite
each other. Counters include `requested`, `attempted`, `delivered`, `chargedEvents`, `storiesDelivered`
(rows that really reached the dataset), `storiesRead` (rows the source returned, which is more when
a batch is cut by the budget), `squeezedByDeadline` (requests the run's own deadline cut short, so
the reason names the budget and not the source), `deliveredUncharged`, `counterMismatch` and
`stopReason`.

# Actor input Schema

## `queries` (type: `array`):

Lines to read. Bare digits or a news.ycombinator.com/item?id=<id> link are read as that one story; anything else is read as a search query over stories. The last default line is an id that does not exist, so it comes back as a free NOT_FOUND row and shows you the free path.

## `maxStories` (type: `integer`):

How many stories one search query may return. A query that fills this number exactly is counted in queriesCutByStoryCap, so the cut is visible and never silent. A story id ignores this limit: it is always one story.

## `maxQueries` (type: `integer`):

How many lines this run reads. Lines beyond the number come back as free NOT_CHECKED_CAP rows with a reason.

## Actor input object example

```json
{
  "queries": [
    "python",
    "show hn",
    "45751400",
    "https://news.ycombinator.com/item?id=1",
    "999999999999"
  ],
  "maxStories": 30,
  "maxQueries": 25
}
```

# Actor output Schema

## `dataset` (type: `string`):

One row per story. A paid row carries storyId, hnUrl (the news.ycombinator.com link), title, url (the story's own link, null for a text post), author, points, comments, createdAt (ISO 8601 UTC), itemType (story, poll or job), source (the endpoint the row was read from), billable (true when the row is the kind the price list sells: a story row the platform may charge for; false when this actor could not be sold per event), query (the search that found it, null when the row came from a story id) and matchesTotal (how many stories the whole query matched, as the source reports it, null for a story id). A story reached twice in one run is one sale: the second line that reaches it comes back as a free DUPLICATE row. An id the source does not know is a free NOT_FOUND, a query that matched nothing is a free EMPTY, and a source that refused is a free NOT_CHECKED_ERROR; all of them carry a reason and no story fields. A value the source does not publish is null, never invented. Status rows are written in batches, so sort by actorIndex to get the input order back. A row whose `comments` is null came from a story id or a link: the source answers a comment tree there, not a count.

## `summary` (type: `string`):

Counters of the run: requested, attempted, delivered, rowsWritten, chargedEvents, storiesDelivered (rows that reached the dataset), storiesRead (rows the source returned), squeezedByDeadline (requests cut short by the run's own deadline, where the reason names the budget, not the source), deliveredUncharged, deliveredUnverified, chargeUncertain, writeErrors, cappedNotCharged, storiesNotFound, queriesEmpty, queriesInvalid, storiesDuplicate, queriesCutByStoryCap, cutByDeadline, cutByStop, cutByLimit, byStatus, counterMismatch, stopReason, datasetNote, firstFailure, billingActive, priceNote, eventName, maxStories, maxQueries, source, startedAt, finishedAt and runId. The bare RUN_SUMMARY lives in the default store of the run; a copy under RUN_SUMMARY-<runId> is kept in the named store hacker-news-summary, so two runs do not overwrite each other.

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "queries": [
        "python",
        "show hn",
        "45751400",
        "https://news.ycombinator.com/item?id=1",
        "999999999999"
    ]
};

// Run the Actor and wait for it to finish
const run = await client.actor("stas_k/hacker-news-api").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "queries": [
        "python",
        "show hn",
        "45751400",
        "https://news.ycombinator.com/item?id=1",
        "999999999999",
    ] }

# Run the Actor and wait for it to finish
run = client.actor("stas_k/hacker-news-api").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "queries": [
    "python",
    "show hn",
    "45751400",
    "https://news.ycombinator.com/item?id=1",
    "999999999999"
  ]
}' |
apify call stas_k/hacker-news-api --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,stas_k/hacker-news-api"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/unRtH3nbSTyaL8f0S/builds/TW3AiSWWL0puAxGvt/openapi.json
