openapi: 3.0.0
info:
  title: Reddit Comments API
  description: |
    The Reddit Comments API returns the comments on a Reddit post by its ID or link, in the order Reddit shows them, with their replies nested. The first page holds the comments Reddit shows first, with replies up to three levels deep. Each next page, and each thread's hidden replies, holds up to about 100 more comments, two levels deep. Wherever Reddit cuts a thread, the comment carries the number of hidden replies and a token that loads them. Share links from the Reddit app work too. NSFW posts are returned only when `safe` is `off`.

    **Cross-linking**: the `id` of a post returned by the Reddit Search API can be passed as `post_id`.

    Pages are cursor-based: pass `pagination.next_page_token`, or a comment's `replies_next_page_token`, as `next_page_token` with the same `post_id` or `url`, `sort_by` and `safe`.
  version: 1.0.0
servers:
  - url: https://www.searchapi.io/api/v1
paths:
  /search:
    get:
      summary: Reddit Comments
      security:
        - ApiKeyAuth: []
        - ApiKeyQuery: []
      parameters:
        - name: engine
          in: query
          required: true
          description: Search engine to use
          schema:
            type: string
            enum: ["reddit_comments"]
            default: "reddit_comments"
        - name: post_id
          in: query
          required: false
          description: ID of the post, such as `t3_ckbolc` from Reddit Search results or `ckbolc` from a post link. Required if url is not provided, and cannot be combined with it.
          schema:
            type: string
            pattern: '^(?:t3_)?[0-9a-z]{1,13}$'
        - name: url
          in: query
          required: false
          description: Link to the post on reddit.com, old.reddit.com or redd.it, or a share link from the Reddit app. Required if post_id is not provided, and cannot be combined with it.
          schema:
            type: string
            maxLength: 2048
        - name: sort_by
          in: query
          required: false
          description: Order of the comments. By default, the order Reddit picks for the post, usually best.
          schema:
            type: string
            enum: ["best", "top", "new", "controversial", "old", "q_and_a"]
        - name: safe
          in: query
          required: false
          description: Set to `off` to return the comments of an NSFW post, its later pages and hidden replies included.
          schema:
            type: string
            enum: ["active", "off"]
            default: "active"
        - name: next_page_token
          in: query
          required: false
          description: Token for the next page of comments, from `pagination.next_page_token`, or for the replies Reddit hides below a comment, from its `replies_next_page_token`. Send it with the same post_id or url, sort_by and safe. A token from an NSFW post works only with safe set to off. Pages get shorter as you go, and Reddit stops after a few hundred to about a thousand top-level comments, depending on the post and the order.
          schema:
            type: string
            pattern: '^(?:nsfw\.)?[A-Za-z0-9+/]{1,8192}={0,2}$'
      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'
        '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'
        error:
          type: string
          description: Error message explaining why no comments were returned.
        search_information:
          $ref: '#/components/schemas/SearchInformation'
        comments:
          type: array
          description: Top-level comments of the page, or the replies the token loads, in the order Reddit shows them.
          items:
            $ref: '#/components/schemas/Comment'
        pagination:
          $ref: '#/components/schemas/Pagination'
    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: Reddit URL for this post
        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
        post_id:
          type: string
          description: ID of the post requested
        url:
          type: string
          description: Link to the post requested
        sort_by:
          type: string
          description: Order of the comments requested
        safe:
          type: string
          description: NSFW filter setting
        next_page_token:
          type: string
          description: Token of the page requested
    SearchInformation:
      type: object
      properties:
        sort_by:
          type: string
          enum: ["best", "top", "new", "controversial", "old", "q_and_a"]
          description: Order Reddit shows the comments in, as a `sort_by` value.
    Pagination:
      type: object
      properties:
        next_page_token:
          type: string
          description: Token to pass as `next_page_token` to get the next page of comments.
    Comment:
      type: object
      required: [position, id, iso_date]
      properties:
        position:
          type: integer
          description: Position of the comment among its siblings on the page.
        id:
          type: string
          description: Reddit fullname of the comment, prefixed with `t1_`.
        author:
          $ref: '#/components/schemas/CommentAuthor'
        text:
          type: string
          description: Text of the comment, with paragraphs separated by blank lines.
        text_links:
          type: array
          description: Links to pages outside Reddit found in the comment.
          items:
            type: string
        image:
          type: string
          description: Image or GIF posted in the comment.
        link:
          type: string
          description: Link to the comment on Reddit.
        score:
          type: integer
          description: Net vote score of the comment.
        awards_count:
          type: integer
          description: Number of awards the comment has received.
        iso_date:
          type: string
          format: date-time
          description: Date the comment was posted, in ISO 8601.
        edited_iso_date:
          type: string
          format: date-time
          description: Date the comment was last edited, in ISO 8601.
        distinguished:
          type: string
          description: Role the author commented with, such as `moderator` or `admin`.
        is_submitter:
          type: boolean
          description: Whether the author of the comment also wrote the post.
        is_pinned:
          type: boolean
          description: Whether the comment is pinned to the top of the comments.
        is_locked:
          type: boolean
          description: Whether the comment is locked for replies.
        is_score_hidden:
          type: boolean
          description: Whether Reddit still hides the score of the comment.
        is_removed:
          type: boolean
          description: Whether the comment was removed or deleted.
        parent_id:
          type: string
          description: ID of the comment the comment replies to, or of the post for a top-level comment.
        replies:
          type: array
          description: Replies to the comment in the order Reddit shows them, nested the same way.
          items:
            $ref: '#/components/schemas/Comment'
        more_replies:
          type: integer
          description: Number of replies Reddit hides below the comment.
        replies_next_page_token:
          type: string
          description: Token to pass as `next_page_token` to get the replies Reddit hides below the comment.
    CommentAuthor:
      type: object
      required: [username, id, link]
      properties:
        username:
          type: string
          description: Username of the author.
        id:
          type: string
          description: Reddit fullname of the author, prefixed with `t2_`.
        link:
          type: string
          description: Link to the author's profile.
    ErrorResponse:
      type: object
      required: [error]
      properties:
        error:
          type: string
          description: Error message describing what went wrong
