openapi: 3.0.0
info:
  title: Reddit Comment Search API
  description: |
    The Reddit Comment Search API searches Reddit comments across all of Reddit or within one subreddit. Each comment comes with its text, the links and image in it, its author, score and date, the post it was made on and its subreddit.

    **Cross-linking**: pass a comment's `post.id` to the Reddit Post API as `post_id` to get the post and its top comments.
  version: 1.0.0
servers:
  - url: https://www.searchapi.io/api/v1
paths:
  /search:
    get:
      summary: Reddit Comment Search
      security:
        - ApiKeyAuth: []
        - ApiKeyQuery: []
      parameters:
        - name: engine
          in: query
          required: true
          description: Search engine to use
          schema:
            type: string
            enum: ["reddit_comment_search"]
            default: "reddit_comment_search"
        - name: q
          in: query
          required: true
          description: Search query.
          schema:
            type: string
            maxLength: 512
        - name: subreddit
          in: query
          required: false
          description: Name of the subreddit to search in.
          schema:
            type: string
            pattern: '^[A-Za-z0-9][A-Za-z0-9_]{1,20}$'
        - name: sort_by
          in: query
          required: false
          description: Sort order of the comments.
          schema:
            type: string
            enum: ["relevance", "top", "new"]
            default: "relevance"
        - name: safe
          in: query
          required: false
          description: Set to `off` to include comments on NSFW posts.
          schema:
            type: string
            enum: ["active", "off"]
            default: "active"
        - name: next_page_token
          in: query
          required: false
          description: Token for the next page of results, from `pagination.next_page_token` of the previous response. Reddit stops after 25 pages.
          schema:
            type: string
            pattern: '^[A-Za-z0-9+/_-]{1,4096}={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.
        comments:
          type: array
          description: Comments matching the query, 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 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: Search query
        subreddit:
          type: string
          description: Subreddit the search was restricted to
        sort_by:
          type: string
          description: Sort order of the comments
        safe:
          type: string
          description: NSFW filter setting
        next_page_token:
          type: string
          description: Token of the page requested
    Comment:
      type: object
      required: [position, id, link, score, iso_date, post, subreddit]
      properties:
        position:
          type: integer
          description: Position of the comment 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 and quoted lines starting with `>`.
        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 Reddit shows for the comment.
        iso_date:
          type: string
          format: date-time
          description: Date the comment was posted, in ISO 8601.
        reply_to:
          $ref: '#/components/schemas/CommentReplyTo'
        post:
          $ref: '#/components/schemas/CommentPost'
        subreddit:
          $ref: '#/components/schemas/CommentSubreddit'
    CommentReplyTo:
      type: object
      required: [id]
      properties:
        id:
          type: string
          description: Reddit fullname of the comment this one replies to, prefixed with `t1_`.
    CommentAuthor:
      type: object
      required: [username, link]
      properties:
        username:
          type: string
          description: Username of the author.
        link:
          type: string
          description: Link to the author's profile.
    CommentPost:
      type: object
      required: [id, title, link, score, comments, iso_date]
      properties:
        id:
          type: string
          description: Reddit fullname of the post the comment was made on, prefixed with `t3_`.
        title:
          type: string
          description: Title of the post.
        link:
          type: string
          description: Link to the post on Reddit.
        score:
          type: integer
          description: Net vote score of the post.
        comments:
          type: integer
          description: Number of comments on the post.
        iso_date:
          type: string
          format: date-time
          description: Date the post was created, in ISO 8601.
        is_nsfw:
          type: boolean
          description: Whether the post is marked NSFW.
        is_spoiler:
          type: boolean
          description: Whether the post is marked as a spoiler.
    CommentSubreddit:
      type: object
      required: [name, id, link]
      properties:
        name:
          type: string
          description: Name of the subreddit the comment was made in, or `u_` followed by the username for a comment on a user's profile.
        id:
          type: string
          description: Reddit fullname of the subreddit, prefixed with `t5_`.
        link:
          type: string
          description: Link to the subreddit or profile.
        icon:
          type: string
          description: Icon image of the subreddit.
        is_nsfw:
          type: boolean
          description: Whether the subreddit is marked NSFW.
    Pagination:
      type: object
      properties:
        next_page_token:
          type: string
          description: Token to pass as `next_page_token` to get the next page of results.
    ErrorResponse:
      type: object
      required: [error]
      properties:
        error:
          type: string
          description: Error message describing what went wrong
