{
  "openapi": "3.1.0",
  "info": {
    "title": "unacasa API",
    "version": "1.1.0",
    "summary": "Facts and search for every parcel, building and cadastral unit in Spain, and the live adverts of agency websites.",
    "description": "Base URL https://api.unacasa.es. Every call needs an OAuth 2 access token of https://auth.unacasa.es/realms/unacasa (Authorization: Bearer). The token scope unacasa.read opens the facts and the search; the fields of the unacasa valuation models need unacasa.valuation (a field without the scope comes back with status forbidden in /facts, and gives 403 insufficient_scope in /search). A unit (flat, house, shop) has its own facts at /facts/unit/{unit_ref}. Every fact names its source, licence and data date. The field catalogues are data: GET /fields (facts) and GET /search/fields?entity= (search filters) list what each call accepts. The same facts and search are available to AI clients over MCP at POST /mcp. Specs: docs/FACTS_API_SPEC.md and SEARCH_SPEC.md.",
    "contact": {
      "name": "unacasa",
      "url": "https://unacasa.es/developers"
    }
  },
  "servers": [
    {
      "url": "https://api.unacasa.es"
    }
  ],
  "security": [
    {
      "oauth2": [
        "unacasa.read"
      ]
    }
  ],
  "tags": [
    {
      "name": "facts",
      "description": "The facts of one parcel, with field-level provenance."
    },
    {
      "name": "search",
      "description": "Search units, parcels, buildings and agency adverts by any catalogue field."
    },
    {
      "name": "places",
      "description": "Places by name (municipality, district, neighbourhood ...) for the place filter."
    },
    {
      "name": "mcp",
      "description": "The Model Context Protocol server for AI clients."
    }
  ],
  "paths": {
    "/facts/{refcat}": {
      "get": {
        "tags": [
          "facts"
        ],
        "operationId": "getParcelFacts",
        "summary": "The facts of a parcel",
        "description": "Every public field of the parcel (555 fields in 14 groups), or a selection by fields, groups or preset. A layer that fails does not stop the answer: it is listed in partial_failures and its fields have status failed.",
        "parameters": [
          {
            "$ref": "#/components/parameters/refcat"
          },
          {
            "$ref": "#/components/parameters/fields"
          },
          {
            "$ref": "#/components/parameters/groups"
          },
          {
            "$ref": "#/components/parameters/preset"
          },
          {
            "name": "format",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "flat",
                "nested"
              ],
              "default": "flat"
            },
            "description": "nested = the answer of each layer as one object"
          }
        ],
        "responses": {
          "200": {
            "description": "The facts",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Facts"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/facts": {
      "get": {
        "tags": [
          "facts"
        ],
        "operationId": "findParcelFacts",
        "summary": "The facts of the parcel at a point or an address",
        "parameters": [
          {
            "name": "lat",
            "in": "query",
            "schema": {
              "type": "number"
            },
            "description": "WGS84 latitude (with lon)"
          },
          {
            "name": "lon",
            "in": "query",
            "schema": {
              "type": "number"
            }
          },
          {
            "name": "address",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "a street address in Spain, e.g. Calle de Ferraz 94, Madrid"
          },
          {
            "name": "municipality",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "postal",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "$ref": "#/components/parameters/fields"
          },
          {
            "$ref": "#/components/parameters/groups"
          },
          {
            "$ref": "#/components/parameters/preset"
          }
        ],
        "responses": {
          "200": {
            "description": "The facts; parcel.found_by says how the parcel was found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Facts"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/fields": {
      "get": {
        "tags": [
          "facts"
        ],
        "operationId": "listFactFields",
        "summary": "The catalogue of the facts",
        "security": [],
        "parameters": [
          {
            "name": "group",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "entity",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "parcel",
                "unit"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "key, group, type, unit, label, description and source of each field",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/presets": {
      "get": {
        "tags": [
          "facts"
        ],
        "operationId": "listPresets",
        "summary": "The named field selections (farmland, buyer_home, climate, soil, hazards, building)",
        "security": [],
        "responses": {
          "200": {
            "description": "The presets",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/search": {
      "get": {
        "tags": [
          "search"
        ],
        "operationId": "search",
        "summary": "Search units, parcels, buildings or agency adverts",
        "description": "Filters are query parameters named after the fields of GET /search/fields?entity=: field=v (a comma list = any of), field.min, field.max, field.not, field.null=true|false, field.like (text). All filters must match. Examples: entity=unit&property_kind=FLAT&built_m2.min=80&floor_position=top,intermediate&facing=S,SE&coast_m.max=2000; entity=listing&operation=SALE&listing_kind=FLAT&amount.max=300000&place=<place_id>. count is the size of the whole result set (exact up to 100 000); a page has limit rows; next_cursor gives the next page in the same order.",
        "parameters": [
          {
            "name": "entity",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "unit",
                "parcel",
                "building",
                "listing"
              ],
              "default": "unit"
            }
          },
          {
            "name": "place",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "uuid"
            },
            "description": "a place_id of GET /places: the rows inside that place (any level, from region to census section)"
          },
          {
            "name": "near",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "lat,lon; with radius (metres, default 500, max 20 000)"
          },
          {
            "name": "radius",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 20000
            }
          },
          {
            "name": "bbox",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "west,south,east,north"
          },
          {
            "name": "poly",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "lon,lat;lon,lat;... a drawn area, 3 to 500 points"
          },
          {
            "name": "q",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "address text (for listing: title, address and locality)"
          },
          {
            "name": "sort",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "field[:asc|desc][,field...]; NULLs last"
          },
          {
            "name": "fields",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "comma list of fields to return (default: the main fields)"
          },
          {
            "name": "facets",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "comma list of fields: the top 20 values of each with their counts"
          },
          {
            "name": "summary",
            "in": "query",
            "schema": {
              "type": "boolean"
            },
            "description": "medians and range over the whole result set (unit and parcel need unacasa.valuation)"
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 1000,
              "default": 50
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "next_cursor of the previous page (same filters and sort)"
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 0
            },
            "description": "rows to skip (use cursor for deep pages)"
          },
          {
            "name": "format",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "json",
                "refs",
                "parcels",
                "groups"
              ],
              "default": "json"
            },
            "description": "refs = one reference per line (header X-Next-Cursor); parcels = the distinct parcels; groups = one row per parcel of a unit search (needs unacasa.valuation)"
          }
        ],
        "responses": {
          "200": {
            "description": "One page of the result set",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SearchResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/InsufficientScope"
          }
        }
      }
    },
    "/search/fields": {
      "get": {
        "tags": [
          "search"
        ],
        "operationId": "listSearchFields",
        "summary": "The filter fields of a search entity",
        "parameters": [
          {
            "name": "entity",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "unit",
                "parcel",
                "building",
                "listing"
              ],
              "default": "unit"
            }
          },
          {
            "name": "lang",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "es",
                "en"
              ],
              "default": "es"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "code, label, type, ops, facet, sortable, default, virtual values and the token scope of each field",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/places": {
      "get": {
        "tags": [
          "places"
        ],
        "operationId": "searchPlaces",
        "summary": "Places, streets and addresses by name",
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "kinds",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "comma list of place kinds (municipality, district, neighbourhood, street ...)"
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "maximum": 50,
              "default": 10
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The hits, best first, each with its id, kind, name and the places above it",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/mcp": {
      "post": {
        "tags": [
          "mcp"
        ],
        "operationId": "mcp",
        "summary": "MCP server (streamable HTTP, JSON-RPC 2.0, one JSON answer per request)",
        "description": "Tools: find_parcel, get_parcel_facts, get_climate, get_soil, get_valuation, get_unit_facts, list_fields, find_place, search_fields, search. Protocol versions 2025-11-25, 2025-06-18, 2025-03-26. Without a token the answer is 401 with WWW-Authenticate resource_metadata (RFC 9728): GET /.well-known/oauth-protected-resource names the authorization server.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The JSON-RPC answer"
          },
          "202": {
            "description": "A notification was accepted"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/facts/unit/{unit_ref}": {
      "get": {
        "tags": [
          "facts"
        ],
        "operationId": "getUnitFacts",
        "summary": "The facts of a cadastral unit",
        "description": "A flat, house, shop or parking space: use, surfaces, floor and floor position, building, energy certificate, tourist licences; with unacasa.valuation the sale and rent estimates, the Catastro reference value and the yearly IBI estimate. parcel=true adds the facts of its parcel.",
        "parameters": [
          {
            "name": "unit_ref",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[0-9A-Za-z]{20}$|^(NA|VI|SS|BI)[0-9A-Z]{15,19}$"
            },
            "example": "8960309VK3786B0009XY"
          },
          {
            "$ref": "#/components/parameters/fields"
          },
          {
            "$ref": "#/components/parameters/groups"
          },
          {
            "$ref": "#/components/parameters/preset"
          },
          {
            "name": "parcel",
            "in": "query",
            "schema": {
              "type": "boolean",
              "default": false
            },
            "description": "add the facts of the parcel"
          }
        ],
        "responses": {
          "200": {
            "description": "The facts; unit (and parcel) say which unit and parcel",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Facts"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/place-facts": {
      "get": {
        "summary": "Figures of an area or a street",
        "description": "The rows of a unacasa area or street page (municipality, district, barrio, postal code, census section, street), the same as its CSV: id, label, value, unit, period, source. Give the page path or a place_id from GET /places. Sale prices are not included. Without a token: 20 calls a day per address; a free account: 100.",
        "operationId": "getPlaceFacts",
        "parameters": [
          {
            "name": "path",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            },
            "example": "/madrid/zona-malasana",
            "description": "path of the unacasa page"
          },
          {
            "name": "place_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "format": "uuid"
            },
            "description": "a place id from GET /places"
          },
          {
            "name": "lang",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "es",
                "en"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "title, url, citation, attribution, sources, data (rows), tables (full tables with CSV/JSON links)"
          },
          "404": {
            "description": "no area or street page at this path"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "oauth2": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://auth.unacasa.es/realms/unacasa/protocol/openid-connect/auth",
            "tokenUrl": "https://auth.unacasa.es/realms/unacasa/protocol/openid-connect/token",
            "scopes": {
              "unacasa.read": "facts and search",
              "unacasa.valuation": "the unacasa valuation fields",
              "unacasa.reports": "location reports"
            }
          },
          "clientCredentials": {
            "tokenUrl": "https://auth.unacasa.es/realms/unacasa/protocol/openid-connect/token",
            "scopes": {
              "unacasa.read": "facts and search",
              "unacasa.valuation": "the unacasa valuation fields",
              "unacasa.reports": "location reports"
            }
          }
        }
      }
    },
    "parameters": {
      "refcat": {
        "name": "refcat",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string",
          "pattern": "^[0-9A-Za-z]{14}$|^(NA|VI|SS|BI)[0-9A-Z]{9,12}$"
        },
        "description": "the 14-character cadastral parcel reference, or a foral parcel key",
        "example": "8960309VK3786B"
      },
      "fields": {
        "name": "fields",
        "in": "query",
        "schema": {
          "type": "string"
        },
        "description": "comma list of field keys of GET /fields; a key ending in * is a prefix"
      },
      "groups": {
        "name": "groups",
        "in": "query",
        "schema": {
          "type": "string"
        },
        "description": "comma list: cadastre, building, land_use, farming, planning, terrain, hazards, infrastructure, proximity, area, location, energy, valuation, climate, soil, sunlight (unit facts: unit, energy, valuation)"
      },
      "preset": {
        "name": "preset",
        "in": "query",
        "schema": {
          "type": "string"
        },
        "description": "a preset of GET /presets"
      }
    },
    "schemas": {
      "Fact": {
        "type": "object",
        "properties": {
          "value": {},
          "unit": {
            "type": [
              "string",
              "null"
            ]
          },
          "type": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "ok",
              "absent",
              "failed",
              "forbidden"
            ]
          },
          "source": {
            "type": "string"
          },
          "source_provider": {
            "type": "string"
          },
          "source_url": {
            "type": "string"
          },
          "licence": {
            "type": "string"
          },
          "dataset_vintage": {
            "type": [
              "string",
              "null"
            ]
          },
          "needs_scope": {
            "type": "string"
          }
        }
      },
      "Facts": {
        "type": "object",
        "properties": {
          "parcel": {
            "type": "object",
            "properties": {
              "refcat": {
                "type": "string"
              },
              "lat": {
                "type": "number"
              },
              "lon": {
                "type": "number"
              },
              "url": {
                "type": "string"
              },
              "found_by": {
                "type": "object"
              }
            }
          },
          "fields": {
            "type": "object",
            "additionalProperties": {
              "$ref": "#/components/schemas/Fact"
            }
          },
          "counts": {
            "type": "object"
          },
          "partial_failures": {
            "type": "array",
            "items": {
              "type": "object"
            }
          }
        }
      },
      "SearchResult": {
        "type": "object",
        "properties": {
          "entity": {
            "type": "string"
          },
          "count": {
            "type": "integer"
          },
          "count_exact": {
            "type": "boolean"
          },
          "took_ms": {
            "type": "integer"
          },
          "items": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "ref": {
                  "type": "string"
                }
              },
              "additionalProperties": true
            }
          },
          "facets": {
            "type": "object",
            "additionalProperties": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "value": {
                    "type": [
                      "string",
                      "null"
                    ]
                  },
                  "n": {
                    "type": "integer"
                  }
                }
              }
            }
          },
          "summary": {
            "type": "object"
          },
          "next_cursor": {
            "type": "string"
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "detail": {
            "type": "string"
          },
          "needs_scope": {
            "type": "string"
          }
        }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "A parameter is not valid",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "No valid access token",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "InsufficientScope": {
        "description": "The token lacks the scope of a field or an option (needs_scope)",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "NotFound": {
        "description": "No such parcel",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    }
  }
}
