{
  "openapi": "3.0.3",
  "info": {
    "title": "Massar Brand Integration API",
    "version": "1.0.0",
    "description": "Server-to-server API for Egyptian brand accounts. Keys are created by a brand administrator in Applications \u2192 API. All current keys have the full supported integration surface within one brand; selectable read-only scopes are not implemented. Production only; no public sandbox, order cancellation/update, pickup booking, stock API, or label PDF endpoint is promised here. See the guide for webhooks and quick starts.",
    "contact": {
      "email": "contact@massar-eg.co",
      "url": "https://massar-eg.co/integration/api"
    }
  },
  "servers": [
    {
      "url": "https://torok-api.onrender.com/api/integrations/v1",
      "description": "Production \u2014 real writes"
    }
  ],
  "security": [
    {
      "BrandApiKey": []
    }
  ],
  "paths": {
    "/cities": {
      "get": {
        "operationId": "listCities",
        "summary": "Find delivery cities",
        "description": "Search English city, Arabic city or English governorate name. Returns at most 1000 serviceable cities. No pagination parameters.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Cities"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request; error contains field paths when validation fails.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Brand is inactive.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "120 requests per minute per key exceeded. Honor Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Temporary database interruption; honor Retry-After and retry safely.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            },
            "example": "Nasr"
          }
        ]
      }
    },
    "/quote": {
      "post": {
        "operationId": "getQuote",
        "summary": "Calculate price and collection amount",
        "description": "No shipment is created. COD total = productCodAmount + deliveryCost. PREPAID total = 0. Prices follow your brand agreement; never hardcode shipping fees.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Quote"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request; error contains field paths when validation fails.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Brand is inactive.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "120 requests per minute per key exceeded. Honor Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Temporary database interruption; honor Retry-After and retry safely.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/QuoteRequest"
              }
            }
          }
        }
      }
    },
    "/orders": {
      "post": {
        "operationId": "createOrder",
        "summary": "Create a shipment",
        "description": "PRODUCTION WRITE: creates a real shipment record. Identical normalized payload and externalOrderId returns the existing order. Changed payload with same ID returns 409. No Idempotency-Key header is used. After a timeout, look up or retry the exact same payload; never mint a new externalOrderId to retry.",
        "responses": {
          "400": {
            "description": "Invalid request; error contains field paths when validation fails.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Brand is inactive.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "120 requests per minute per key exceeded. Honor Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Temporary database interruption; honor Retry-After and retry safely.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "201": {
            "description": "Created; created=true",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreateOrderResult"
                }
              }
            }
          },
          "200": {
            "description": "Idempotent repeat; created=false",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreateOrderResult"
                }
              }
            }
          },
          "409": {
            "description": "External ID already exists with different data.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateOrder"
              },
              "example": {
                "externalOrderId": "YOUR-ORDER-1001",
                "receiverName": "Example customer",
                "receiverAddress": "Building, street, floor and apartment",
                "cityId": "REPLACE_WITH_CITY_ID",
                "receiverMobile1": "REPLACE_WITH_CUSTOMER_PHONE",
                "productCodAmount": 1000,
                "paymentMethod": "COD",
                "items": [
                  {
                    "itemName": "Cotton shirt",
                    "sku": "SHIRT-M",
                    "quantity": 1
                  }
                ]
              }
            }
          }
        }
      }
    },
    "/orders/{externalOrderId}": {
      "get": {
        "operationId": "getOrder",
        "summary": "Look up an order by your external ID",
        "description": "Brand-scoped lookup using your original externalOrderId, not the Massar waybill. URL-encode the path value.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Order"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request; error contains field paths when validation fails.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Brand is inactive.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "120 requests per minute per key exceeded. Honor Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Temporary database interruption; honor Retry-After and retry safely.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Order not found for this brand.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "externalOrderId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "maxLength": 120
            },
            "example": "YOUR-ORDER-1001"
          }
        ]
      }
    },
    "/webhook": {
      "get": {
        "operationId": "getWebhook",
        "summary": "Read your webhook subscription",
        "description": "Returns null if no subscription exists; otherwise includes active=false for a disabled subscription.",
        "responses": {
          "400": {
            "description": "Invalid request; error contains field paths when validation fails.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Brand is inactive.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "120 requests per minute per key exceeded. Honor Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Temporary database interruption; honor Retry-After and retry safely.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "200": {
            "description": "Subscription or null",
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    {
                      "$ref": "#/components/schemas/WebhookConfig"
                    }
                  ],
                  "nullable": true
                }
              }
            }
          }
        }
      },
      "put": {
        "operationId": "setWebhook",
        "summary": "Register or rotate a webhook",
        "description": "One endpoint per brand. Every PUT rotates the signing secret, including the same URL. Use public HTTPS port 443; private/loopback hosts, credentials, redirects and non-public DNS are refused. Secret is returned once.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebhookRegistration"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request; error contains field paths when validation fails.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Brand is inactive.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "120 requests per minute per key exceeded. Honor Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Temporary database interruption; honor Retry-After and retry safely.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "url": {
                    "type": "string",
                    "format": "uri",
                    "maxLength": 1000
                  }
                },
                "required": [
                  "url"
                ],
                "additionalProperties": false
              },
              "example": {
                "url": "https://your-store.example/api/massar/webhook"
              }
            }
          }
        }
      },
      "delete": {
        "operationId": "deleteWebhook",
        "summary": "Disable webhook delivery",
        "description": "",
        "responses": {
          "400": {
            "description": "Invalid request; error contains field paths when validation fails.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Brand is inactive.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "120 requests per minute per key exceeded. Honor Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Temporary database interruption; honor Retry-After and retry safely.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "204": {
            "description": "Subscription disabled; empty response body."
          }
        }
      }
    },
    "/webhook/test": {
      "post": {
        "operationId": "testWebhook",
        "summary": "Send a signed test event",
        "description": "Sends webhook.test to your configured receiver (external side effect). HTTP 502 means delivery failed; inspect responseStatus and error. The test can be retried by the delivery worker.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebhookTestResult"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request; error contains field paths when validation fails.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Brand is inactive.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "120 requests per minute per key exceeded. Honor Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Temporary database interruption; honor Retry-After and retry safely.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "No active webhook.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Receiver test failed.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebhookTestResult"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BrandApiKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "masar_live_\u2026",
        "description": "Use Authorization: Bearer <key>. Never place in browser code."
      }
    },
    "schemas": {
      "CreateOrder": {
        "type": "object",
        "properties": {
          "externalOrderId": {
            "type": "string",
            "minLength": 1,
            "maxLength": 120,
            "description": "Stable ID from your store. Reuse for retries; unique within your brand."
          },
          "receiverName": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160
          },
          "receiverAddress": {
            "type": "string",
            "minLength": 1,
            "maxLength": 500
          },
          "cityId": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64,
            "description": "ID returned by GET /cities, not a city name."
          },
          "receiverMobile1": {
            "type": "string",
            "minLength": 6,
            "maxLength": 32
          },
          "receiverMobile2": {
            "type": "string",
            "maxLength": 32
          },
          "receiverEmail": {
            "type": "string",
            "format": "email",
            "maxLength": 160
          },
          "productCodAmount": {
            "type": "number",
            "minimum": 0,
            "maximum": 30000,
            "description": "Product value in EGP excluding delivery fees. COD total including fees must also be <= 30000."
          },
          "paymentMethod": {
            "type": "string",
            "enum": [
              "COD",
              "PREPAID"
            ],
            "default": "COD"
          },
          "customerNote": {
            "type": "string",
            "maxLength": 1000
          },
          "weight": {
            "type": "number",
            "minimum": 0,
            "maximum": 20,
            "description": "Weight in kg."
          },
          "openPackage": {
            "type": "boolean",
            "default": false
          },
          "insurancePlan": {
            "type": "string",
            "enum": [
              "NONE",
              "BASIC",
              "SAFETY",
              "SECURED"
            ],
            "default": "NONE"
          },
          "items": {
            "type": "array",
            "minItems": 1,
            "maxItems": 20,
            "items": {
              "type": "object",
              "properties": {
                "itemName": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 200
                },
                "sku": {
                  "type": "string",
                  "maxLength": 128
                },
                "quantity": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 999
                }
              },
              "required": [
                "itemName",
                "quantity"
              ],
              "additionalProperties": false
            }
          }
        },
        "required": [
          "externalOrderId",
          "receiverName",
          "receiverAddress",
          "cityId",
          "receiverMobile1",
          "productCodAmount",
          "items"
        ],
        "additionalProperties": false
      },
      "QuoteRequest": {
        "type": "object",
        "properties": {
          "cityId": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64,
            "description": "ID returned by GET /cities, not a city name."
          },
          "productCodAmount": {
            "type": "number",
            "minimum": 0,
            "maximum": 30000,
            "description": "Product value in EGP excluding delivery fees. COD total including fees must also be <= 30000."
          },
          "paymentMethod": {
            "type": "string",
            "enum": [
              "COD",
              "PREPAID"
            ],
            "default": "COD"
          },
          "weight": {
            "type": "number",
            "minimum": 0,
            "maximum": 20,
            "description": "Weight in kg."
          },
          "openPackage": {
            "type": "boolean",
            "default": false
          },
          "insurancePlan": {
            "type": "string",
            "enum": [
              "NONE",
              "BASIC",
              "SAFETY",
              "SECURED"
            ],
            "default": "NONE"
          }
        },
        "required": [
          "cityId"
        ],
        "additionalProperties": false
      },
      "Order": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "externalOrderId": {
            "type": "string"
          },
          "waybillNo": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "description": "Current status label; store unchanged. New labels may be introduced."
          },
          "productCodAmount": {
            "type": "number"
          },
          "deliveryCost": {
            "type": "number"
          },
          "totalCodAmount": {
            "type": "number"
          },
          "paymentMethod": {
            "type": "string",
            "enum": [
              "COD",
              "PREPAID"
            ]
          },
          "trackingUrl": {
            "type": "string",
            "format": "uri"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "CreateOrderResult": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Order"
          },
          {
            "type": "object",
            "properties": {
              "created": {
                "type": "boolean"
              }
            },
            "required": [
              "created"
            ]
          }
        ]
      },
      "Quote": {
        "type": "object",
        "properties": {
          "cityId": {
            "type": "string"
          },
          "deliveryZone": {
            "type": "integer"
          },
          "deliveryCost": {
            "type": "number"
          },
          "openPackage": {
            "type": "boolean"
          },
          "openPackageFee": {
            "type": "number"
          },
          "insuranceFee": {
            "type": "number"
          },
          "productCodAmount": {
            "type": "number"
          },
          "totalCodAmount": {
            "type": "number"
          },
          "currency": {
            "type": "string",
            "enum": [
              "EGP"
            ]
          }
        }
      },
      "City": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "nameAr": {
            "type": "string",
            "nullable": true
          },
          "governorate": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              },
              "nameAr": {
                "type": "string",
                "nullable": true
              }
            }
          },
          "deliveryZone": {
            "type": "integer"
          }
        }
      },
      "Cities": {
        "type": "object",
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/City"
            }
          }
        },
        "required": [
          "data"
        ]
      },
      "WebhookConfig": {
        "type": "object",
        "properties": {
          "url": {
            "type": "string",
            "format": "uri"
          },
          "active": {
            "type": "boolean"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "WebhookRegistration": {
        "type": "object",
        "properties": {
          "url": {
            "type": "string",
            "format": "uri"
          },
          "active": {
            "type": "boolean"
          },
          "signingSecret": {
            "type": "string",
            "description": "Shown once. Every PUT rotates it. Store separately from the API key."
          },
          "message": {
            "type": "string"
          }
        }
      },
      "WebhookTestResult": {
        "type": "object",
        "properties": {
          "delivered": {
            "type": "boolean"
          },
          "status": {
            "type": "string"
          },
          "responseStatus": {
            "type": "integer",
            "nullable": true
          },
          "error": {
            "type": "string",
            "nullable": true
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        },
        "required": [
          "error"
        ]
      },
      "WebhookEvent": {
        "type": "object",
        "properties": {
          "event": {
            "type": "string",
            "enum": [
              "order.status_changed",
              "order.attempt_recorded",
              "webhook.test"
            ]
          },
          "timestamp": {
            "type": "string",
            "format": "date-time"
          },
          "data": {
            "type": "object",
            "description": "Order events include externalOrderId, waybillNo, status, collected, productCodAmount, deliveryCost, totalCodAmount, trackingUrl and updatedAt; attempts may add fields. webhook.test includes message. Allow additional fields."
          }
        },
        "required": [
          "event",
          "timestamp",
          "data"
        ]
      }
    }
  },
  "externalDocs": {
    "url": "https://massar-eg.co/integration/api"
  }
}
