{
  "openapi": "3.1.0",
  "info": {
    "title": "MASAR Partner API",
    "version": "1.0.0",
    "description": "Read-only integration API for authorized MASAR transport organizations.",
    "termsOfService": "https://dev.masardz.com/terms.html",
    "contact": {
      "name": "MASAR Developer Support",
      "url": "https://dev.masardz.com/support.html"
    }
  },
  "servers": [
    {
      "url": "https://api.masardz.com/api/v1/partner",
      "description": "Production"
    }
  ],
  "security": [
    {
      "BearerApiKey": []
    }
  ],
  "components": {
    "securitySchemes": {
      "BearerApiKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "MASAR_API_KEY"
      }
    },
    "parameters": {
      "Page": {
        "name": "page",
        "in": "query",
        "schema": {
          "type": "integer",
          "minimum": 1,
          "default": 1
        }
      },
      "PerPage": {
        "name": "per_page",
        "in": "query",
        "schema": {
          "type": "integer",
          "minimum": 1,
          "maximum": 100,
          "default": 25
        }
      }
    },
    "schemas": {
      "Meta": {
        "type": "object",
        "properties": {
          "request_id": {
            "type": "string"
          },
          "scope": {
            "type": [
              "string",
              "null"
            ]
          },
          "rate_limit": {
            "type": "object",
            "properties": {
              "limit": {
                "type": "integer"
              },
              "remaining": {
                "type": "integer"
              },
              "reset_at": {
                "type": "integer"
              }
            }
          }
        }
      },
      "Pagination": {
        "type": "object",
        "properties": {
          "page": {
            "type": "integer"
          },
          "per_page": {
            "type": "integer"
          },
          "total": {
            "type": "integer"
          },
          "total_pages": {
            "type": "integer"
          }
        }
      },
      "Bus": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "plate_number": {
            "type": [
              "string",
              "null"
            ]
          },
          "internal_code": {
            "type": [
              "string",
              "null"
            ]
          },
          "bus_name": {
            "type": [
              "string",
              "null"
            ]
          },
          "seats_count": {
            "type": [
              "integer",
              "null"
            ]
          },
          "brand": {
            "type": [
              "string",
              "null"
            ]
          },
          "model": {
            "type": [
              "string",
              "null"
            ]
          },
          "year": {
            "type": [
              "integer",
              "null"
            ]
          },
          "current_status": {
            "type": [
              "string",
              "null"
            ]
          },
          "status": {
            "type": [
              "string",
              "null"
            ]
          },
          "current_route_id": {
            "type": [
              "integer",
              "null"
            ]
          },
          "last_gps_at": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "Route": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "code": {
            "type": [
              "string",
              "null"
            ]
          },
          "start_name": {
            "type": [
              "string",
              "null"
            ]
          },
          "end_name": {
            "type": [
              "string",
              "null"
            ]
          },
          "distance_estimate": {
            "type": [
              "number",
              "null"
            ]
          },
          "duration_estimate": {
            "type": [
              "integer",
              "null"
            ]
          },
          "status": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "RouteStop": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "route_id": {
            "type": "integer"
          },
          "stop_name": {
            "type": "string"
          },
          "stop_order": {
            "type": "integer"
          },
          "latitude": {
            "type": [
              "number",
              "null"
            ]
          },
          "longitude": {
            "type": [
              "number",
              "null"
            ]
          },
          "is_start": {
            "type": [
              "boolean",
              "integer"
            ]
          },
          "is_end": {
            "type": [
              "boolean",
              "integer"
            ]
          },
          "status": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "Trip": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "route_id": {
            "type": [
              "integer",
              "null"
            ]
          },
          "bus_id": {
            "type": [
              "integer",
              "null"
            ]
          },
          "trip_date": {
            "type": [
              "string",
              "null"
            ]
          },
          "direction": {
            "type": [
              "string",
              "null"
            ]
          },
          "trip_type": {
            "type": [
              "string",
              "null"
            ]
          },
          "service_kind": {
            "type": [
              "string",
              "null"
            ]
          },
          "planned_departure_at": {
            "type": [
              "string",
              "null"
            ]
          },
          "planned_arrival_at": {
            "type": [
              "string",
              "null"
            ]
          },
          "actual_departure_at": {
            "type": [
              "string",
              "null"
            ]
          },
          "actual_arrival_at": {
            "type": [
              "string",
              "null"
            ]
          },
          "status": {
            "type": [
              "string",
              "null"
            ]
          },
          "delay_minutes": {
            "type": [
              "integer",
              "null"
            ]
          },
          "route_name": {
            "type": [
              "string",
              "null"
            ]
          },
          "route_code": {
            "type": [
              "string",
              "null"
            ]
          },
          "plate_number": {
            "type": [
              "string",
              "null"
            ]
          },
          "bus_name": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "BusLocation": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "plate_number": {
            "type": [
              "string",
              "null"
            ]
          },
          "internal_code": {
            "type": [
              "string",
              "null"
            ]
          },
          "bus_name": {
            "type": [
              "string",
              "null"
            ]
          },
          "last_latitude": {
            "type": [
              "number",
              "null"
            ]
          },
          "last_longitude": {
            "type": [
              "number",
              "null"
            ]
          },
          "last_gps_at": {
            "type": [
              "string",
              "null"
            ]
          },
          "last_speed_kmh": {
            "type": [
              "number",
              "null"
            ]
          },
          "last_heading": {
            "type": [
              "number",
              "null"
            ]
          },
          "last_accuracy": {
            "type": [
              "number",
              "null"
            ]
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "const": false
          },
          "code": {
            "type": "string"
          },
          "trace_id": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "data": {
            "type": "null"
          },
          "errors": {
            "type": [
              "object",
              "array"
            ]
          },
          "meta": {
            "type": "object"
          },
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              },
              "retryable": {
                "type": "boolean"
              },
              "correlation_id": {
                "type": "string"
              }
            }
          }
        }
      },
      "StatusResponse": {
        "type": "object",
        "required": [
          "success",
          "code",
          "data",
          "meta"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true
          },
          "code": {
            "type": "string",
            "const": "OK"
          },
          "trace_id": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "data": {
            "type": "object",
            "properties": {
              "plan": {
                "type": "string",
                "enum": [
                  "API Standard",
                  "API Business"
                ]
              },
              "level": {
                "type": "string",
                "enum": [
                  "standard",
                  "business"
                ]
              },
              "scopes": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "rate_limit_per_minute": {
                "type": "integer"
              }
            }
          },
          "errors": {
            "type": "array"
          },
          "meta": {
            "$ref": "#/components/schemas/Meta"
          }
        }
      },
      "BusesResponse": {
        "type": "object",
        "required": [
          "success",
          "code",
          "data",
          "meta"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true
          },
          "code": {
            "type": "string",
            "const": "OK"
          },
          "trace_id": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "data": {
            "type": "object",
            "properties": {
              "items": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Bus"
                }
              },
              "pagination": {
                "$ref": "#/components/schemas/Pagination"
              }
            }
          },
          "errors": {
            "type": "array"
          },
          "meta": {
            "$ref": "#/components/schemas/Meta"
          }
        }
      },
      "RoutesResponse": {
        "type": "object",
        "required": [
          "success",
          "code",
          "data",
          "meta"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true
          },
          "code": {
            "type": "string",
            "const": "OK"
          },
          "trace_id": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "data": {
            "type": "object",
            "properties": {
              "items": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Route"
                }
              },
              "pagination": {
                "$ref": "#/components/schemas/Pagination"
              }
            }
          },
          "errors": {
            "type": "array"
          },
          "meta": {
            "$ref": "#/components/schemas/Meta"
          }
        }
      },
      "RouteStopsResponse": {
        "type": "object",
        "required": [
          "success",
          "code",
          "data",
          "meta"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true
          },
          "code": {
            "type": "string",
            "const": "OK"
          },
          "trace_id": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "data": {
            "type": "object",
            "properties": {
              "route": {
                "type": "object"
              },
              "items": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/RouteStop"
                }
              }
            }
          },
          "errors": {
            "type": "array"
          },
          "meta": {
            "$ref": "#/components/schemas/Meta"
          }
        }
      },
      "TripsResponse": {
        "type": "object",
        "required": [
          "success",
          "code",
          "data",
          "meta"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true
          },
          "code": {
            "type": "string",
            "const": "OK"
          },
          "trace_id": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "data": {
            "type": "object",
            "properties": {
              "items": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Trip"
                }
              },
              "pagination": {
                "$ref": "#/components/schemas/Pagination"
              }
            }
          },
          "errors": {
            "type": "array"
          },
          "meta": {
            "$ref": "#/components/schemas/Meta"
          }
        }
      },
      "TripResponse": {
        "type": "object",
        "required": [
          "success",
          "code",
          "data",
          "meta"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true
          },
          "code": {
            "type": "string",
            "const": "OK"
          },
          "trace_id": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "data": {
            "$ref": "#/components/schemas/Trip"
          },
          "errors": {
            "type": "array"
          },
          "meta": {
            "$ref": "#/components/schemas/Meta"
          }
        }
      },
      "BusLocationResponse": {
        "type": "object",
        "required": [
          "success",
          "code",
          "data",
          "meta"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true
          },
          "code": {
            "type": "string",
            "const": "OK"
          },
          "trace_id": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "data": {
            "$ref": "#/components/schemas/BusLocation"
          },
          "errors": {
            "type": "array"
          },
          "meta": {
            "$ref": "#/components/schemas/Meta"
          }
        }
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Invalid or missing API key",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "Forbidden": {
        "description": "Plan or scope denied",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "NotFound": {
        "description": "Resource not found",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "RateLimited": {
        "description": "Rate limit exceeded",
        "headers": {
          "Retry-After": {
            "schema": {
              "type": "integer"
            }
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      }
    }
  },
  "paths": {
    "/status": {
      "get": {
        "tags": [
          "Integration"
        ],
        "summary": "Integration status",
        "parameters": [],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StatusResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/buses": {
      "get": {
        "tags": [
          "Fleet"
        ],
        "summary": "List buses",
        "parameters": [
          {
            "$ref": "#/components/parameters/Page"
          },
          {
            "$ref": "#/components/parameters/PerPage"
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BusesResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        },
        "description": "Required scope: `buses:read`."
      }
    },
    "/routes": {
      "get": {
        "tags": [
          "Network"
        ],
        "summary": "List routes",
        "parameters": [
          {
            "$ref": "#/components/parameters/Page"
          },
          {
            "$ref": "#/components/parameters/PerPage"
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RoutesResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        },
        "description": "Required scope: `routes:read`."
      }
    },
    "/routes/{id}/stops": {
      "get": {
        "tags": [
          "Network"
        ],
        "summary": "Route stops",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "minimum": 1
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RouteStopsResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        },
        "description": "Required scope: `stops:read`."
      }
    },
    "/trips/active": {
      "get": {
        "tags": [
          "Trips"
        ],
        "summary": "Active trips",
        "parameters": [
          {
            "$ref": "#/components/parameters/Page"
          },
          {
            "$ref": "#/components/parameters/PerPage"
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TripsResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        },
        "description": "Required scope: `trips:read`."
      }
    },
    "/trips/{id}": {
      "get": {
        "tags": [
          "Trips"
        ],
        "summary": "Trip detail",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "minimum": 1
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TripResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        },
        "description": "Required scope: `trips:read`."
      }
    },
    "/buses/{id}/location": {
      "get": {
        "tags": [
          "Fleet"
        ],
        "summary": "Last known bus location",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "minimum": 1
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BusLocationResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        },
        "description": "Required scope: `gps:read`."
      }
    }
  },
  "x-masar-scopes": {
    "buses:read": "Read buses",
    "routes:read": "Read routes",
    "stops:read": "Read route stops",
    "trips:read": "Read trips",
    "gps:read": "Read last known bus location"
  }
}