{
  "openapi": "3.1.0",
  "info": {
    "title": "TownIndex local business search",
    "version": "1.0.0",
    "summary": "Find local businesses near a place: trade, problem, name, open now, taking emergency calls now; request an appointment.",
    "description": "TownIndex is a free, ad-free directory of local businesses in all 50 US states and DC. This API answers one person's question at a time; no key or sign-in. It can also send an appointment request to a business for the user, after the user confirms their email with a code. Every result links to its TownIndex page (credit TownIndex when you show results). Not for downloading the directory: see the terms of use (https://townindex.org/terms/). AI assistants can also use the MCP server at https://townindex.org/api/mcp. Please keep to about one request a second: faster clients get 429 with Retry-After.",
    "contact": { "url": "https://townindex.org/about/" },
    "termsOfService": "https://townindex.org/terms/"
  },
  "servers": [{ "url": "https://townindex.org" }],
  "paths": {
    "/api/search": {
      "get": {
        "operationId": "searchBusinesses",
        "summary": "Search local businesses, nearest first",
        "description": "Plain words: what and where (\"plumber 06114\", \"dentist in Wolcott CT\", \"roofer Texas\"). Keep it to the trade, problem, specialty or name and the place: other words (\"best\", \"cheap\", \"near me\") must match the business's own text, which narrows and slows the search. The place, best first: a ZIP (nearest first within 25 miles, with miles), a town with its state (town names repeat across states), a state; or pass the searcher's ZIP as near. Problems are read as the trade that fixes them (\"no heat\" = HVAC). For anything urgent, search the trade with the place and set emergency=1 rather than the word \"emergency\". A filter that finds nothing returns the unfiltered results, marked fallback. Hours are known for only some businesses: suggest calling ahead.",
        "parameters": [
          { "name": "q", "in": "query", "required": true, "description": "What and where, in plain words (2 to 200 characters): a trade, problem, specialty or business name, and a ZIP, a town with its state, or a state. E.g. \"plumber 06114\", \"no heat Wolcott CT\", \"dentist tx\".", "schema": { "type": "string", "minLength": 2, "maxLength": 200 } },
          { "name": "near", "in": "query", "description": "The searcher's 5-digit ZIP, used when the words name no place: nearest first within 25 miles. A place in the words wins.", "schema": { "type": "string", "pattern": "^[0-9]{5}$" } },
          { "name": "open", "in": "query", "description": "1: only businesses open now.", "schema": { "type": "string", "enum": ["1"] } },
          { "name": "emergency", "in": "query", "description": "1: only businesses taking emergency calls now (24/7 emergency service or on call). Use it for anything urgent, with the trade and place in q.", "schema": { "type": "string", "enum": ["1"] } },
          { "name": "state", "in": "query", "description": "Two-letter state code, lower case (ct, tx...).", "schema": { "type": "string", "pattern": "^[a-z]{2}$" } },
          { "name": "page", "in": "query", "description": "20 results a page, 1 to 50.", "schema": { "type": "integer", "minimum": 1, "maximum": 50, "default": 1 } }
        ],
        "responses": {
          "200": { "description": "A page of results", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/SearchAnswer" } } } },
          "400": { "description": "A parameter is invalid", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "429": { "description": "Too many requests: wait as long as the Retry-After header says" }
        }
      }
    },
    "/api/on-call/near": {
      "get": {
        "operationId": "onCallNear",
        "summary": "Emergency providers on call right now near a point",
        "description": "Businesses whose owner has switched on \"on call\" right now, nearest first. Often empty: then use searchBusinesses with emergency=1.",
        "parameters": [
          { "name": "lat", "in": "query", "required": true, "schema": { "type": "number", "minimum": -90, "maximum": 90 } },
          { "name": "lon", "in": "query", "required": true, "schema": { "type": "number", "minimum": -180, "maximum": 180 } },
          { "name": "miles", "in": "query", "schema": { "type": "number", "exclusiveMinimum": 0, "maximum": 100, "default": 15 } },
          { "name": "category", "in": "query", "description": "A trade slug (plumbers, electricians, hvac, locksmiths, towing...).", "schema": { "type": "string", "pattern": "^[a-z-]{2,40}$" } }
        ],
        "responses": {
          "200": { "description": "Providers on call", "content": { "application/json": { "schema": { "type": "object", "properties": { "on_call": { "type": "array", "items": { "type": "object" } } } } } } },
          "400": { "description": "A parameter is invalid", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
        }
      }
    },
    "/api/requests": {
      "post": {
        "operationId": "requestAppointment",
        "summary": "Ask a business for an appointment for the user (step 1 of 2)",
        "description": "Only when the user asks, with their real name and email. Works for businesses that take requests (most appointment-type trades and offices with an email on file); others answer 400. We email the user a 6-digit code; nothing reaches the business until confirmRequest. The business contacts the user directly. Keep medical and other personal details out of the message.",
        "requestBody": { "required": true, "content": { "application/json": { "schema": {
          "type": "object", "required": ["business_id", "name", "email", "contact_by", "message"],
          "properties": {
            "business_id": { "type": "string", "format": "uuid", "description": "The business id from search." },
            "name": { "type": "string", "minLength": 2, "maxLength": 80, "description": "The user's name." },
            "email": { "type": "string", "maxLength": 200, "description": "The user's email: the code goes there." },
            "contact_by": { "type": "string", "enum": ["email", "text"], "description": "How the business should reach the user." },
            "phone": { "type": "string", "description": "The user's US mobile number, required for text." },
            "message": { "type": "string", "minLength": 5, "maxLength": 500, "description": "What the user needs, in a sentence or two." }
          } } } } },
        "responses": {
          "200": { "description": "Code emailed to the user", "content": { "application/json": { "schema": { "type": "object", "properties": {
            "request_id": { "type": "string", "format": "uuid" }, "code_sent_to": { "type": "string" }, "minutes": { "type": "integer" } } } } } },
          "400": { "description": "Not accepted (the business doesn't take requests, a limit, a missing field): the error says why", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
        }
      }
    },
    "/api/requests/{request_id}/confirm": {
      "post": {
        "operationId": "confirmRequest",
        "summary": "Send the request to the business with the user's 6-digit code (step 2 of 2)",
        "parameters": [{ "name": "request_id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
        "requestBody": { "required": true, "content": { "application/json": { "schema": {
          "type": "object", "required": ["code"], "properties": { "code": { "type": "string", "pattern": "^[0-9]{6}$" } } } } } },
        "responses": {
          "200": { "description": "Sent to the business", "content": { "application/json": { "schema": { "type": "object", "properties": {
            "sent": { "type": "boolean" }, "business": { "type": "string" }, "business_phone": { "type": "string" },
            "contact_by": { "type": "string" }, "already": { "type": "boolean" } } } } } },
          "400": { "description": "Wrong or expired code: the error says how many tries are left", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "SearchAnswer": {
        "type": "object",
        "properties": {
          "query": { "type": "string" },
          "total": { "type": "integer", "description": "How many match (at most 1,000 are paged)." },
          "offset": { "type": "integer" },
          "fallback": { "type": "string", "enum": ["open", "emergency"], "description": "The filter found nothing: these are the unfiltered results." },
          "understood": {
            "type": "object",
            "description": "How the words were read.",
            "properties": {
              "category": { "type": "object", "properties": { "slug": { "type": "string" }, "name": { "type": "string" }, "emergency": { "type": "boolean" } } },
              "town": { "type": "object", "properties": { "name": { "type": "string" }, "states": { "type": "array", "items": { "type": "string" } } } },
              "zip": { "type": "object", "properties": { "code": { "type": "string" }, "town": { "type": "string" }, "state": { "type": "string" }, "unknown": { "type": "boolean" } } },
              "words": { "type": "string" }, "open_now": { "type": "boolean" }, "emergency": { "type": "boolean" }
            }
          },
          "results": { "type": "array", "items": { "$ref": "#/components/schemas/Business" } }
        }
      },
      "Business": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "name": { "type": "string" },
          "category": { "type": "string" }, "category_name": { "type": "string" },
          "street": { "type": "string" }, "town": { "type": "string" }, "state": { "type": "string" },
          "phone": { "type": "string", "description": "E.164, e.g. +12035551234" },
          "website": { "type": "string" },
          "lat": { "type": "number" }, "lon": { "type": "number" },
          "miles": { "type": "number", "description": "Distance from the place searched, when there is one." },
          "hours": { "type": "object", "description": "Weekly hours, when known: mo..su, each a list of [open, close] times." },
          "emergency_hours": { "type": "object", "description": "When it takes emergency calls, when known." },
          "open_now": { "type": "boolean" },
          "license_verified": { "type": "boolean" },
          "claimed": { "type": "boolean", "description": "The owner has claimed and maintains the listing." },
          "chain": { "type": "boolean" },
          "url": { "type": "string", "description": "The business's TownIndex page." }
        }
      },
      "Error": { "type": "object", "properties": { "error": { "type": "string" } } }
    }
  }
}
