openapi: 3.0.0
info:
  title: Hacker News Search API
  description: |
    Search Hacker News stories, comments, Ask HN and Show HN posts, polls and jobs by keyword, with author, date, points and comment filters.

    Each result carries its Hacker News item id, date and a link to its discussion, and its author when the source names one. Stories carry their title, points and comment count, and their link when they have one. Comments carry their text and the story they belong to.

    Hacker News returns at most 1,000 results per search.
  version: 1.0.0
servers:
  - url: https://www.searchapi.io/api/v1
paths:
  /search:
    get:
      summary: Hacker News Search
      security:
        - ApiKeyAuth: []
        - ApiKeyQuery: []
      parameters:
        - name: engine
          in: query
          required: true
          description: 'Search engine. Must be set to: `hacker_news_search`'
          schema:
            type: string
            enum: ["hacker_news_search"]
        - name: q
          in: query
          required: true
          description: The terms to search for, up to 512 bytes in UTF-8, which is 512 ASCII characters.
          schema:
            type: string
        - name: type
          in: query
          required: false
          description: The kind of item to return. `story` also returns Ask HN and Show HN posts.
          schema:
            type: string
            enum: ["story", "comment", "ask_hn", "show_hn", "poll", "job"]
            default: "story"
        - name: sort_by
          in: query
          required: false
          description: Order of the results. `relevance` ranks by match and points, `date` puts the newest first.
          schema:
            type: string
            enum: ["relevance", "date"]
            default: "relevance"
        - name: author
          in: query
          required: false
          description: Hacker News username whose items to return. Usernames are case-sensitive.
          schema:
            type: string
            pattern: '^[A-Za-z0-9_-]{1,64}$'
        - name: created_after
          in: query
          required: false
          description: Return items created after this Unix timestamp, in seconds.
          schema:
            type: integer
            minimum: 0
            maximum: 9999999999
        - name: created_before
          in: query
          required: false
          description: Return items created before this Unix timestamp, in seconds. Must be later than `created_after`.
          schema:
            type: integer
            minimum: 0
            maximum: 9999999999
        - name: min_points
          in: query
          required: false
          description: Return items with at least this many points. Not supported for `comment` and `job`.
          schema:
            type: integer
            minimum: 0
            maximum: 999999999
        - name: min_comments
          in: query
          required: false
          description: Return items with at least this many comments. Not supported for `comment` and `job`.
          schema:
            type: integer
            minimum: 0
            maximum: 999999999
        - name: page
          in: query
          required: false
          description: Page of results. The last page holds result 1,000, so with `num` 20 it is page 50.
          schema:
            type: integer
            minimum: 1
            default: 1
        - name: num
          in: query
          required: false
          description: Number of results per page.
          schema:
            type: integer
            minimum: 1
            maximum: 100
            default: 20
        - name: zero_retention
          in: query
          required: false
          description: Set to true to disable logging and persistent storage for this request. Enterprise plans only.
          schema:
            type: boolean
            default: false
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SearchResponse'
        '400':
          description: Validation Error. There is an issue with query parameters, such as missing required parameters or invalid values.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Authentication Error. The API key is missing or invalid.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '403':
          description: Forbidden. Zero data retention is not available to the account, or the account requires `zero_retention=true`.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '429':
          description: Rate Limit Exceeded. The number of allowed requests has been exceeded. Consider upgrading your plan or waiting for the limit to reset.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          description: Server Error. Internal server error.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '503':
          description: Timeout. We could not retrieve results in 90 seconds.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: 'Use Bearer authentication. Format: "Bearer YOUR_API_KEY"'
    ApiKeyQuery:
      type: apiKey
      in: query
      name: api_key
      description: Pass API key as query parameter
  schemas:
    SearchResponse:
      type: object
      properties:
        search_metadata:
          $ref: '#/components/schemas/SearchMetadata'
        search_parameters:
          $ref: '#/components/schemas/SearchParameters'
        search_information:
          $ref: '#/components/schemas/SearchInformation'
        organic_results:
          type: array
          description: Items Hacker News returns for the search, in its order.
          items:
            $ref: '#/components/schemas/OrganicResult'
        pagination:
          $ref: '#/components/schemas/Pagination'
        error:
          type: string
          description: Why the search returned no results.
    SearchMetadata:
      type: object
      required: [status, created_at]
      properties:
        id:
          type: string
          description: Unique identifier for the search request
        status:
          type: string
          description: Status of the search request
        created_at:
          type: string
          format: date-time
          description: Timestamp when the search was created
        request_time_taken:
          type: number
          description: Time taken to make the request in seconds
        parsing_time_taken:
          type: number
          description: Time taken to parse the results in seconds
        total_time_taken:
          type: number
          description: Total time taken for the search in seconds
        request_url:
          type: string
          description: Hacker News search page for this search
        html_url:
          type: string
          description: URL to view HTML results
        json_url:
          type: string
          description: URL to view JSON results
    SearchParameters:
      type: object
      properties:
        engine:
          type: string
          description: Search engine used
        q:
          type: string
          description: The terms searched for
        type:
          type: string
          description: The kind of item returned
        sort_by:
          type: string
          description: Order of the results
        author:
          type: string
          description: Username whose items were returned
        page:
          type: integer
          description: Page of results
        num:
          type: integer
          description: Number of results per page
        created_after:
          type: integer
          description: Lower bound of the creation time, as a Unix timestamp
        created_before:
          type: integer
          description: Upper bound of the creation time, as a Unix timestamp
        min_points:
          type: integer
          description: Minimum points
        min_comments:
          type: integer
          description: Minimum comment count
        zero_retention:
          type: boolean
          description: Whether zero data retention was requested
    SearchInformation:
      type: object
      required: [total_results, is_total_results_exact]
      properties:
        total_results:
          type: integer
          description: Number of items Hacker News matches for the search, of which at most 1,000 can be paged through.
        is_total_results_exact:
          type: boolean
          description: Whether total_results is an exact count rather than an estimate.
    OrganicResult:
      type: object
      required: [position, item_id, type, discussion_link, created_at]
      properties:
        position:
          type: integer
          description: Position of the item in the results, counting from the first page.
        item_id:
          type: string
          description: Hacker News item id.
        type:
          type: string
          enum: ["story", "comment", "ask_hn", "show_hn", "poll", "job"]
          description: Kind of item.
        title:
          type: string
          description: Title of the story, poll or job.
        link:
          type: string
          description: Link the story or job points to.
        discussion_link:
          type: string
          description: Link to the item on Hacker News.
        author:
          type: string
          description: Username of the item's author.
        points:
          type: integer
          description: Points of the story or poll.
        comments:
          type: integer
          description: Number of comments on the story or poll.
        text:
          type: string
          description: Text of the comment or post, as plain text.
        parent_id:
          type: string
          description: Item id of the story or comment the comment replies to.
        story_id:
          type: string
          description: Item id of the story the comment belongs to.
        story_title:
          type: string
          description: Title of the story the comment belongs to.
        story_link:
          type: string
          description: Link the comment's story points to.
        created_at:
          type: string
          format: date-time
          description: Time the item was posted.
    Pagination:
      type: object
      required: [current]
      properties:
        current:
          type: integer
          description: The current page.
        next_page:
          type: integer
          description: Value to pass as `page` for the next page.
    ErrorResponse:
      type: object
      required: [error]
      properties:
        error:
          type: string
          description: Error message describing what went wrong
