# Hacker News Search (`hackernews.search`)

Full-text search of Hacker News stories and comments with title, link, author, points and comment count.

Docs: https://primaryapi.com/docs/api-reference/hackernews/search

- Endpoint: `POST https://api.primaryapi.com/v1/run/hackernews.search`
- Category: Social · Platform: `hackernews` · Kind: search · Status: live
- Price: 1 credit per call. Every call costs the same, including calls that were retried automatically.
- Billing unit: per successful call
- Reliability: routed to a healthy upstream and retried automatically when an attempt fails; live 30-day status: `GET https://api.primaryapi.com/v1/status/apis?sku=hackernews.search`
- Execution: synchronous (answers 202 only if it outlives the wait window)

## Input

| Field | Type | Required | Default | Constraints | Description |
|---|---|---|---|---|---|
| `query` | string | yes |  | max 500 chars | Search keyword, e.g. "ai". |
| `tags` | string | no |  | max 200 chars | "story", "comment", "show_hn", "ask_hn", "author_pg" or "(story,poll)". Omit for all. |
| `sort` | enum | no | "relevance" | one of: relevance, date | relevance (default) or date (newest first). |
| `limit` | integer | no | 20 | 1–100 | Maximum number of results to return (1-100, default 20). |
| `cursor` | string | no |  |  | Opaque pagination cursor: pass the previous response's nextCursor. |

Unknown fields are rejected with `400 invalid_input`. The routing controls `preferLatencyUnderMs` and `maxCredits` may be sent in the same JSON body.

## Output

When `output.found` is `true`, `output.data` has this shape:

| Field | Type | Description |
|---|---|---|
| `results` | object[] |  |
| `results[].id` | string | HN item id |
| `results[].type` | string \| null | story \| comment \| poll \| pollopt \| job |
| `results[].title` | string \| null | Story title (a comment's story title) |
| `results[].url` | string \| null | Linked URL (a comment carries its story URL) |
| `results[].text` | string \| null | Comment or self-post text as HTML |
| `results[].author` | string \| null |  |
| `results[].points` | integer \| null |  |
| `results[].comments` | integer \| null |  |
| `results[].createdUtc` | integer \| null |  |
| `results[].storyId` | string \| null |  |
| `results[].permalink` | string | https://news.ycombinator.com/item?id=… |
| `total` | integer \| null | Approximate number of matches (only the first 1,000 can be paged) |
| `nextCursor` | string \| null |  |

When the thing you asked about does not exist (or a search matches nothing) the call still succeeds: `output.found` is `false`, `output.data` is `null` and `output.reason` says why (`not_found`, `private`, `no_results`, …). Misses are free unless the notes below say misses are billed.

Pagination: pass the previous response’s `nextCursor` as `cursor` to get the next page; `nextCursor: null` means there are no more results.

## Example request

```bash
curl -sS -X POST "https://api.primaryapi.com/v1/run/hackernews.search" \
  -H "Authorization: Bearer $PRIMARYAPI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query":"rust","tags":"story","limit":1}'
```

## Example response

_Values are placeholders that follow the output schema; `credits` is what this call would cost at the usual price._

```json
{
  "output": {
    "found": true,
    "data": {
      "results": [
        {
          "id": "22238335",
          "type": "story",
          "title": "Why Discord is switching from Go to Rust",
          "url": "https://blog.discordapp.com/why-discord-is-switching-from-go-to-rust-a190bbca2b1f",
          "text": null,
          "author": "Sikul",
          "points": 1582,
          "comments": 642,
          "createdUtc": 1580837440,
          "storyId": "22238335",
          "permalink": "https://news.ycombinator.com/item?id=22238335"
        }
      ],
      "total": 61089,
      "nextCursor": "1"
    }
  },
  "provider": "PrimaryAPI",
  "credits": 1,
  "items": 1,
  "requestId": "req_01JBXK6Y2N9V8T4R3Q2P1M0A7Z",
  "nextCursor": null
}
```

## Pricing and reliability

- Usual price: 1 credit per call
- Price ceiling: the usual price, even when a call is retried. A quote’s `maxCredits` is the most a given call can cost; the `maxCredits` routing control caps it per call
- Live 30-day status, success rate, uptime and latency: `GET https://api.primaryapi.com/v1/status/apis?sku=hackernews.search`

## Errors to handle

`invalid_input`, `unauthorized`, `insufficient_balance`, `key_limit_reached`, `trial_cap_reached`, `forbidden`, `not_found`, `idempotency_conflict`, `rate_limited`, `all_sources_failed`, `service_unavailable`, `timeout`. See https://primaryapi.com/docs/errors.

## Notes

Comment text and story text are HTML as stored by HN.

