{
  "openapi": "3.1.0",
  "info": {
    "title": "CropHist API",
    "version": "1.0.0",
    "summary": "Field-level crop history for 63 million fields in the US, Canada, Argentina and Brazil.",
    "description": "Year-by-year crop history for agricultural fields, looked up by field ID or by latitude/longitude. Crops come from national satellite crop maps: USDA Cropland Data Layer (US, 2008-2025, plus a 2026 preseason forecast and CDL confidence for 2017-2023), AAFC Annual Crop Inventory (Canada, 2009-2025), INTA Mapa Nacional de Cultivos (Argentina, 2019-2025) and MapBiomas Collection 11 (Brazil, 1985-2025). Field boundaries by Digifarm. Free tier: 100 lookups/day with a key from POST /v1/signup. Docs: https://api.crophist.com/ . MCP server: https://mcp.crophist.com/ .",
    "contact": {"name": "Digifarm", "email": "hello@digifarm.io", "url": "https://crophist.com/"}
  },
  "servers": [{"url": "https://api.crophist.com"}],
  "security": [{"bearerAuth": []}],
  "tags": [
    {"name": "fields", "description": "Crop history per field"},
    {"name": "areas", "description": "Crop statistics for an area"},
    {"name": "account", "description": "API key and usage"}
  ],
  "paths": {
    "/v1/field/{field_id}": {
      "get": {
        "tags": ["fields"],
        "operationId": "getFieldById",
        "summary": "Crop history of one field by its CropHist field ID",
        "description": "US field IDs look like '5005.6V73'; Canada, Argentina and Brazil IDs carry a 'ca_', 'ar_' or 'br_' prefix.",
        "parameters": [{"name": "field_id", "in": "path", "required": true, "schema": {"type": "string"}, "example": "5005.6V73"}],
        "responses": {
          "200": {"description": "Field history", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/FieldHistory"}}}},
          "401": {"$ref": "#/components/responses/Unauthorized"},
          "404": {"$ref": "#/components/responses/Error"},
          "429": {"$ref": "#/components/responses/RateLimited"}
        }
      }
    },
    "/v1/field": {
      "get": {
        "tags": ["fields"],
        "operationId": "getFieldAtPoint",
        "summary": "Find the field at a latitude/longitude and return its crop history",
        "parameters": [
          {"name": "lat", "in": "query", "required": true, "schema": {"type": "number"}, "example": 43.25483},
          {"name": "lng", "in": "query", "required": true, "schema": {"type": "number"}, "example": -93.62481},
          {"name": "country", "in": "query", "required": false, "description": "Force a country instead of detecting it from the coordinates.", "schema": {"type": "string", "enum": ["us", "ca", "ar", "br"]}}
        ],
        "responses": {
          "200": {"description": "Field history", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/FieldHistory"}}}},
          "400": {"$ref": "#/components/responses/Error"},
          "401": {"$ref": "#/components/responses/Unauthorized"},
          "404": {"description": "No field at this location", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}},
          "429": {"$ref": "#/components/responses/RateLimited"}
        }
      }
    },
    "/v1/fields": {
      "post": {
        "tags": ["fields"],
        "operationId": "getFieldsBatch",
        "summary": "Crop history for many fields (Pro: up to 100 per request, Enterprise: up to 1,000)",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["field_ids"], "properties": {"field_ids": {"type": "array", "items": {"type": "string"}, "example": ["5005.6V73", "5005.76GT"]}}}}}},
        "responses": {
          "200": {"description": "One entry per requested field", "content": {"application/json": {"schema": {"type": "object", "properties": {"results": {"type": "array", "items": {"type": "object", "properties": {"field_id": {"type": "string"}, "history": {"type": "array", "items": {"$ref": "#/components/schemas/YearEntry"}}}}}}}}}},
          "400": {"$ref": "#/components/responses/Error"},
          "401": {"$ref": "#/components/responses/Unauthorized"},
          "403": {"description": "Tier does not include batch lookups", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}
        }
      }
    },
    "/v1/stats": {
      "get": {
        "tags": ["areas"],
        "operationId": "getAreaStats",
        "summary": "Crop area breakdown for a bounding box or a US state",
        "description": "Works without a key at 25 requests/day per IP; with a key it counts toward the key's daily limit.",
        "security": [{}, {"bearerAuth": []}],
        "parameters": [
          {"name": "bbox", "in": "query", "required": false, "description": "west,south,east,north in degrees", "schema": {"type": "string"}, "example": "-93.7,42.0,-93.5,42.1"},
          {"name": "state", "in": "query", "required": false, "description": "2-letter US state code; use instead of bbox", "schema": {"type": "string"}, "example": "IA"},
          {"name": "year", "in": "query", "required": false, "schema": {"type": "integer", "default": 2024}}
        ],
        "responses": {
          "200": {"description": "Crop areas", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/AreaStats"}}}},
          "400": {"$ref": "#/components/responses/Error"},
          "429": {"$ref": "#/components/responses/RateLimited"}
        }
      }
    },
    "/v1/forecast/{field_id}": {
      "get": {
        "tags": ["fields"],
        "operationId": "getForecast",
        "summary": "2026 preseason crop forecast for one US field",
        "security": [{}],
        "parameters": [{"name": "field_id", "in": "path", "required": true, "schema": {"type": "string"}, "example": "5005.6V73"}],
        "responses": {
          "200": {"description": "Forecast", "content": {"application/json": {"schema": {"type": "object", "properties": {"field_id": {"type": "string"}, "forecast": {"$ref": "#/components/schemas/ForecastEntry"}}}}}},
          "404": {"$ref": "#/components/responses/Error"}
        }
      }
    },
    "/v1/analyze": {
      "post": {
        "tags": ["areas"],
        "operationId": "analyzePolygon",
        "summary": "Crop history inside a GeoJSON polygon (Enterprise)",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["geojson"], "properties": {"geojson": {"type": "object", "description": "GeoJSON Polygon or Feature"}, "years": {"type": "array", "items": {"type": "integer"}}}}}}},
        "responses": {
          "200": {"description": "Per-year crop composition", "content": {"application/json": {"schema": {"type": "object", "properties": {"history": {"type": "array", "items": {"type": "object"}}}}}}},
          "400": {"$ref": "#/components/responses/Error"},
          "401": {"$ref": "#/components/responses/Unauthorized"},
          "403": {"description": "Tier does not include polygon analysis", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}
        }
      }
    },
    "/v1/export": {
      "get": {
        "tags": ["areas"],
        "operationId": "exportState",
        "summary": "Download link for a US state-year field file (Enterprise)",
        "parameters": [
          {"name": "state", "in": "query", "required": true, "schema": {"type": "string"}, "example": "IA"},
          {"name": "year", "in": "query", "required": true, "schema": {"type": "integer", "minimum": 2008, "maximum": 2025}},
          {"name": "format", "in": "query", "required": false, "schema": {"type": "string", "enum": ["parquet", "csv"], "default": "parquet"}}
        ],
        "responses": {
          "200": {"description": "Signed download URL, valid for one hour", "content": {"application/json": {"schema": {"type": "object", "properties": {"url": {"type": "string"}, "expires_in": {"type": "integer"}, "format": {"type": "string"}}}}}},
          "401": {"$ref": "#/components/responses/Unauthorized"},
          "403": {"description": "Tier does not include exports", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}},
          "404": {"$ref": "#/components/responses/Error"}
        }
      }
    },
    "/v1/signup": {
      "post": {
        "tags": ["account"],
        "operationId": "signup",
        "summary": "Get a free API key (100 lookups/day), sent by email",
        "security": [{}],
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["email"], "properties": {"email": {"type": "string", "format": "email"}}}}}},
        "responses": {
          "200": {"description": "Key sent", "content": {"application/json": {"schema": {"type": "object", "properties": {"message": {"type": "string"}, "tier": {"type": "string"}, "daily_limit": {"type": "integer"}}}}}},
          "400": {"$ref": "#/components/responses/Error"},
          "409": {"description": "A key already exists for this email", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}
        }
      }
    },
    "/v1/usage": {
      "get": {
        "tags": ["account"],
        "operationId": "getUsage",
        "summary": "Tier and today's usage of the calling key",
        "responses": {
          "200": {"description": "Usage", "content": {"application/json": {"schema": {"type": "object", "properties": {"tier": {"type": "string"}, "email": {"type": "string"}, "daily_usage": {"type": "integer"}, "daily_limit": {"type": ["integer", "null"]}, "daily_remaining": {"type": ["integer", "null"]}, "created_at": {"type": "string"}}}}}},
          "401": {"$ref": "#/components/responses/Unauthorized"}
        }
      }
    },
    "/v1/usage/history": {
      "get": {
        "tags": ["account"],
        "operationId": "getUsageHistory",
        "summary": "Call counts for the key's last 7 active days",
        "responses": {
          "200": {"description": "History", "content": {"application/json": {"schema": {"type": "object", "properties": {"history": {"type": "array", "items": {"type": "object", "properties": {"date": {"type": "string"}, "count": {"type": "integer"}}}}, "tier": {"type": "string"}}}}}},
          "401": {"$ref": "#/components/responses/Unauthorized"}
        }
      }
    },
    "/v1/key/rotate": {
      "post": {
        "tags": ["account"],
        "operationId": "rotateKey",
        "summary": "Replace the calling key with a new one; the old key stops working",
        "responses": {
          "200": {"description": "New key", "content": {"application/json": {"schema": {"type": "object", "properties": {"new_api_key": {"type": "string"}, "tier": {"type": "string"}, "message": {"type": "string"}}}}}},
          "401": {"$ref": "#/components/responses/Unauthorized"}
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {"type": "http", "scheme": "bearer", "description": "API key from POST /v1/signup or https://api.crophist.com/ (header: Authorization: Bearer <api_key>)"}
    },
    "schemas": {
      "FieldHistory": {
        "type": "object",
        "properties": {
          "field_id": {"type": "string", "example": "5005.6V73"},
          "country": {"type": "string", "enum": ["us", "ca", "ar", "br"]},
          "history": {"type": "array", "items": {"$ref": "#/components/schemas/YearEntry"}}
        }
      },
      "YearEntry": {
        "description": "One observed year (from the national crop map), or for US fields one 2026 forecast entry.",
        "oneOf": [{"$ref": "#/components/schemas/ObservedYear"}, {"$ref": "#/components/schemas/ForecastEntry"}]
      },
      "ObservedYear": {
        "type": "object",
        "properties": {
          "year": {"type": "integer", "example": 2024},
          "crop_code": {"type": "integer", "description": "Class code of the country's national legend (US: CDL codes)", "example": 1},
          "crop_name": {"type": "string", "example": "Corn"},
          "color": {"type": "string", "example": "#f2c94c"},
          "fraction": {"type": "number", "description": "Share of the field's pixels in this class (US); 1.0 for centroid-sampled countries"},
          "confidence": {"type": "integer", "minimum": 1, "maximum": 100, "description": "USDA CDL confidence at the field centre, US 2017-2023 only"}
        }
      },
      "ForecastEntry": {
        "type": "object",
        "properties": {
          "year": {"type": "integer", "example": 2026},
          "forecast": {"type": "boolean", "const": true},
          "horizon": {"type": "string", "example": "march"},
          "predicted_class_name": {"type": "string", "example": "corn"},
          "confidence": {"type": "number", "description": "Probability of the predicted class (0-1)"},
          "crops": {"type": "array", "items": {"type": "object", "properties": {"code": {"type": ["integer", "null"]}, "fraction": {"type": "number"}}}},
          "model_version": {"type": "string"}
        }
      },
      "AreaStats": {
        "type": "object",
        "properties": {
          "year": {"type": "integer"},
          "total_ha": {"type": "number"},
          "pixel_count": {"type": "integer"},
          "crops": {"type": "array", "items": {"type": "object", "properties": {"crop_code": {"type": "integer"}, "crop_name": {"type": "string"}, "color": {"type": "string"}, "area_ha": {"type": "number"}, "fraction": {"type": "number"}, "pixel_count": {"type": "integer"}}}}
        }
      },
      "Error": {"type": "object", "properties": {"error": {"type": "string"}}}
    },
    "responses": {
      "Error": {"description": "Error", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}},
      "Unauthorized": {"description": "Missing or invalid API key", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}},
      "RateLimited": {"description": "Daily limit reached", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}
    }
  }
}
