{
  "openapi": "3.1.0",
  "info": {
    "title": "Serva Reseller API",
    "version": "1.0.0",
    "description": "Compatibility API for balance lookup, service listing, order creation, and order status checks. Each API-enabled account uses one active key for integrations."
  },
  "servers": [
    {
      "url": "https://serva-s.com",
      "description": "Production"
    }
  ],
  "paths": {
    "/api/v1": {
      "get": {
        "summary": "Execute a reseller action by query string",
        "security": [
          { "ApiKeyQuery": [] },
          { "ApiKeyHeader": [] },
          { "BearerAuth": [] }
        ],
        "parameters": [
          {
            "name": "action",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "enum": ["balance", "services", "add", "status"]
            }
          },
          {
            "name": "key",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Raw API key. Optional when using x-api-key or Authorization: Bearer."
          },
          {
            "name": "service",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "link",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "target",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "target_link",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "quantity",
            "in": "query",
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "order",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "orders",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Comma-separated batch list."
          }
        ],
        "responses": {
          "200": {
            "description": "Action result",
            "headers": {
              "x-serva-request-id": {
                "description": "Support tracing identifier for the exact API request.",
                "schema": {
                  "type": "string"
                }
              },
              "x-serva-api-version": {
                "description": "Current public API release stamp.",
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    { "$ref": "#/components/schemas/BalanceResponse" },
                    { "$ref": "#/components/schemas/ServicesResponse" },
                    { "$ref": "#/components/schemas/AddResponse" },
                    { "$ref": "#/components/schemas/StatusResponse" },
                    { "$ref": "#/components/schemas/BatchStatusResponse" },
                    { "$ref": "#/components/schemas/ErrorResponse" }
                  ]
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Execute a reseller action by request body",
        "description": "Preferred method. Supports JSON, application/x-www-form-urlencoded, and multipart/form-data.",
        "security": [
          { "ApiKeyQuery": [] },
          { "ApiKeyHeader": [] },
          { "BearerAuth": [] }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ResellerRequest"
              }
            },
            "application/x-www-form-urlencoded": {
              "schema": {
                "$ref": "#/components/schemas/ResellerRequest"
              }
            },
            "multipart/form-data": {
              "schema": {
                "$ref": "#/components/schemas/ResellerRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Action result",
            "headers": {
              "x-serva-request-id": {
                "description": "Support tracing identifier for the exact API request.",
                "schema": {
                  "type": "string"
                }
              },
              "x-serva-api-version": {
                "description": "Current public API release stamp.",
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    { "$ref": "#/components/schemas/BalanceResponse" },
                    { "$ref": "#/components/schemas/ServicesResponse" },
                    { "$ref": "#/components/schemas/AddResponse" },
                    { "$ref": "#/components/schemas/StatusResponse" },
                    { "$ref": "#/components/schemas/BatchStatusResponse" },
                    { "$ref": "#/components/schemas/ErrorResponse" }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyQuery": {
        "type": "apiKey",
        "in": "query",
        "name": "key"
      },
      "ApiKeyHeader": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key"
      },
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "schemas": {
      "ResellerRequest": {
        "type": "object",
        "required": ["action"],
        "properties": {
          "action": {
            "type": "string",
            "enum": ["balance", "services", "add", "status"]
          },
          "key": {
            "type": "string",
            "description": "Raw API key generated from the dashboard. Optional when using x-api-key or Authorization: Bearer."
          },
          "service": {
            "type": "string",
            "description": "API-facing service ID returned by the services action."
          },
          "link": {
            "type": "string"
          },
          "target": {
            "type": "string"
          },
          "target_link": {
            "type": "string"
          },
          "quantity": {
            "type": "integer"
          },
          "order": {
            "type": "string"
          },
          "orders": {
            "type": "string",
            "description": "Comma-separated order numbers for batch status."
          },
          "coupon": {
            "type": "string"
          },
          "coupon_code": {
            "type": "string"
          },
          "fields": {
            "description": "Manual provider fields. Can be an object, an array of field objects, or a JSON-encoded string."
          },
          "inputs": {
            "description": "Alias of fields."
          },
          "custom_fields": {
            "description": "Alias of fields."
          }
        },
        "additionalProperties": true
      },
      "BalanceResponse": {
        "type": "object",
        "properties": {
          "balance": {
            "type": "string"
          },
          "currency": {
            "type": "string",
            "example": "USD"
          }
        }
      },
      "FieldObject": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "required": {
            "type": "boolean"
          },
          "placeholder": {
            "type": "string"
          },
          "provider_key": {
            "type": ["string", "null"]
          },
          "quantity_source": {
            "type": ["string", "null"]
          }
        }
      },
      "ServiceObject": {
        "type": "object",
        "properties": {
          "service": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "name_ar": {
            "type": "string"
          },
          "name_en": {
            "type": "string"
          },
          "category": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "rate": {
            "type": "string"
          },
          "min": {
            "type": "integer"
          },
          "max": {
            "type": "integer"
          },
          "refill": {
            "type": "boolean"
          },
          "cancel": {
            "type": "boolean"
          },
          "pricing_type": {
            "type": "string"
          },
          "has_quantity": {
            "type": "boolean"
          },
          "link_required": {
            "type": "boolean"
          },
          "fields": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/FieldObject"
            }
          }
        }
      },
      "ServicesResponse": {
        "type": "array",
        "items": {
          "$ref": "#/components/schemas/ServiceObject"
        }
      },
      "AddResponse": {
        "type": "object",
        "properties": {
          "order": {
            "type": "string"
          },
          "charge": {
            "type": "string"
          },
          "currency": {
            "type": "string"
          },
          "balance": {
            "type": "string"
          }
        }
      },
      "StatusResponse": {
        "type": "object",
        "properties": {
          "charge": {
            "type": "string"
          },
          "start_count": {
            "type": ["integer", "null"]
          },
          "status": {
            "type": "string"
          },
          "remains": {
            "type": ["integer", "null"]
          },
          "currency": {
            "type": "string"
          }
        }
      },
      "BatchStatusResponse": {
        "type": "object",
        "additionalProperties": {
          "oneOf": [
            { "$ref": "#/components/schemas/StatusResponse" },
            { "$ref": "#/components/schemas/ErrorItem" }
          ]
        }
      },
      "ErrorItem": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "error_code": {
            "type": "string",
            "description": "Machine-readable code such as INVALID_API_KEY or API_ACCESS_NOT_ALLOWED."
          }
        }
      }
    }
  }
}
