openapi: 3.0.0
info:
  title: Instagram Hashtag API
  description: |
    The Instagram Hashtag API returns the top reels Instagram shows publicly for a hashtag, with the
    hashtag's rounded reel total, Instagram's summary of it and the related searches it suggests.
    Instagram matches reels to the hashtag as a keyword, so a reel's caption may not carry the hashtag,
    and a rare hashtag such as `kccurling` can return reels that never use it. Most posts are reels, and
    the occasional photo carousel comes back with `type` `carousel`, a `/p/` permalink and its photo as `link`.

    A hashtag page returns its top reels 12 at a time. Each response carries a `pagination.next_page_token`
    while Instagram has more reels. Pass it back as `next_page_token`, with the same `hashtag`, for the next
    page. It is absent on the last page. Instagram's list moves between calls, so a later page can repeat
    reels from an earlier one, and a token past the end of the list answers with an `error` and is billed.
    The reels carry no likes, comment counts or posting dates, and the recent tab is not returned.

    For broad hashtags such as `football`, Instagram often shows a category page instead. Its reels are
    grouped under related keywords, each reel carries the `keyword` it is listed under, and these reels
    have no caption, video link or author username. A category page comes in one response, with no
    further pages.

    Instagram has no public page for some hashtags, popular ones and any with an underscore included.
    The search then answers with an `error` and is billed.

    Direct Instagram CDN URLs are signed and expire. Repeat the search to get fresh URLs.

    **Cross-linking**: Pass a reel's `id` as the `post_id` of the Instagram Post API for its likes,
    comments, posting date and author name. Pass a reel's author `username` as the `username` of the
    Instagram Profile API for the author's profile.
  version: 1.0.0
servers:
  - url: https://www.searchapi.io/api/v1
paths:
  /search:
    get:
      summary: Instagram Hashtag
      security:
        - ApiKeyAuth: []
        - ApiKeyQuery: []
      parameters:
        - name: engine
          in: query
          required: true
          description: Search engine identifier
          schema:
            type: string
            enum: ["instagram_hashtag"]
            default: "instagram_hashtag"
        - name: hashtag
          in: query
          required: true
          description: The hashtag to look up, with or without the leading "#" (e.g., "sourdoughbread" or "#sourdoughbread"), made of letters, numbers, underscores and emoji, up to 100 characters not counting the leading "#", and matched regardless of letter case. Instagram has no public page for a hashtag with an underscore, such as "new_york", which returns the no-page error, while "newyork" has one.
          schema:
            type: string
          example: "sourdoughbread"
        - name: next_page_token
          in: query
          required: false
          description: Token for the next page of reels, taken from `pagination.next_page_token` in the previous response for the same `hashtag`. Omit it for the first page. A later page can repeat reels from an earlier one, because Instagram's list moves between calls. A token Instagram no longer accepts answers 200 with an `error` and costs a credit.
          schema:
            type: string
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SearchResponse'
        '400':
          description: Validation Error. A required query parameter is missing or invalid.
          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, or Instagram answered but returned nothing usable. The body is a bare
            {"error": ...} rather than a search response, and the search is not billed.
            A hashtag Instagram has no public page for is not this case: it is a 200 with an error.
          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'
        hashtag:
          $ref: '#/components/schemas/Hashtag'
        posts:
          type: array
          description: Top posts Instagram matches to the hashtag as a keyword, mostly reels, in the order Instagram shows them.
          items:
            $ref: '#/components/schemas/Post'
        related_searches:
          type: array
          description: Related searches Instagram suggests for the hashtag.
          items:
            $ref: '#/components/schemas/RelatedSearch'
        related_interests:
          type: array
          description: Other Instagram categories a category page links to.
          items:
            $ref: '#/components/schemas/RelatedInterest'
        pagination:
          $ref: '#/components/schemas/Pagination'
        error:
          type: string
          description: Error message when Instagram has no public page for the hashtag, or when the next_page_token is past the end of its list or no longer accepted.
    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: Instagram 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 identifier
        hashtag:
          type: string
          description: The hashtag looked up, without the leading "#"
        next_page_token:
          type: string
          description: The next_page_token the page was requested with
    Hashtag:
      type: object
      required: [name, title, link]
      properties:
        name:
          type: string
          description: The hashtag without the leading "#", as it appears in its Instagram page link.
        title:
          type: string
          description: The hashtag with its leading "#" (e.g., "#sourdoughbread").
        link:
          type: string
          description: URL of the hashtag's page on Instagram.
        reels:
          type: string
          description: Number of reels Instagram counts for the hashtag, rounded as Instagram displays it (e.g., "18M").
        extracted_reels:
          type: integer
          description: The reel count expanded from the rounded label (e.g., 18000000 for "18M").
        description:
          type: string
          description: Instagram's generated summary of the hashtag, which Instagram sometimes writes in a language other than English.
        sources:
          type: array
          description: Pages Instagram cites for its summary.
          items:
            $ref: '#/components/schemas/Source'
    Source:
      type: object
      required: [link]
      properties:
        link:
          type: string
          description: URL of a page Instagram cites.
    Post:
      type: object
      required: [position, id, media_id, permalink, type, user]
      properties:
        position:
          type: integer
          description: Position of the post in the results, starting at 1.
        id:
          type: string
          description: Shortcode of the post, usable as the post_id of the Instagram Post API.
        media_id:
          type: string
          description: Instagram's numeric media id of the post.
        permalink:
          type: string
          description: URL of the post on Instagram, under `/reel/` for a reel and `/p/` otherwise.
        type:
          type: string
          description: Media type, `reel` for most posts, `carousel` for a photo carousel or `image` for a single photo.
        link:
          type: string
          description: URL of the reel's video file, or of the photo for a carousel or image post.
        caption:
          type: string
          description: Caption of the post.
        views:
          type: integer
          description: Number of times the reel was played.
        has_like_and_view_counts_disabled:
          type: boolean
          description: Whether the author hides the post's like and view counts on Instagram.
        keyword:
          type: string
          description: Related keyword the reel is listed under on a category page.
        user:
          $ref: '#/components/schemas/PostUser'
        video_versions:
          type: array
          description: Video files of the reel.
          items:
            $ref: '#/components/schemas/VideoVersion'
        thumbnail:
          type: string
          description: URL of the post's cover image.
    PostUser:
      type: object
      required: [id]
      properties:
        id:
          type: string
          description: Instagram's numeric user id of the author.
        username:
          type: string
          description: Instagram username of the author.
        avatar:
          type: string
          description: URL of the author's profile picture.
        is_verified:
          type: boolean
          description: Whether the author's account is verified.
    VideoVersion:
      type: object
      required: [url]
      properties:
        url:
          type: string
          description: URL of the video file.
    RelatedSearch:
      type: object
      required: [query, link]
      properties:
        query:
          type: string
          description: Related search Instagram suggests.
        link:
          type: string
          description: URL of the related search's page on Instagram.
    RelatedInterest:
      type: object
      required: [name, link]
      properties:
        name:
          type: string
          description: Name of the category.
        link:
          type: string
          description: URL of the category's page on Instagram.
        thumbnail:
          type: string
          description: URL of the category's cover image.
    Pagination:
      type: object
      required: [next_page_token]
      properties:
        next_page_token:
          type: string
          description: Token for the next page of reels, to pass as `next_page_token` with the same `hashtag`.
    ErrorResponse:
      type: object
      required: [error]
      properties:
        error:
          type: string
          description: Error message describing what went wrong
