openapi: "3.0.3"
info:
  title: BaanMe Thailand Property API
  description: >
    Public read-only endpoints for Thailand real estate data.
    Provides market statistics, area/region information, property search,
    and blog content. No authentication required for read operations.
  version: "1.0.0"
  contact:
    email: support@baanme.com
    url: https://baanme.com
servers:
  - url: https://api.thaipropertiesnetwork.com
    description: Production API

paths:
  /properties/search/by-query:
    get:
      operationId: searchProperties
      summary: Find condos, houses, and other listings for rent or sale in Thailand
      description: >
        Search BaanMe's active public listings. Use this operation for requests
        such as "find a condo in Bangkok". Each result includes the transaction,
        asking price with its currency, and its canonical BaanMe listing link. Rental
        prices are monthly. Use next_offset with the same filters for the next page.
      parameters:
        - name: location
          in: query
          description: Thai place name or BaanMe location slug (e.g. Bangkok, Chiang Mai, or on-nut-station). Transit searches use a 2 km radius.
          schema: { type: string }
          example: Bangkok
        - name: property_type
          in: query
          description: Canonical id or alias, case-insensitive. house and home mean detached_house; apartment and flat mean condo or serviced_apartment.
          schema: { type: string, enum: ["condo","serviced_apartment","detached_house","townhouse","land","office","retail","warehouse","hotel","building","gastronomy","industrial","house","home","apartment","flat"] }
          example: condo
        - name: offer
          in: query
          description: Rent or sale. Omit for both. Specify one transaction when interpreting a user's budget.
          schema: { type: string, enum: [rent, sale] }
        - name: price_min
          in: query
          description: Minimum asking price in THB. Use with offer to distinguish monthly rent and total purchase price.
          schema: { type: number, minimum: 0 }
        - name: price_max
          in: query
          description: Maximum asking price in THB. Use with offer to distinguish monthly rent and total purchase price.
          schema: { type: number, minimum: 0 }
        - name: bedrooms_min
          in: query
          schema: { type: integer, minimum: 0 }
        - name: bedrooms_max
          in: query
          description: Set both bedroom bounds to the same number for an exact count. Zero means studio.
          schema: { type: integer, minimum: 0 }
        - name: pet_policy
          in: query
          schema: { type: string, enum: [cats, small-dogs, all-dogs] }
          description: Required advertised rental pet policy.
        - name: furnishing
          in: query
          schema: { type: string, enum: [furnished, part_furnished, unfurnished] }
          description: Required advertised furnishing.
        - name: lease_months
          in: query
          schema: { type: integer, enum: [1, 3, 6, 12, 36, 60, 120, 240] }
          description: Exact advertised rental lease length in months.
        - name: move_in_by
          in: query
          schema: { type: string, format: date }
          description: Require an advertised available date on or before this date.
        - name: transit_distance_m
          in: query
          schema: { type: integer, minimum: 1, maximum: 2000 }
          description: Straight-line radius in metres. Requires a station location.
        - name: include_near_misses
          in: query
          schema: { type: boolean, default: false }
          description: If no exact matches are found, include separate options up to 10% over the price cap.
        - name: locale
          in: query
          schema: { type: string, enum: [en, th, zh], default: en }
        - name: limit
          in: query
          schema: { type: integer, minimum: 1, maximum: 24, default: 10 }
        - name: offset
          in: query
          description: Pass next_offset from a previous response.
          schema: { type: integer, minimum: 0, default: 0 }
      responses:
        "200":
          description: Matching public listings and pagination
          content:
            application/json:
              schema:
                type: object
                required: [total_count, returned, next_offset, resolved_location, resolved_location_title, transit_distance_m, near_misses, results, shortlist_url]
                properties:
                  total_count: { type: integer, description: Catalog count before result projection }
                  returned: { type: integer }
                  next_offset: { type: integer, nullable: true }
                  resolved_location: { type: string, nullable: true }
                  resolved_location_title: { type: string, nullable: true, description: Human-readable place the location text was interpreted as when it was resolved by name matching or an alias }
                  transit_distance_m: { type: integer, nullable: true }
                  shortlist_url: { type: string, format: uri, nullable: true }
                  results:
                    type: array
                    items:
                      $ref: '#/components/schemas/BaanMePropertyMatch'
                  near_misses:
                    type: array
                    items:
                      type: object
                      properties:
                        listing:
                          $ref: '#/components/schemas/BaanMePropertyMatch'
                        missed_constraints:
                          type: array
                          items: { type: string, enum: [price_max] }
        "400":
          description: Invalid search filters
        "404":
          description: Location could not be resolved
        "429":
          description: Too many requests
        "503":
          description: Property search temporarily unavailable

  /market-stats/{regionType}/{regionId}:
    get:
      operationId: getMarketStats
      summary: Get real estate market statistics for a Thai region
      description: >
        Returns price medians, percentiles, rental yields, and inventory counts
        for a specific province, district, or subdistrict. Data is broken down
        by property type (condo, house, townhouse, etc.) and bedroom count.
        Refreshed every 6 hours from live listing data.
      parameters:
        - name: regionType
          in: path
          required: true
          schema:
            type: string
            enum: [province, district, subdistrict]
          description: Geographic level
        - name: regionId
          in: path
          required: true
          schema:
            type: string
          description: Numeric ID of the region
        - name: locale
          in: query
          schema:
            type: string
            enum: [en, th, zh]
            default: en
          description: Language for region names
      responses:
        "200":
          description: Market statistics
          content:
            application/json:
              schema:
                type: object
                properties:
                  location:
                    type: object
                    properties:
                      type:
                        type: string
                      id:
                        type: string
                      name:
                        type: string
                  updatedAt:
                    type: string
                    format: date-time
                  summary:
                    type: object
                    properties:
                      totalActive:
                        type: integer
                        description: Total active listings in region
                      forSale:
                        type: integer
                      forRent:
                        type: integer
                      newListings30d:
                        type: integer
                        description: New listings in the last 30 days
                  byPropertyType:
                    type: object
                    description: Stats keyed by property type (condo, detached_house, etc.)
                    additionalProperties:
                      type: object
                      properties:
                        count:
                          type: integer
                        sale:
                          type: object
                          nullable: true
                          properties:
                            median:
                              type: number
                              description: Median sale price in THB
                            p25:
                              type: number
                            p75:
                              type: number
                            medianPerSqm:
                              type: number
                        rent:
                          type: object
                          nullable: true
                          properties:
                            median:
                              type: number
                              description: Median monthly rent in THB
                            p25:
                              type: number
                            p75:
                              type: number
                            medianPerSqm:
                              type: number
                        avgArea:
                          type: number
                          nullable: true
                          description: Average indoor area in sqm
                        byBedroom:
                          type: object
                          additionalProperties:
                            type: object
                            properties:
                              count:
                                type: integer
                              saleMedian:
                                type: number
                                nullable: true
                              rentMedian:
                                type: number
                                nullable: true
                  grossYield:
                    type: object
                    description: Annual gross rental yield percentage per property type
                    additionalProperties:
                      type: number
                      nullable: true

  /regions/by-slug/{slug}:
    get:
      operationId: getRegionBySlug
      summary: Get area/region details by URL slug
      description: >
        Returns region information including name, description, editorial guide
        content, geographic hierarchy (breadcrumbs), and bounding box.
        Supports provinces, districts, subdistricts, projects, and transit stops.
      parameters:
        - name: slug
          in: path
          required: true
          schema:
            type: string
          description: URL slug (e.g., "bangkok", "chiang-mai", "sukhumvit")
        - name: locale
          in: query
          schema:
            type: string
            enum: [en, th, zh]
            default: en
      responses:
        "200":
          description: Region details
          content:
            application/json:
              schema:
                type: object
                properties:
                  type:
                    type: string
                    enum: [province, district, subdistrict, country, project, transport_stop]
                  id:
                    type: integer
                  slug:
                    type: string
                  name:
                    type: string
                  description:
                    type: string
                  guideContent:
                    type: string
                    nullable: true
                    deprecated: true
                    description: >-
                      Rich editorial content about the area, as HTML. Derived
                      from guideDocument and kept for compatibility; prefer
                      guideDocument, which is the source of truth.
                  guideDocument:
                    type: object
                    nullable: true
                    description: >-
                      Structured editorial content about the area. An ordered
                      list of blocks (h3/h4/p/ul), each carrying text spans
                      that may be bold. Null for areas not yet migrated from
                      the HTML form.
                    properties:
                      version:
                        type: integer
                        enum: [1]
                      blocks:
                        type: array
                        items:
                          type: object
                  breadcrumbs:
                    type: array
                    items:
                      type: object
                      properties:
                        name:
                          type: string
                        slug:
                          type: string

  /blog/posts:
    get:
      operationId: listBlogPosts
      summary: List published blog posts
      parameters:
        - name: category
          in: query
          schema:
            type: string
        - name: page
          in: query
          schema:
            type: integer
            default: 1
        - name: limit
          in: query
          schema:
            type: integer
            default: 12
            maximum: 50
        - name: lang
          in: query
          schema:
            type: string
            enum: [en, th, zh]
            default: en
      responses:
        "200":
          description: Paginated blog posts

  /blog/posts/{slug}:
    get:
      operationId: getBlogPost
      summary: Get a single blog post by slug
      parameters:
        - name: slug
          in: path
          required: true
          schema:
            type: string
        - name: lang
          in: query
          schema:
            type: string
            enum: [en, th, zh]
            default: en
      responses:
        "200":
          description: Blog post with full HTML content

  /regions/provinces:
    get:
      operationId: listProvinces
      summary: List Thai provinces
      parameters:
        - name: countryId
          in: query
          schema:
            type: integer
            default: 1
          description: Country ID (1 = Thailand)
        - name: locale
          in: query
          schema:
            type: string
            enum: [en, th, zh]
            default: en
        - name: limit
          in: query
          schema:
            type: integer
            default: 12
      responses:
        "200":
          description: List of provinces with property counts

  /search/autocomplete/{tx}/{query}:
    get:
      operationId: searchAutocomplete
      summary: Search for locations, properties, and projects
      description: >
        Autocomplete search supporting Thai romanization variants,
        phonetic matching, and fuzzy matching. Returns locations,
        properties, projects, and transit stops ranked by relevance
        and popularity. The {tx} segment controls which inventory the
        returned resultsCount reflects — rent or sale. An unrecognized
        value (e.g. a stale client's "any") falls back to rent.
      parameters:
        - name: tx
          in: path
          required: true
          schema:
            type: string
            enum: [rent, sale]
          description: Transaction filter for the returned resultsCount
        - name: query
          in: path
          required: true
          schema:
            type: string
        - name: locale
          in: query
          schema:
            type: string
            enum: [en, th, zh]
            default: en
        - name: type
          in: query
          schema:
            type: string
          description: Comma-separated types to filter (locations, properties, projects)
      responses:
        "200":
          description: Search results with type, title, and link
components:
  schemas:
    BaanMePropertyMatch:
      type: object
      properties:
        id: { type: string, nullable: true }
        title: { type: string, nullable: true }
        offer_id: { type: string, format: uuid, nullable: true }
        offer: { type: string, enum: [rent, sale] }
        listing_status: { type: string, enum: [active], description: Published listing status. Confirm current availability with the listing agent. }
        price: { type: number, nullable: true }
        currency: { type: string, nullable: true }
        price_period: { type: string, enum: [month, total] }
        price_thb: { type: number, nullable: true, description: Asking price when the listing currency is THB }
        contract_months: { type: number, nullable: true }
        pet_policy: { type: string, nullable: true }
        furnishing: { type: string, nullable: true }
        advertised_available_from: { type: string, format: date, nullable: true }
        matched_constraints:
          type: array
          items: { type: string }
        property_type: { type: string, nullable: true }
        bedrooms: { type: integer, nullable: true }
        bathrooms: { type: integer, nullable: true }
        indoor_area_sqm: { type: number, nullable: true }
        district: { type: string, nullable: true }
        province: { type: string, nullable: true }
        url: { type: string, format: uri }
