{
  "openapi": "3.1.0",
  "info": {
    "title": "Magbiz ERP API",
    "version": "1.0.0",
    "description": "Your system can create customers, issue and approve documents, and follow payments in Magbiz through an “API user”. Every request goes through the same permissions and rules as the staff screens. Documentation: https://magbizcloud.com/en/developers"
  },
  "servers": [
    {
      "url": "https://erpservice.magbizcloud.com"
    }
  ],
  "security": [
    {
      "apiToken": []
    }
  ],
  "tags": [
    {
      "name": "API user"
    },
    {
      "name": "Customers and master data"
    },
    {
      "name": "Documents"
    },
    {
      "name": "Recurring billing contracts"
    },
    {
      "name": "Customer links"
    },
    {
      "name": "Period close"
    }
  ],
  "paths": {
    "/api_user/me": {
      "post": {
        "operationId": "api_user_me",
        "tags": [
          "API user"
        ],
        "summary": "The API user of this token",
        "description": "Tests the connection · returns the API user, organization and webhook status",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {},
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "user_id": {
                              "type": "string",
                              "description": "API user id"
                            },
                            "code": {
                              "type": "string",
                              "description": "Username"
                            },
                            "role": {
                              "type": "string",
                              "description": "Role"
                            },
                            "org_id": {
                              "type": "string",
                              "description": "Organization id"
                            },
                            "branch": {
                              "type": "string",
                              "description": "Branch"
                            },
                            "warehouse": {
                              "type": "string",
                              "description": "Default warehouse"
                            },
                            "hook_url": {
                              "type": "string",
                              "description": "Webhook URL (empty = not set)"
                            },
                            "hook_last_ok": {
                              "type": "string",
                              "description": "Time of the last successful webhook"
                            },
                            "hook_last_error": {
                              "type": "string",
                              "description": "Last webhook error"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "user_id",
                            "code",
                            "role",
                            "org_id"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/api_user/hook": {
      "post": {
        "operationId": "api_user_hook",
        "tags": [
          "API user"
        ],
        "summary": "Set or remove the webhook",
        "description": "Send url to set it and get a new secret · send it empty to stop",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "url": {
                    "type": "string",
                    "description": "An https URL to a host on the internet · empty = remove"
                  }
                },
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "url": {
                              "type": "string",
                              "description": "The URL set"
                            },
                            "secret": {
                              "type": "string",
                              "description": "The whsec_… signing secret, shown once"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "url"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/customer/update": {
      "post": {
        "operationId": "customer_update",
        "tags": [
          "Customers and master data"
        ],
        "summary": "Create or update a customer",
        "description": "Without id a new customer is created",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "description": "Customer to update"
                  },
                  "client_ref": {
                    "type": "string",
                    "description": "Your reference to avoid duplicates — for new records only (rejected together with id)"
                  },
                  "code": {
                    "type": "string",
                    "description": "Customer code"
                  },
                  "name": {
                    "type": "string",
                    "description": "Customer name"
                  },
                  "contact_type": {
                    "type": "string",
                    "description": "C company · P individual"
                  },
                  "tax_no": {
                    "type": "string",
                    "description": "Tax ID"
                  },
                  "info_bname": {
                    "type": "string",
                    "description": "Billing name"
                  },
                  "info_baddress": {
                    "type": "string",
                    "description": "Billing address"
                  },
                  "info_bphone": {
                    "type": "string",
                    "description": "Billing phone"
                  },
                  "info_bemail": {
                    "type": "string",
                    "description": "Billing email"
                  },
                  "remark": {
                    "type": "string",
                    "description": "Remark"
                  }
                },
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "id": {
                              "type": "string",
                              "description": "Customer id (use as the document contact_id)"
                            },
                            "code": {
                              "type": "string",
                              "description": "Customer code"
                            },
                            "name": {
                              "type": "string",
                              "description": "Customer name"
                            },
                            "replayed": {
                              "type": "string",
                              "description": "Y = this client_ref was sent before; the existing record is returned, nothing new is created"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "id"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/master/list": {
      "post": {
        "operationId": "master_list",
        "tags": [
          "Customers and master data"
        ],
        "summary": "List master data",
        "description": "The organization's master data of one set, e.g. payment_period payment terms",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "master": {
                    "type": "string",
                    "description": "Master data set"
                  },
                  "status": {
                    "type": "string",
                    "description": "A = active only"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "master"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "array",
                          "items": {
                            "type": "object",
                            "properties": {
                              "code": {
                                "type": "string",
                                "description": "Code"
                              },
                              "text": {
                                "type": "string",
                                "description": "Name"
                              },
                              "status": {
                                "type": "string",
                                "description": "Status"
                              }
                            },
                            "additionalProperties": true,
                            "required": [
                              "code",
                              "text"
                            ]
                          }
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/document/update": {
      "post": {
        "operationId": "document_update",
        "tags": [
          "Documents"
        ],
        "summary": "Create or update a document",
        "description": "Without id a new draft (D) is created · fields match the document form on screen; extra fields of each type start with info_ · an owner_id other than the API user can be set after the document is created",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "description": "Document to update"
                  },
                  "client_ref": {
                    "type": "string",
                    "description": "Your reference to avoid duplicates — for new records only (rejected together with id)"
                  },
                  "doc_type": {
                    "type": "string",
                    "description": "Document type, e.g. IN invoice · TI tax invoice · BC billing contract · GJ journal voucher · PV payment voucher"
                  },
                  "doc_date": {
                    "type": "string",
                    "description": "Document date"
                  },
                  "contact_id": {
                    "type": "string",
                    "description": "Customer or vendor"
                  },
                  "owner_id": {
                    "type": "string",
                    "description": "Owner"
                  },
                  "ref_doc_no": {
                    "type": "string",
                    "description": "Reference document number"
                  },
                  "price_option": {
                    "type": "string",
                    "description": "In prices include VAT · Ex prices exclude VAT"
                  },
                  "remark": {
                    "type": "string",
                    "description": "Remark"
                  },
                  "detail": {
                    "type": "array",
                    "description": "Lines: no code name qty unit price · documents that settle others use ref_doc_no amount"
                  }
                },
                "additionalProperties": true,
                "required": [
                  "doc_type",
                  "doc_date"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "id": {
                              "type": "string",
                              "description": "Document id"
                            },
                            "doc_no": {
                              "type": "string",
                              "description": "Document number"
                            },
                            "doc_type": {
                              "type": "string",
                              "description": "Document type"
                            },
                            "status": {
                              "type": "string",
                              "description": "Status (D draft · A approved · C cancelled · some document types have more)"
                            },
                            "net_total": {
                              "type": "number",
                              "description": "Net total calculated by ERP"
                            },
                            "replayed": {
                              "type": "string",
                              "description": "Y = this client_ref was sent before; the existing record is returned, nothing new is created"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "id",
                            "doc_no",
                            "doc_type",
                            "status"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/document/detail": {
      "post": {
        "operationId": "document_detail",
        "tags": [
          "Documents"
        ],
        "summary": "Read a document",
        "description": "The whole document: header, lines (detail), amount paid (paid), credited (cn) and info_ fields",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "description": "Document (<organization id>-<number>)"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "id": {
                              "type": "string",
                              "description": "Document id"
                            },
                            "doc_no": {
                              "type": "string",
                              "description": "Document number"
                            },
                            "doc_type": {
                              "type": "string",
                              "description": "Document type"
                            },
                            "status": {
                              "type": "string",
                              "description": "Status (D draft · A approved · C cancelled · some document types have more)"
                            },
                            "net_total": {
                              "type": "number",
                              "description": "Net total calculated by ERP"
                            },
                            "doc_date": {
                              "type": "string",
                              "description": "Document date"
                            },
                            "contact_id": {
                              "type": "string",
                              "description": "Customer or vendor"
                            },
                            "paid": {
                              "type": "number",
                              "description": "Amount paid"
                            },
                            "cn": {
                              "type": "number",
                              "description": "Amount credited"
                            },
                            "detail": {
                              "type": "array",
                              "description": "Document lines"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "id",
                            "doc_no",
                            "doc_type",
                            "status"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/document/status": {
      "post": {
        "operationId": "document_status",
        "tags": [
          "Documents"
        ],
        "summary": "Approve or cancel a document",
        "description": "The same checks as the on-screen buttons — the role needs that button · cancelling needs a reason",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "description": "Document (<organization id>-<number>)"
                  },
                  "status": {
                    "type": "string",
                    "description": "A approve · C cancel"
                  },
                  "reason": {
                    "type": "string",
                    "description": "Reason (required to cancel)"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "id",
                  "status"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "id": {
                              "type": "string",
                              "description": "Document id"
                            },
                            "doc_no": {
                              "type": "string",
                              "description": "Document number"
                            },
                            "doc_type": {
                              "type": "string",
                              "description": "Document type"
                            },
                            "status": {
                              "type": "string",
                              "description": "Status (D draft · A approved · C cancelled · some document types have more)"
                            },
                            "net_total": {
                              "type": "number",
                              "description": "Net total calculated by ERP"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "id",
                            "doc_no",
                            "doc_type",
                            "status"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/document/payments": {
      "post": {
        "operationId": "document_payments",
        "tags": [
          "Documents"
        ],
        "summary": "Payments and credits of a document",
        "description": "Receipts and credit notes still in effect against this document",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "description": "Document (<organization id>-<number>)"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "doc_no": {
                              "type": "string",
                              "description": "Document number"
                            },
                            "payments": {
                              "type": "array",
                              "description": "Each item: source_doc_no source_doc_type amount wht (withholding tax) wht_cert (certificate received)"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "doc_no",
                            "payments"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/document/defaults": {
      "post": {
        "operationId": "document_defaults",
        "tags": [
          "Documents"
        ],
        "summary": "Defaults for a new document",
        "description": "Values ERP derives from the organization settings, so your system keeps no copy of them",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "doc_type": {
                    "type": "string",
                    "description": "Document type · billing = let ERP pick an invoice or tax invoice from the settings"
                  },
                  "credit_term": {
                    "type": "string",
                    "description": "Credit term (used with billing)"
                  },
                  "note": {
                    "type": "string",
                    "description": "Text appended to the default remark"
                  },
                  "contact_id": {
                    "type": "string",
                    "description": "Customer — returns the bill-to details"
                  },
                  "acc_roles": {
                    "type": "array",
                    "description": "Account roles whose account codes you need"
                  }
                },
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "doc_type": {
                              "type": "string",
                              "description": "Document type"
                            },
                            "remark": {
                              "type": "string",
                              "description": "Default remark"
                            },
                            "bill_to": {
                              "type": "object",
                              "description": "Bill-to info_ fields, ready to put on the document"
                            },
                            "accounts": {
                              "type": "object",
                              "description": "Account codes for the roles asked"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "accounts"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/billing_contract/start": {
      "post": {
        "operationId": "billing_contract_start",
        "tags": [
          "Recurring billing contracts"
        ],
        "summary": "Start a billing contract",
        "description": "If already due, ERP issues the first period's document per the contract settings",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "description": "Document (<organization id>-<number>)"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "id": {
                              "type": "string",
                              "description": "Document id"
                            },
                            "doc_no": {
                              "type": "string",
                              "description": "Document number"
                            },
                            "doc_type": {
                              "type": "string",
                              "description": "Document type"
                            },
                            "status": {
                              "type": "string",
                              "description": "Status (D draft · A approved · C cancelled · some document types have more)"
                            },
                            "net_total": {
                              "type": "number",
                              "description": "Net total calculated by ERP"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "id",
                            "doc_no",
                            "doc_type",
                            "status"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/billing_contract/end": {
      "post": {
        "operationId": "billing_contract_end",
        "tags": [
          "Recurring billing contracts"
        ],
        "summary": "End a contract",
        "description": "Stops billing the next periods",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "description": "Document (<organization id>-<number>)"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "id": {
                              "type": "string",
                              "description": "Document id"
                            },
                            "doc_no": {
                              "type": "string",
                              "description": "Document number"
                            },
                            "doc_type": {
                              "type": "string",
                              "description": "Document type"
                            },
                            "status": {
                              "type": "string",
                              "description": "Status (D draft · A approved · C cancelled · some document types have more)"
                            },
                            "net_total": {
                              "type": "number",
                              "description": "Net total calculated by ERP"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "id",
                            "doc_no",
                            "doc_type",
                            "status"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/billing_contract/cancel": {
      "post": {
        "operationId": "billing_contract_cancel",
        "tags": [
          "Recurring billing contracts"
        ],
        "summary": "Cancel a contract",
        "description": "Allowed while no period document is in effect",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "description": "Document (<organization id>-<number>)"
                  },
                  "reason": {
                    "type": "string",
                    "description": "Reason"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "id": {
                              "type": "string",
                              "description": "Document id"
                            },
                            "doc_no": {
                              "type": "string",
                              "description": "Document number"
                            },
                            "doc_type": {
                              "type": "string",
                              "description": "Document type"
                            },
                            "status": {
                              "type": "string",
                              "description": "Status (D draft · A approved · C cancelled · some document types have more)"
                            },
                            "net_total": {
                              "type": "number",
                              "description": "Net total calculated by ERP"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "id",
                            "doc_no",
                            "doc_type",
                            "status"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/doc_share/create": {
      "post": {
        "operationId": "doc_share_create",
        "tags": [
          "Customer links"
        ],
        "summary": "Create a customer link",
        "description": "A link to view the document and report payment without signing in · the full link is in this response only",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "kind": {
                    "type": "string",
                    "description": "doc"
                  },
                  "id": {
                    "type": "string",
                    "description": "Document (<organization id>-<number>)"
                  },
                  "days": {
                    "type": "string",
                    "description": "Link lifetime 7, 30 or 90 days (default 30)"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "kind",
                  "id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "url": {
                              "type": "string",
                              "description": "Full link"
                            },
                            "links": {
                              "type": "array",
                              "description": "All links of the document: id tail state (active expired revoked) created_time expire_time view_count"
                            },
                            "pay": {
                              "type": "object",
                              "description": "Invoices: outstanding wht to_transfer"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "url",
                            "links"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/doc_share/list": {
      "post": {
        "operationId": "doc_share_list",
        "tags": [
          "Customer links"
        ],
        "summary": "Links of a document",
        "description": "Links created and their state (without the full link)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "kind": {
                    "type": "string",
                    "description": "doc"
                  },
                  "id": {
                    "type": "string",
                    "description": "Document (<organization id>-<number>)"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "kind",
                  "id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "links": {
                              "type": "array",
                              "description": "All links of the document"
                            },
                            "pay": {
                              "type": "object",
                              "description": "Amount the customer has to pay (invoices)"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "links"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/doc_share/revoke": {
      "post": {
        "operationId": "doc_share_revoke",
        "tags": [
          "Customer links"
        ],
        "summary": "Revoke a link",
        "description": "The link stops working immediately",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "share_id": {
                    "type": "string",
                    "description": "The link id from links"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "share_id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "links": {
                              "type": "array",
                              "description": "All links of the document"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "links"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/month_end/expect": {
      "post": {
        "operationId": "month_end_expect",
        "tags": [
          "Period close"
        ],
        "summary": "Report an expected month-end balance",
        "description": "The balance your system expects for one account, shown in the month-end checklist for the accountants to compare · the role must be able to edit the checklist",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "period": {
                    "type": "string",
                    "description": "Month YYYY-MM (not a closed period)"
                  },
                  "account": {
                    "type": "string",
                    "description": "Balance sheet account code"
                  },
                  "amount": {
                    "type": "string",
                    "description": "Expected amount (baht)"
                  },
                  "contacts": {
                    "type": "array",
                    "description": "Per customer: contact_id amount — replaces the previous set"
                  },
                  "note": {
                    "type": "string",
                    "description": "Note (up to 250 characters)"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "period",
                  "account"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "`what` = `ok` with `info`, or `what` = `error` (business errors are returned with HTTP 200)",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "required": [
                        "what",
                        "info"
                      ],
                      "properties": {
                        "what": {
                          "const": "ok"
                        },
                        "info": {
                          "type": "object",
                          "properties": {
                            "period": {
                              "type": "string",
                              "description": "Month"
                            },
                            "account": {
                              "type": "string",
                              "description": "Account code"
                            },
                            "amount": {
                              "type": "string",
                              "description": "Amount saved"
                            }
                          },
                          "additionalProperties": true,
                          "required": [
                            "period",
                            "account",
                            "amount"
                          ]
                        }
                      }
                    },
                    {
                      "$ref": "#/components/schemas/Error"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "webhooks": {
    "document.changed": {
      "post": {
        "summary": "A document of your system changed",
        "description": "Signed with the `X-Magbiz-Signature: t=<unix>,v1=<hex>` header — v1 = HMAC-SHA256(secret, `<t>.<body>`). Read the current state with /document/detail.",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "event": {
                    "const": "document.changed"
                  },
                  "id": {
                    "type": "string"
                  },
                  "org_id": {
                    "type": "string"
                  },
                  "doc_no": {
                    "type": "string"
                  },
                  "doc_type": {
                    "type": "string"
                  },
                  "ext_ref": {
                    "type": "string",
                    "description": "Your client_ref"
                  },
                  "occurred_at": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Received (any 2xx)"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiToken": {
        "type": "http",
        "scheme": "bearer",
        "description": "API user token (mbapi_…) issued at Settings › API users"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "what"
        ],
        "properties": {
          "what": {
            "const": "error"
          },
          "error_code": {
            "type": "string"
          },
          "msg": {
            "type": "string"
          }
        }
      }
    }
  }
}
