{"openapi":"3.1.0","info":{"title":"POST /api/v1/ledgers/apply-coa","version":"1.0.0","description":"Apply AI-generated COA"},"servers":[{"url":"https://api.ondayzero.com","description":"Production"}],"paths":{"/api/v1/ledgers/apply-coa":{"post":{"tags":["ledgers"],"summary":"Apply AI-generated COA","description":"Apply an AI-generated or custom chart of accounts template to the business, upserting over existing ledgers. Protected ledgers (system, required, bank-linked) are skipped. Existing custom ledgers not present in the template are deactivated.","operationId":"apply_coa","parameters":[{"name":"authorization","in":"header","required":false,"schema":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Authorization"}},{"name":"x-business-id","in":"header","required":false,"schema":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"X-Business-Id"}}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/CustomCoaTemplate"}}}},"responses":{"201":{"description":"Successful Response","content":{"application/json":{"schema":{"$ref":"#/components/schemas/SuccessEnvelope_ApplyCoaResponse_"}}}},"400":{"description":"Bad Request - Invalid input","content":{"application/json":{"example":{"error":"validation_error","message":"Validation failed: due_date: Input should be a valid date","errors":{"due_date":"Input should be a valid date"},"code":"GEN_002","request_id":"3f0e7c2a-9b4d-4e1a-8c6f-2d5b7a1e9c30"},"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"401":{"description":"Unauthorized - Missing or expired token, an MCP OAuth token used against the REST API, or no access to this business","content":{"application/json":{"example":{"error":"unauthorized","message":"Authentication required","code":"AUTH_001","request_id":"3f0e7c2a-9b4d-4e1a-8c6f-2d5b7a1e9c30"},"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"403":{"description":"Forbidden - Insufficient permissions","content":{"application/json":{"example":{"error":"forbidden","message":"You don't have permission to access this resource","code":"AUTH_002","request_id":"3f0e7c2a-9b4d-4e1a-8c6f-2d5b7a1e9c30"},"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"422":{"description":"Validation Error","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"},"example":{"error":"validation_error","message":"Validation failed: amount: Input should be a valid integer","errors":{"amount":"Input should be a valid integer"},"code":"GEN_002","request_id":"3f0e7c2a-9b4d-4e1a-8c6f-2d5b7a1e9c30"}}}}},"x-fastapi-operation-id":"apply_coa_api_v1_ledgers_apply_coa_post"}}},"components":{"schemas":{"ApplyCoaFailure":{"properties":{"row":{"anyOf":[{"type":"integer"},{"type":"null"}],"title":"Row","description":"1-based position in the submitted template, if known."},"name":{"type":"string","title":"Name","description":"Account name from the row, for identification."},"sort_code":{"anyOf":[{"type":"integer"},{"type":"null"}],"title":"Sort Code","description":"Account number from the row."},"reason":{"type":"string","title":"Reason","description":"User-facing explanation of the rejection."}},"type":"object","required":["name","reason"],"title":"ApplyCoaFailure","description":"One template row that could not be applied, and why."},"ApplyCoaResponse":{"properties":{"created":{"type":"integer","title":"Created","description":"Number of new ledgers created.","default":0},"updated":{"type":"integer","title":"Updated","description":"Number of existing ledgers updated.","default":0},"skipped":{"type":"integer","title":"Skipped","description":"Number of protected ledgers skipped (system, bank-linked).","default":0},"deactivated":{"type":"integer","title":"Deactivated","description":"Number of old custom ledgers deactivated (not in template).","default":0},"skipped_names":{"items":{"type":"string"},"type":"array","title":"Skipped Names","description":"Names of protected ledgers that were skipped."},"deactivated_names":{"items":{"type":"string"},"type":"array","title":"Deactivated Names","description":"Names of custom ledgers deactivated because their sort_code was absent from the new template."},"failed":{"items":{"$ref":"#/components/schemas/ApplyCoaFailure"},"type":"array","title":"Failed","description":"Rows that could not be applied, each with the reason. The rest of the template is still applied — a bad row no longer fails the whole request."},"total":{"type":"integer","title":"Total","description":"Total ledgers in the submitted template.","default":0}},"type":"object","title":"ApplyCoaResponse","description":"Response from applying an AI-generated or custom COA template.\n\nProvides counts and details for each action taken: created (new ledgers),\nupdated (existing unlocked ledgers whose sort_code matched), skipped\n(protected system/bank-connected ledgers), and deactivated (existing\ncustom ledgers whose sort_code was not in the new template)."},"CustomCoaLedger":{"properties":{"name":{"type":"string","minLength":1,"title":"Name","description":"Ledger name"},"type":{"type":"string","title":"Type","description":"Ledger type: asset, liability, equity, revenue, expense"},"sub_type":{"type":"string","title":"Sub Type","description":"Ledger sub-type: current_assets, non-current_assets, transfers_between_accounts, uncategorized_assets, current_liabilities, non-current_liabilities, equity, operating_revenues, other_income, cost_of_goods_sold, operating_expenses, other_expenses"},"sort_code":{"type":"string","maxLength":20,"title":"Sort Code","description":"Numeric sort code for ordering (digits only, max 20 chars)"},"debit_credit":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Debit Credit","description":"Debit/credit behavior: debit, credit. Optional - determined from the ledger type."},"report_cash_flow":{"anyOf":[{"type":"boolean"},{"type":"null"}],"title":"Report Cash Flow","description":"Include in cash flow reports. Optional."},"editable":{"anyOf":[{"type":"boolean"},{"type":"null"}],"title":"Editable","description":"Whether line entries can be manually added or removed","default":true},"description":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Description","description":"Ledger description"},"financial_account_type":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Financial Account Type","description":"Financial account type for Templated Mode. Options: bank_account, credit_card, payments, payroll, loan, prepaid_card, accounts_receivable, accounts_payable"}},"type":"object","required":["name","type","sub_type","sort_code"],"title":"CustomCoaLedger","description":"Schema for a ledger definition in a custom COA template.\n\nOnly name, type, sub_type, and sort_code are required. debit_credit is\ndetermined from the ledger type."},"CustomCoaTemplate":{"properties":{"ledgers":{"items":{"$ref":"#/components/schemas/CustomCoaLedger"},"type":"array","minItems":1,"title":"Ledgers","description":"List of ledger definitions for the custom COA"}},"type":"object","required":["ledgers"],"title":"CustomCoaTemplate","description":"Schema for a custom COA template that defines all ledgers."},"SuccessEnvelope_ApplyCoaResponse_":{"properties":{"success":{"type":"boolean","title":"Success","default":true},"message":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Message"},"code":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Code"},"data":{"anyOf":[{"$ref":"#/components/schemas/ApplyCoaResponse"},{"type":"null"}]}},"additionalProperties":true,"type":"object","title":"SuccessEnvelope[ApplyCoaResponse]"},"ErrorResponse":{"title":"ErrorResponse","type":"object","description":"Envelope returned by every non-2xx response. Branch on `code` (stable) rather than `message` (human-readable, may change).","required":["error","message","code"],"properties":{"error":{"type":"string","description":"Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`."},"message":{"type":"string","description":"Human-readable explanation, safe to show to end users."},"code":{"type":"string","description":"Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`)."},"request_id":{"type":"string","description":"Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied."},"errors":{"type":"object","additionalProperties":{"type":"string"},"description":"Field-level validation messages keyed by field name. Present on 400/422 validation failures only."}}}},"securitySchemes":{"BearerAuth":{"type":"http","scheme":"bearer","bearerFormat":"API Token","description":"API token authentication. Format: `Bearer dz_...`"}}},"security":[{"BearerAuth":[]}]}