{
  "openapi": "3.1.0",
  "info": {
    "title": "Tarima.Cool Cold-Chain Visibility & Produce Brokerage API",
    "version": "1.1.0",
    "summary": "REST API for cross-border produce load passports, FSMA 204 KDE / EDI 856 ASN records, and carrier ELD / reefer telematics ingestion.",
    "description": "Official Partner Visibility & Cold-Chain API for Tarima.Cool (Pharr TX, Pompano Beach FL, and Vineland NJ border/port cold-chain desks). Links origin packing shed harvest manifests (Mexico, Peru, Guatemala, Ecuador, Brazil) with US border cooler inventory, crop-specific PACA/DRC thermal safety windows, and outbound 53-foot reefer carrier telematics.",
    "contact": {
      "name": "Tarima.Cool Integrations & Cold-Chain Desk",
      "url": "https://tarima.cool/api-docs.html",
      "email": "gerald@tarima.cool"
    }
  },
  "servers": [
    {
      "url": "https://tarima.cool",
      "description": "Production & Partner Evaluation Sandbox Host (routed via /api/v1/*)"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Service readiness and discovery metadata" },
    { "name": "Authentication", "description": "OAuth 2.0 Client Credentials token exchange" },
    { "name": "Shipments", "description": "Straight 22-pallet & 53-foot 3-zone mixed Lot Passports, FSMA 204 KDE, and EDI 856 ASN" },
    { "name": "Telematics", "description": "Inbound ELD GPS, reefer setpoint, return/discharge air, and pulp probe telemetry ingestion" },
    { "name": "AI & GEO Corpus", "description": "Public structured cold-chain specifications and border wait telemetry" }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "Scoped partner API key passed in X-API-Key header for sandbox or production machine-to-machine requests."
      },
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "description": "HS256 Bearer JWT obtained from POST /api/v1/oauth/token (TTL 3600s)."
      },
      "OAuth2ClientCredentials": {
        "type": "oauth2",
        "flows": {
          "clientCredentials": {
            "tokenUrl": "https://tarima.cool/api/v1/oauth/token",
            "scopes": {
              "shipments:read": "Read straight and 3-zone shipment passports, FSMA 204 KDE, and EDI 856 payloads",
              "telematics:read": "Read recent ingested ELD GPS and reefer temperature events",
              "telematics:write": "Post real-time ELD/reefer telematics pings and evaluate crop thermal excursion alerts"
            }
          }
        }
      }
    },
    "schemas": {
      "ErrorResponse": {
        "type": "object",
        "required": ["error", "detail"],
        "properties": {
          "error": { "type": "string", "example": "unauthorized" },
          "detail": { "type": "string", "example": "Provide a valid X-API-Key header or Authorization: Bearer <token> from POST /api/v1/oauth/token" },
          "docs_url": { "type": "string", "format": "uri", "example": "https://tarima.cool/api-docs.html#auth" }
        }
      },
      "OAuthTokenRequest": {
        "type": "object",
        "required": ["grant_type", "client_id", "client_secret"],
        "properties": {
          "grant_type": { "type": "string", "enum": ["client_credentials"] },
          "client_id": { "type": "string", "example": "tarima_sbx_partner" },
          "client_secret": { "type": "string", "example": "<your_client_secret>" }
        }
      },
      "OAuthTokenResponse": {
        "type": "object",
        "properties": {
          "access_token": { "type": "string" },
          "token_type": { "type": "string", "example": "Bearer" },
          "expires_in": { "type": "integer", "example": 3600 },
          "scope": { "type": "string", "example": "shipments:read telematics:read telematics:write" },
          "environment": { "type": "string", "enum": ["sandbox", "production"] }
        }
      },
      "ShipmentZone": {
        "type": "object",
        "properties": {
          "zone_id": { "type": "string", "example": "SINGLE_ZONE_A" },
          "setpoint_f": { "type": "number", "example": 40.5 },
          "arrival_pulp_temp_f": { "type": "number", "example": 40.8 },
          "pallets": { "type": "integer", "example": 22 },
          "cases": { "type": "integer", "example": 1760 },
          "commodity": { "type": "string", "example": "Hass Avocados 48s #1" },
          "gtin14": { "type": "string", "example": "10850049123015" },
          "traceability_lot_code": { "type": "string", "example": "TRM-8910" },
          "pti_voice_pick_code": { "type": "string", "example": "15-47" },
          "ethylene_profile": { "type": "string" }
        }
      },
      "Shipment": {
        "type": "object",
        "properties": {
          "shipment_id": { "type": "string", "example": "TRM-SBX-8910" },
          "environment": { "type": "string", "example": "sandbox" },
          "status": { "type": "string", "enum": ["IN_TRANSIT", "READY_FOR_DISPATCH", "DISPATCHED"] },
          "load_mode": { "type": "string", "enum": ["22_pallet_straight", "53ft_3zone_mixed"] },
          "corridor": { "type": "string", "enum": ["PHARR_TX", "POMPANO_FL", "VINELAND_NJ"] },
          "bol_number": { "type": "string", "example": "BOL-TRM-SBX-8910" },
          "po_number": { "type": "string", "example": "PO-RETAIL-2026-8910" },
          "scac": { "type": "string", "example": "TRMC" },
          "carrier_usdot": { "type": "string", "example": "3914021" },
          "primary_crop_key": {
            "type": "string",
            "enum": ["hass_avocado", "honey_mango", "persian_lime", "md2_pineapple", "jumbo_blueberry"]
          },
          "pallets_total": { "type": "integer", "example": 22 },
          "cases_total": { "type": "integer", "example": 1760 },
          "gross_weight_lb": { "type": "integer", "example": 44800 },
          "edi_856_asn_available": { "type": "boolean" },
          "fsma_204_kde_verified": { "type": "boolean" },
          "zones": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ShipmentZone" }
          }
        }
      },
      "TelematicsEventRequest": {
        "type": "object",
        "required": ["shipment_id"],
        "properties": {
          "shipment_id": { "type": "string", "description": "Tarima shipment ID or BOL number", "example": "TRM-SBX-8910" },
          "event_type": {
            "type": "string",
            "default": "POSITION_TEMP",
            "enum": ["POSITION_TEMP", "REEFER_ALARM", "DOOR_OPEN", "GEOFENCE_ARRIVAL", "DEPARTURE", "ETA_UPDATE"]
          },
          "source_provider": { "type": "string", "example": "project44_movement" },
          "equipment_id": { "type": "string", "example": "REEF-53104" },
          "device_serial": { "type": "string", "example": "COPELAND-GO-TX991" },
          "timestamp_utc": { "type": "string", "format": "date-time", "example": "2026-11-12T18:30:00Z" },
          "latitude": { "type": "number", "minimum": -90, "maximum": 90, "example": 29.4241 },
          "longitude": { "type": "number", "minimum": -180, "maximum": 180, "example": -98.4936 },
          "reefer_setpoint_f": { "type": "number", "example": 40.5 },
          "return_air_temp_f": { "type": "number", "example": 40.9 },
          "discharge_air_temp_f": { "type": "number", "example": 39.8 },
          "pulp_probe_temp_f": { "type": "number", "example": 40.7 },
          "ambient_temp_f": { "type": "number", "example": 78.0 },
          "door_status": { "type": "string", "default": "CLOSED", "example": "CLOSED" }
        }
      },
      "ThermalEvaluation": {
        "type": "object",
        "properties": {
          "status": {
            "type": "string",
            "enum": ["IN_RANGE_GOOD_ARRIVAL", "CHILLING_INJURY_ALERT", "WARM_EXCURSION_ALERT", "NO_TEMP_READING"]
          },
          "rule_crop": { "type": "string", "example": "Hass Avocados" },
          "measured_f": { "type": "number", "example": 40.7 },
          "target_window_f": { "type": "string", "example": "39-43" },
          "excursion_detected": { "type": "boolean" },
          "detail": { "type": "string" }
        }
      },
      "TelematicsEventResponse": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean", "example": true },
          "event_id": { "type": "string" },
          "shipment_id": { "type": "string" },
          "bol_number": { "type": "string" },
          "client_id": { "type": "string" },
          "environment": { "type": "string" },
          "source_provider": { "type": "string" },
          "event_type": { "type": "string" },
          "received_at_utc": { "type": "string", "format": "date-time" },
          "thermal_evaluation": { "$ref": "#/components/schemas/ThermalEvaluation" }
        }
      },
      "OutboundWebhookPayload": {
        "type": "object",
        "description": "Payload format pushed to subscriber webhooks signed with X-Tarima-Signature: t=<unix>,v1=<hmac_sha256>.",
        "properties": {
          "webhook_id": { "type": "string", "example": "wh_evt_20261112_8910" },
          "event_name": {
            "type": "string",
            "enum": ["shipment.crossed_border", "shipment.offer_confirmed", "telemetry.excursion_alert", "edi.856_dispatched"]
          },
          "shipment_id": { "type": "string", "example": "TRM-SBX-8910" },
          "corridor": { "type": "string", "example": "PHARR_TX" },
          "occurred_at_utc": { "type": "string", "format": "date-time" },
          "data": { "type": "object" }
        }
      }
    }
  },
  "paths": {
    "/api/v1/health": {
      "get": {
        "tags": ["Health"],
        "summary": "Public API health check",
        "description": "Returns service version, readiness status, and links to developer docs and OpenAPI specification.",
        "responses": {
          "200": {
            "description": "Service online",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "service": { "type": "string" },
                    "version": { "type": "string" },
                    "status": { "type": "string", "example": "ok" },
                    "docs_url": { "type": "string", "format": "uri" },
                    "openapi_url": { "type": "string", "format": "uri" },
                    "timestamp_utc": { "type": "string", "format": "date-time" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/oauth/token": {
      "post": {
        "tags": ["Authentication"],
        "summary": "Exchange OAuth 2.0 Client Credentials for a Bearer JWT",
        "description": "Accepts grant_type=client_credentials, client_id, and client_secret via JSON body, application/x-www-form-urlencoded, or HTTP Basic Authorization header.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/OAuthTokenRequest" }
            },
            "application/x-www-form-urlencoded": {
              "schema": { "$ref": "#/components/schemas/OAuthTokenRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "HS256 Bearer access token issued (3600s TTL)",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/OAuthTokenResponse" }
              }
            }
          },
          "400": { "description": "Unsupported grant_type or missing credentials" },
          "401": { "description": "Unknown client_id, inactive client, or invalid client_secret" }
        }
      }
    },
    "/api/v1/shipments": {
      "get": {
        "tags": ["Shipments"],
        "summary": "List active cold-chain shipments",
        "description": "Returns straight 22-pallet (default) and 53-foot 3-zone mixed shipments with origin shed GLN, border/port cooler metadata, equipment IDs, and FSMA 204 KDE status.",
        "security": [{ "BearerAuth": [] }, { "ApiKeyAuth": [] }],
        "parameters": [
          {
            "name": "corridor",
            "in": "query",
            "schema": { "type": "string", "enum": ["PHARR_TX", "POMPANO_FL", "VINELAND_NJ"] }
          },
          {
            "name": "load_mode",
            "in": "query",
            "schema": { "type": "string", "enum": ["22_pallet_straight", "53ft_3zone_mixed"] }
          },
          {
            "name": "status",
            "in": "query",
            "schema": { "type": "string", "enum": ["IN_TRANSIT", "READY_FOR_DISPATCH", "DISPATCHED"] }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "minimum": 1, "maximum": 50, "default": 20 }
          }
        ],
        "responses": {
          "200": {
            "description": "List of shipments matching filter criteria",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "environment": { "type": "string" },
                    "authenticated_client": { "type": "string" },
                    "default_load_mode": { "type": "string", "example": "22_pallet_straight" },
                    "count": { "type": "integer" },
                    "shipments": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/Shipment" }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid X-API-Key / Bearer token",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/api/v1/shipments/{shipmentId}": {
      "get": {
        "tags": ["Shipments"],
        "summary": "Retrieve full Lot Passport, FSMA 204 KDE, EDI 856 ASN, and telemetry history",
        "security": [{ "BearerAuth": [] }, { "ApiKeyAuth": [] }],
        "parameters": [
          {
            "name": "shipmentId",
            "in": "path",
            "required": true,
            "description": "Shipment ID (e.g. TRM-SBX-8910) or BOL number (e.g. BOL-TRM-SBX-8910)",
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Detailed shipment record including sample_edi_856_x12 and recent_telematics_events",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Shipment" }
              }
            }
          },
          "401": { "description": "Unauthorized" },
          "404": { "description": "Shipment not found" }
        }
      }
    },
    "/api/v1/telematics/events": {
      "get": {
        "tags": ["Telematics"],
        "summary": "List recent ingested ELD GPS and reefer telematics events",
        "security": [{ "BearerAuth": [] }, { "ApiKeyAuth": [] }],
        "parameters": [
          { "name": "shipment_id", "in": "query", "schema": { "type": "string" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 20 } }
        ],
        "responses": {
          "200": {
            "description": "Recent telemetry pings with crop thermal evaluations",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "environment": { "type": "string" },
                    "count": { "type": "integer" },
                    "events": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/TelematicsEventResponse" }
                    }
                  }
                }
              }
            }
          },
          "401": { "description": "Unauthorized" }
        }
      },
      "post": {
        "tags": ["Telematics"],
        "summary": "Ingest carrier ELD / reefer temperature ping & evaluate PACA thermal safety",
        "description": "Validates telemetry against the shipment's primary crop PACA/DRC window (e.g. Hass Avocado 39-43F, Honey Mango 50-55F, Persian Lime 47-52F), persists the event, and returns immediate thermal_evaluation status.",
        "security": [{ "BearerAuth": [] }, { "ApiKeyAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/TelematicsEventRequest" }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Telematics event persisted and evaluated",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/TelematicsEventResponse" }
              }
            }
          },
          "400": { "description": "Validation error" },
          "401": { "description": "Unauthorized" },
          "404": { "description": "Shipment not found" }
        }
      }
    },
    "/api/ai/index.json": {
      "get": {
        "tags": ["AI & GEO Corpus"],
        "summary": "Discovery index for structured cold-chain JSON feeds",
        "responses": { "200": { "description": "JSON discovery catalog" } }
      }
    },
    "/api/ai/crops.json": {
      "get": {
        "tags": ["AI & GEO Corpus"],
        "summary": "Biological cold-chain & ethylene safety specs across 5 tropical crops",
        "responses": { "200": { "description": "Crop thermal matrix" } }
      }
    },
    "/api/ai/erp-edi-specs.json": {
      "get": {
        "tags": ["AI & GEO Corpus"],
        "summary": "Optional ERP handoffs (Famous, Silo, Produce Pro, EDI 856) and FSMA 204 KDE specs",
        "responses": { "200": { "description": "ERP & EDI 856 specs" } }
      }
    },
    "/api/ai/live-corridors.json": {
      "get": {
        "tags": ["AI & GEO Corpus"],
        "summary": "Live 14-day grove weather telemetry, US CBP border wait times, and USDA AMS quotes",
        "responses": { "200": { "description": "Corridor telemetry JSON" } }
      }
    }
  }
}
