{"openapi":"3.1.0","info":{"title":"XPENSE API","version":"v1","description":"Expenses, receipts, legal entities and receipt senders for XPENSE (expense.xgi.io).\n\nPrivate workspace routes require XAuth credentials (`Authorization: Bearer`). Existing suite-token access is retained, and an explicit user-session token addressed to this API can use the strict resource profile. Tokens identify the caller; current permissions at `xpense` are asked of XAuth's Access Graph on every request. Each route names its permission in `x-permission`. OpenAPI is public; sender-link confirmation and intake webhooks retain their own token proofs.\n\nOn legacy routes, an XAuth API key (`Authorization: Bearer xak_…`) is accepted wherever the token is; XAuth decides it at each request (its status, lifetime, client-address condition, owner and policies). A person's key is that person, capped by the key's policies. A workspace's key is no one (sub `key:<id>`, no email); it holds roles at `xpense` as a person does, or single permissions by its policies, and works in its own workspace and those below it. It is taken only on the routes marked `x-workspace-keys: true` — reads that key nothing on the caller and answer the workspace's data — and still needs the route's permission there (403 `forbidden` without it). Everywhere else it is refused: 403 `workspace_key_not_accepted`, whatever it holds.\n\nRoutes that work in a workspace take `X-Workspace` (the workspace's guid or slug), and the caller must be a member of it, per the Access Graph. There is no default workspace.\n\nEvery refusal is an RFC 9457 problem (`application/problem+json`): `type`, `title`, `status`, `detail` (a sentence for a person), `code` and `trace`.\n\nCanonical `/v1/finance/*` routes require a current XAuth user/session token issued specifically to https://api.expense.xgi.io, canonical X-Workspace ID, app entitlement, membership and an explicit project permission. Workspace keys and suite-audience credentials are refused. A Finance-enabled project additionally requires the matching xfinance permission. Linked expenses are absent from all legacy workspace-wide reads, aggregates and mutations.\n\nAn additional browser origin requires a fresh account-origin allowance from XAuth and a strict user session addressed to this receiving API, before private reads, writes or forwarding. API keys, common/wrong audiences, delegated actors and unsupported DPoP modes cannot use additional origins. Origin allowance grants no resource permission. Anonymous preflight checks methods/headers only; trusted-suite and deliberate no-Origin modes retain their existing contracts.\n\nMoney is integer minor units (×100) with an ISO 4217 currency. There is no currency conversion: sums are always per currency.\n\nA receipt uploaded with POST /v1/receipts becomes an expense that needs review; Workers AI reads the file in the background (toMarkdown, then @cf/meta/llama-3.3-70b-instruct-fp8-fast in JSON Mode) and leaves suggestions on Expense.document.extraction. Nothing it reads is written to the expense: a person fills it in. Receipts mailed to the intake address arrive through XMail, which calls the Worker's MailIntake entrypoint over a service binding (not an HTTP route); intake is switched on by EMAIL_INTAKE_ADDRESS, empty in this version (GET /v1/intake answers null). Not in this version: reading a Dropbox folder."},"servers":[{"url":"https://api.expense.xgi.io"}],"paths":{"/health":{"get":{"summary":"Service health (the XTND five-key contract)","responses":{"200":{"description":"Up.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Health"}}}}}}},"/version":{"get":{"summary":"Build provenance","responses":{"200":{"description":"The running build.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Version"}}}}}}},"/openapi.json":{"get":{"summary":"This document","responses":{"200":{"description":"OpenAPI 3.1","content":{"application/json":{"schema":{"type":"object"}}}}}}},"/v1/openapi.json":{"get":{"summary":"This document, at its old path","deprecated":true,"description":"Kept while xtnd-expense-web still links here; use /openapi.json.","responses":{"200":{"description":"OpenAPI 3.1","content":{"application/json":{"schema":{"type":"object"}}}}}}},"/v1/me":{"get":{"summary":"The signed-in user","security":[{"bearer":[]}],"responses":{"200":{"description":"Who you are, and your roles and permissions at xpense now, as XAuth answers them.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Me"}}}},"401":{"$ref":"#/components/responses/Unauthorized"}}}},"/v1/workspaces":{"get":{"summary":"The workspaces the signed-in user can see","description":"Also for a workspace's API key: the workspaces it can see, as XWorkspace answers them. When XWorkspace does not act for a workspace's key, its 403 `workspace_key_not_accepted` is passed on.","x-workspace-keys":true,"security":[{"bearer":[]}],"responses":{"200":{"description":"As XWorkspace names them: those they belong to, and those below.","content":{"application/json":{"schema":{"type":"object","required":["workspaces"],"properties":{"workspaces":{"type":"array","items":{"$ref":"#/components/schemas/Workspace"}}}}}}},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/entities":{"get":{"summary":"The workspace's legal entities","x-workspace-keys":true,"description":"Active ones only, unless include_inactive=1.","security":[{"bearer":[]}],"x-permission":"expenses.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"include_inactive","in":"query","required":false,"description":"1 for inactive entities too.","schema":{"type":"string","enum":["0","1"]}}],"responses":{"200":{"description":"Entities.","content":{"application/json":{"schema":{"type":"object","required":["entities"],"properties":{"entities":{"type":"array","items":{"$ref":"#/components/schemas/Entity"}}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"502":{"$ref":"#/components/responses/Upstream"}}},"post":{"summary":"Add a legal entity","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/EntityCreate"}}}},"security":[{"bearer":[]}],"x-permission":"entities.manage","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"201":{"description":"Created.","content":{"application/json":{"schema":{"type":"object","required":["entity"],"properties":{"entity":{"$ref":"#/components/schemas/Entity"}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"415":{"$ref":"#/components/responses/UnsupportedType"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/entities/{entity_id}":{"patch":{"summary":"Change an entity","description":"code, jurisdiction and base_currency are fixed once the entity exists.","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/EntityPatch"}}}},"security":[{"bearer":[]}],"x-permission":"entities.manage","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"entity_id","in":"path","required":true,"description":"The entity.","schema":{"type":"string"}}],"responses":{"200":{"description":"The entity as it now is.","content":{"application/json":{"schema":{"type":"object","required":["entity"],"properties":{"entity":{"$ref":"#/components/schemas/Entity"}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"415":{"$ref":"#/components/responses/UnsupportedType"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/expenses":{"get":{"summary":"The ledger","x-workspace-keys":true,"description":"Newest transaction date first (expenses without a date after every dated one), then newest created first. Paged: pass next_cursor back as cursor.","security":[{"bearer":[]}],"x-permission":"expenses.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"status","in":"query","required":false,"description":"One status.","schema":{"type":"string","enum":["needs_review","ready","approved","rejected"]}},{"name":"entity_id","in":"query","required":false,"description":"An entity id, or `unattributed` for expenses with none.","schema":{"type":"string"}},{"name":"from","in":"query","required":false,"description":"Transaction date from, inclusive (YYYY-MM-DD). Expenses without a date are outside any range.","schema":{"type":"string","format":"date"}},{"name":"to","in":"query","required":false,"description":"Transaction date to, inclusive (YYYY-MM-DD).","schema":{"type":"string","format":"date"}},{"name":"q","in":"query","required":false,"description":"Search merchant names and descriptions (case-insensitive, contains).","schema":{"type":"string"}},{"name":"limit","in":"query","required":false,"description":"1 to 200; 50 when left out.","schema":{"type":"integer","minimum":1,"maximum":200}},{"name":"cursor","in":"query","required":false,"description":"next_cursor from the previous page.","schema":{"type":"string"}}],"responses":{"200":{"description":"A page of expenses.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseList"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"502":{"$ref":"#/components/responses/Upstream"}}},"post":{"summary":"Enter an expense by hand","description":"Any of the editable fields; the rest are null. Status is computed: ready when entity_id, transaction_date, merchant_name, currency and amount_cents are all set, needs_review otherwise.","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseFields"}}}},"security":[{"bearer":[]}],"x-permission":"expenses.write","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"201":{"description":"Created, with source manual.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"415":{"$ref":"#/components/responses/UnsupportedType"},"422":{"$ref":"#/components/responses/Unprocessable"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/expenses/export.csv":{"get":{"summary":"Export expenses as CSV","x-workspace-keys":true,"description":"RFC 4180, every field quoted, CRLF line ends, oldest transaction first. Columns: expense_id, transaction_date, entity_code, merchant_name, merchant_tax_id, currency, amount, tax_amount, category, project_code, description, status, approved_at, approved_by. Amounts are decimal strings with two places, never altered (a negative amount stays -12.34); an unknown amount is an empty field. Against formula injection (OWASP), a text field — entity_code, merchant_name, merchant_tax_id, category, project_code, description, approved_by — that starts with =, +, -, @, a tab or a carriage return is prefixed with a single quote (').","security":[{"bearer":[]}],"x-permission":"expenses.export","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"status","in":"query","required":false,"description":"approved when left out.","schema":{"type":"string","enum":["needs_review","ready","approved","rejected"]}},{"name":"entity_id","in":"query","required":false,"description":"An entity id, or `unattributed` for expenses with none.","schema":{"type":"string"}},{"name":"from","in":"query","required":false,"description":"Transaction date from, inclusive (YYYY-MM-DD). Expenses without a date are outside any range.","schema":{"type":"string","format":"date"}},{"name":"to","in":"query","required":false,"description":"Transaction date to, inclusive (YYYY-MM-DD).","schema":{"type":"string","format":"date"}}],"responses":{"200":{"description":"The CSV file.","content":{"text/csv; charset=utf-8":{"schema":{"type":"string"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/expenses/{expense_id}":{"get":{"summary":"One expense","x-workspace-keys":true,"security":[{"bearer":[]}],"x-permission":"expenses.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"The expense.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"502":{"$ref":"#/components/responses/Upstream"}}},"patch":{"summary":"Fill in or correct an expense","description":"Only while it is needs_review or ready. null clears a field. Status is recomputed.","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseFields"}}}},"security":[{"bearer":[]}],"x-permission":"expenses.write","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"The expense as it now is.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"415":{"$ref":"#/components/responses/UnsupportedType"},"422":{"$ref":"#/components/responses/Unprocessable"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/expenses/{expense_id}/approve":{"post":{"summary":"Approve an expense","description":"Only from ready (which has an entity); 409 otherwise. Records approved_by and approved_at.","security":[{"bearer":[]}],"x-permission":"expenses.approve","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"Approved.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/expenses/{expense_id}/reject":{"post":{"summary":"Reject an expense","description":"From needs_review or ready; 409 otherwise.","requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","required":["reason"],"additionalProperties":false,"properties":{"reason":{"type":"string","minLength":1,"maxLength":1000}}}}}},"security":[{"bearer":[]}],"x-permission":"expenses.approve","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"Rejected.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"415":{"$ref":"#/components/responses/UnsupportedType"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/expenses/{expense_id}/document":{"get":{"summary":"The expense's receipt file","x-workspace-keys":true,"description":"The bytes as uploaded, with their Content-Type, Content-Disposition: inline; filename=\"…\", and Cache-Control: private, no-store.","security":[{"bearer":[]}],"x-permission":"expenses.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"The file.","content":{"application/pdf":{"schema":{"type":"string","format":"binary"}},"image/jpeg":{"schema":{"type":"string","format":"binary"}},"image/png":{"schema":{"type":"string","format":"binary"}},"image/webp":{"schema":{"type":"string","format":"binary"}},"image/heic":{"schema":{"type":"string","format":"binary"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/expenses/{expense_id}/document/extract":{"post":{"summary":"Read the receipt again","description":"Starts a new reading of the expense's receipt by Workers AI, in the background: 202, with extraction.status pending. Allowed after a reading failed or finished, and for a receipt never read; refused (409) while a reading is under way. It changes the document's suggestions only, never the expense.","security":[{"bearer":[]}],"x-permission":"expenses.write","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"202":{"description":"The new reading is pending.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/receipts":{"post":{"summary":"Upload a receipt","description":"Stores the file (in R2, under the workspace's guid) and creates an expense that needs review, with source upload and every value null. Workers AI then reads the file in the background: the answer does not wait, and shows extraction.status pending. At most 10485760 bytes (10 MB). The same file (SHA-256 of its bytes) twice in one workspace is a 409 naming the expense that has it.","requestBody":{"required":true,"content":{"multipart/form-data":{"schema":{"type":"object","required":["file"],"properties":{"file":{"type":"string","format":"binary","description":"application/pdf, image/jpeg, image/png, image/webp, image/heic"},"entity_id":{"type":"string","description":"Optional: an active entity of the workspace. Empty is none."}}}}}},"security":[{"bearer":[]}],"x-permission":"receipts.upload","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"201":{"description":"The new expense.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"description":"The file is already in this workspace.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/DuplicateError"}}}},"413":{"$ref":"#/components/responses/TooLarge"},"415":{"$ref":"#/components/responses/UnsupportedType"},"422":{"$ref":"#/components/responses/Unprocessable"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/summary":{"get":{"summary":"Totals per status and currency","x-workspace-keys":true,"description":"Different currencies are never added together. Expenses without a currency or an amount are in no total. unattributed_count counts expenses without an entity that are not rejected; needs_review_count counts expenses that need review.","security":[{"bearer":[]}],"x-permission":"expenses.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"entity_id","in":"query","required":false,"description":"An entity id, or `unattributed` for expenses with none.","schema":{"type":"string"}},{"name":"from","in":"query","required":false,"description":"Transaction date from, inclusive (YYYY-MM-DD). Expenses without a date are outside any range.","schema":{"type":"string","format":"date"}},{"name":"to","in":"query","required":false,"description":"Transaction date to, inclusive (YYYY-MM-DD).","schema":{"type":"string","format":"date"}}],"responses":{"200":{"description":"The summary.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Summary"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/senders":{"get":{"summary":"The workspace's sender addresses","x-workspace-keys":true,"description":"Verified addresses, and addresses whose verification is pending (sent and not expired).","security":[{"bearer":[]}],"x-permission":"senders.manage","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"200":{"description":"Senders, by address.","content":{"application/json":{"schema":{"type":"object","required":["senders"],"properties":{"senders":{"type":"array","items":{"$ref":"#/components/schemas/Sender"}}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"502":{"$ref":"#/components/responses/Upstream"}}},"post":{"summary":"Add an address: send it a code and a link","description":"Sends one message to the address through XChange (its Dispatch entrypoint, from system@xgi.io), acting for the calling user, with a 6-digit code and a single-use link, valid 24 hours. A new request for the same address replaces the previous one. If XChange does not accept the message: 502, and no verification is left pending. An address verified in another workspace stays there until this verification succeeds, then moves here. At most 3 messages to one address (from all workspaces together) and 20 from one user in any 24 hours, counting the messages XChange accepted: above either, 429 with Retry-After, and nothing is sent or changed.","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/SenderCreate"}}}},"security":[{"bearer":[]}],"x-permission":"senders.manage","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"201":{"description":"Sent.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/SenderCreated"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"415":{"$ref":"#/components/responses/UnsupportedType"},"422":{"$ref":"#/components/responses/Unprocessable"},"429":{"$ref":"#/components/responses/TooManyRequests"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/senders/verify":{"post":{"summary":"Confirm an address with its code","description":"A wrong code is a 422 with remaining_attempts; the 5th wrong code, or an expired verification, is a 410 and the verification is gone.","requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","required":["verification_id","code"],"additionalProperties":false,"properties":{"verification_id":{"type":"string"},"code":{"type":"string","pattern":"^[0-9]{6}$"}}}}}},"security":[{"bearer":[]}],"x-permission":"senders.manage","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"200":{"description":"Verified.","content":{"application/json":{"schema":{"type":"object","required":["sender"],"properties":{"sender":{"$ref":"#/components/schemas/Sender"}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"410":{"$ref":"#/components/responses/Gone"},"415":{"$ref":"#/components/responses/UnsupportedType"},"422":{"description":"Wrong code.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/WrongCodeError"}}}},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/senders/{email}":{"patch":{"summary":"Change a verified address's entity, project or category","requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"properties":{"entity_id":{"type":["string","null"],"description":"An active entity of the workspace; null is unattributed."},"project_code":{"type":["string","null"],"maxLength":64},"category":{"type":["string","null"],"maxLength":100}}}}}},"security":[{"bearer":[]}],"x-permission":"senders.manage","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"email","in":"path","required":true,"description":"The address (URL-encoded).","schema":{"type":"string"}}],"responses":{"200":{"description":"The sender as it now is.","content":{"application/json":{"schema":{"type":"object","required":["sender"],"properties":{"sender":{"$ref":"#/components/schemas/Sender"}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"415":{"$ref":"#/components/responses/UnsupportedType"},"422":{"$ref":"#/components/responses/Unprocessable"},"502":{"$ref":"#/components/responses/Upstream"}}},"delete":{"summary":"Remove an address","description":"Removes its verified mapping and any pending verification in this workspace.","security":[{"bearer":[]}],"x-permission":"senders.manage","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"email","in":"path","required":true,"description":"The address (URL-encoded).","schema":{"type":"string"}}],"responses":{"204":{"description":"Removed."},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/sender-verifications/confirm":{"post":{"summary":"Confirm an address with its emailed link (public)","description":"No sign-in: the token from the link is the proof, and it works once.","requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","required":["token"],"additionalProperties":false,"properties":{"token":{"type":"string"}}}}}},"responses":{"200":{"description":"Confirmed.","content":{"application/json":{"schema":{"type":"object","required":["email","workspace_name"],"properties":{"email":{"type":"string"},"workspace_name":{"type":"string"}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"404":{"$ref":"#/components/responses/NotFound"},"410":{"$ref":"#/components/responses/Gone"},"415":{"$ref":"#/components/responses/UnsupportedType"}}}},"/v1/intake":{"get":{"summary":"Where receipts can be mailed","x-workspace-keys":true,"description":"The address receipts can be mailed to: the Worker variable EMAIL_INTAKE_ADDRESS, the same for every workspace. null while email intake is not switched on (the variable is empty, as it is in this version). Once it is set, mail to it from a verified sender becomes expenses that need review, in the workspace that verified the sender.","security":[{"bearer":[]}],"x-permission":"expenses.read","responses":{"200":{"description":"The intake address.","content":{"application/json":{"schema":{"type":"object","required":["email"],"properties":{"email":{"type":"object","required":["address"],"properties":{"address":{"type":["string","null"],"format":"email"}}}}}}}},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"}}}},"/v1/adapters":{"get":{"summary":"The intakes a source can be created for","x-workspace-keys":true,"description":"None is available yet: see each one's `available`.","security":[{"bearer":[]}],"x-permission":"receipts.upload","responses":{"200":{"description":"Intakes.","content":{"application/json":{"schema":{"type":"object","required":["adapters"],"properties":{"adapters":{"type":"array","items":{"$ref":"#/components/schemas/Adapter"}}}}}}},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"}}}},"/v1/sources":{"get":{"summary":"The workspace's intake sources","x-workspace-keys":true,"description":"The sources created with POST /v1/sources. The workspace's mail-intake source (adapter_id mail-intake), which email intake makes for the messages it stores, has no webhook and is not listed.","security":[{"bearer":[]}],"x-permission":"receipts.upload","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"200":{"description":"Sources.","content":{"application/json":{"schema":{"type":"object","required":["sources"],"properties":{"sources":{"type":"array","items":{"$ref":"#/components/schemas/Source"}}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"502":{"$ref":"#/components/responses/Upstream"}}},"post":{"summary":"Create an intake source","description":"Answers its webhook URL once; only a hash of the URL token is kept.","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/SourceCreate"}}}},"security":[{"bearer":[]}],"x-permission":"receipts.upload","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"201":{"description":"Created.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/SourceCreated"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"415":{"$ref":"#/components/responses/UnsupportedType"},"502":{"$ref":"#/components/responses/Upstream"}}}},"/v1/hooks/{source_id}/{token}":{"post":{"summary":"Intake webhook (any method)","description":"Authenticated by the token in the URL, compared by hash. No intake is available yet: a request with the right token is answered 501 and nothing is stored.","parameters":[{"name":"source_id","in":"path","required":true,"description":"The source.","schema":{"type":"string"}},{"name":"token","in":"path","required":true,"description":"The token from its hook_url.","schema":{"type":"string"}}],"responses":{"404":{"$ref":"#/components/responses/NotFound"},"501":{"$ref":"#/components/responses/NotImplemented"}}}},"/v1/finance/projects/{project_id}/entities":{"get":{"summary":"The workspace's legal entities","x-workspace-keys":false,"description":"Active ones only, unless include_inactive=1.","security":[{"bearer":[]}],"x-permission":"expenses.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"project_id","in":"path","required":true,"description":"Canonical XWorkspace project ID.","schema":{"type":"string"}},{"name":"include_inactive","in":"query","required":false,"description":"1 for inactive entities too.","schema":{"type":"string","enum":["0","1"]}}],"responses":{"200":{"description":"Entities.","content":{"application/json":{"schema":{"type":"object","required":["entities"],"properties":{"entities":{"type":"array","items":{"$ref":"#/components/schemas/Entity"}}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"project"}},"/v1/finance/projects/{project_id}/expenses":{"get":{"summary":"The ledger","x-workspace-keys":false,"description":"Newest transaction date first (expenses without a date after every dated one), then newest created first. Paged: pass next_cursor back as cursor.","security":[{"bearer":[]}],"x-permission":"expenses.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"project_id","in":"path","required":true,"description":"Canonical XWorkspace project ID.","schema":{"type":"string"}},{"name":"status","in":"query","required":false,"description":"One status.","schema":{"type":"string","enum":["needs_review","ready","approved","rejected"]}},{"name":"entity_id","in":"query","required":false,"description":"An entity id, or `unattributed` for expenses with none.","schema":{"type":"string"}},{"name":"from","in":"query","required":false,"description":"Transaction date from, inclusive (YYYY-MM-DD). Expenses without a date are outside any range.","schema":{"type":"string","format":"date"}},{"name":"to","in":"query","required":false,"description":"Transaction date to, inclusive (YYYY-MM-DD).","schema":{"type":"string","format":"date"}},{"name":"q","in":"query","required":false,"description":"Search merchant names and descriptions (case-insensitive, contains).","schema":{"type":"string"}},{"name":"limit","in":"query","required":false,"description":"1 to 200; 50 when left out.","schema":{"type":"integer","minimum":1,"maximum":200}},{"name":"cursor","in":"query","required":false,"description":"next_cursor from the previous page.","schema":{"type":"string"}}],"responses":{"200":{"description":"A page of expenses.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseList"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"project"}},"/v1/finance/projects/{project_id}/expenses/{expense_id}":{"get":{"summary":"One expense","x-workspace-keys":false,"security":[{"bearer":[]}],"x-permission":"expenses.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"project_id","in":"path","required":true,"description":"Canonical XWorkspace project ID.","schema":{"type":"string"}},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"The expense.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"project"},"patch":{"summary":"Fill in or correct an expense","description":"Only while it is needs_review or ready. null clears a field. Status is recomputed.","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseFields"}}}},"security":[{"bearer":[]}],"x-permission":"expenses.write","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"project_id","in":"path","required":true,"description":"Canonical XWorkspace project ID.","schema":{"type":"string"}},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"The expense as it now is.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"415":{"$ref":"#/components/responses/UnsupportedType"},"422":{"$ref":"#/components/responses/Unprocessable"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"project","x-workspace-keys":false}},"/v1/finance/projects/{project_id}/expenses/{expense_id}/approve":{"post":{"summary":"Approve an expense","description":"Only from ready (which has an entity); 409 otherwise. Records approved_by and approved_at.","security":[{"bearer":[]}],"x-permission":"expenses.approve","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"project_id","in":"path","required":true,"description":"Canonical XWorkspace project ID.","schema":{"type":"string"}},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"Approved.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"project","x-workspace-keys":false}},"/v1/finance/projects/{project_id}/expenses/{expense_id}/reject":{"post":{"summary":"Reject an expense","description":"From needs_review or ready; 409 otherwise.","requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","required":["reason"],"additionalProperties":false,"properties":{"reason":{"type":"string","minLength":1,"maxLength":1000}}}}}},"security":[{"bearer":[]}],"x-permission":"expenses.approve","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"project_id","in":"path","required":true,"description":"Canonical XWorkspace project ID.","schema":{"type":"string"}},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"Rejected.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"415":{"$ref":"#/components/responses/UnsupportedType"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"project","x-workspace-keys":false}},"/v1/finance/projects/{project_id}/expenses/{expense_id}/document":{"get":{"summary":"The expense's receipt file","x-workspace-keys":false,"description":"The bytes as uploaded, with their Content-Type, Content-Disposition: inline; filename=\"…\", and Cache-Control: private, no-store.","security":[{"bearer":[]}],"x-permission":"expenses.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"project_id","in":"path","required":true,"description":"Canonical XWorkspace project ID.","schema":{"type":"string"}},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"200":{"description":"The file.","content":{"application/pdf":{"schema":{"type":"string","format":"binary"}},"image/jpeg":{"schema":{"type":"string","format":"binary"}},"image/png":{"schema":{"type":"string","format":"binary"}},"image/webp":{"schema":{"type":"string","format":"binary"}},"image/heic":{"schema":{"type":"string","format":"binary"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"project"}},"/v1/finance/projects/{project_id}/expenses/{expense_id}/document/extract":{"post":{"summary":"Read the receipt again","description":"Starts a new reading of the expense's receipt by Workers AI, in the background: 202, with extraction.status pending. Allowed after a reading failed or finished, and for a receipt never read; refused (409) while a reading is under way. It changes the document's suggestions only, never the expense.","security":[{"bearer":[]}],"x-permission":"expenses.write","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"project_id","in":"path","required":true,"description":"Canonical XWorkspace project ID.","schema":{"type":"string"}},{"name":"expense_id","in":"path","required":true,"description":"The expense.","schema":{"type":"string"}}],"responses":{"202":{"description":"The new reading is pending.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"404":{"$ref":"#/components/responses/NotFound"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"project","x-workspace-keys":false}},"/v1/finance/projects/{project_id}/receipts":{"post":{"summary":"Upload a receipt","description":"Stores the file (in R2, under the workspace's guid) and creates an expense that needs review, with source upload and every value null. Workers AI then reads the file in the background: the answer does not wait, and shows extraction.status pending. At most 10485760 bytes (10 MB). The same file (SHA-256 of its bytes) twice in one workspace is a 409 naming the expense that has it.","requestBody":{"required":true,"content":{"multipart/form-data":{"schema":{"type":"object","required":["file"],"properties":{"file":{"type":"string","format":"binary","description":"application/pdf, image/jpeg, image/png, image/webp, image/heic"},"entity_id":{"type":"string","description":"Optional: an active entity of the workspace. Empty is none."}}}}}},"security":[{"bearer":[]}],"x-permission":"receipts.upload","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"project_id","in":"path","required":true,"description":"Canonical XWorkspace project ID.","schema":{"type":"string"}}],"responses":{"201":{"description":"The new expense.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"413":{"$ref":"#/components/responses/TooLarge"},"415":{"$ref":"#/components/responses/UnsupportedType"},"422":{"$ref":"#/components/responses/Unprocessable"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"project","x-workspace-keys":false}},"/v1/finance/imports/notion":{"post":{"summary":"Import one reviewed Notion source into an explicit canonical project","description":"At most 128 KiB. Source identity is unique by workspace, provider and Notion page ID, independent of project. Exact snapshot/mapping replay returns the existing untouched expense. Changed source/mapping or subsequent local edits return source_import_conflict without overwriting. Native status starts needs_review and spender remains null. Historical payer/status and all attachment references are immutable provenance; no payment, approval, receipt archive or automatic Finance delivery is implied.","security":[{"bearer":[]}],"x-permission":"expenses.write","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"200":{"description":"Exact source replay; no duplicate expense or audit.","content":{"application/json":{"schema":{"type":"object","required":["outcome","expense"],"properties":{"outcome":{"const":"replayed"},"expense":{"$ref":"#/components/schemas/Expense"}}}}}},"201":{"description":"Imported atomically with immutable source provenance and real importer audit.","content":{"application/json":{"schema":{"type":"object","required":["outcome","expense"],"properties":{"outcome":{"const":"created"},"expense":{"$ref":"#/components/schemas/Expense"}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"413":{"description":"The one-record request exceeds 128 KiB."},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["project_id","source","expense"],"properties":{"project_id":{"type":"string","description":"Explicit canonical target project in X-Workspace, with active expenses capability and current write permission."},"source":{"type":"object","additionalProperties":false,"required":["page_id","database_id","data_source_id","page_url","last_edited_time","status","paid_by","receipt_references","original_properties","provider"],"properties":{"page_id":{"type":"string","description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"database_id":{"type":"string","description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"data_source_id":{"type":["string","null"],"description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"page_url":{"type":"string","format":"uri","maxLength":2048,"description":"HTTPS Notion page URL that identifies page_id."},"last_edited_time":{"type":["string","null"],"format":"date-time"},"status":{"type":["string","null"],"maxLength":200},"paid_by":{"type":["object","null"],"additionalProperties":false,"required":["page_id","page_url","name"],"properties":{"page_id":{"type":["string","null"],"description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"page_url":{"type":["string","null"],"format":"uri","maxLength":2048},"name":{"type":["string","null"],"maxLength":300}}},"receipt_references":{"type":"array","maxItems":100,"description":"Original attachment references only. These are not archived receipt files and are never fetched by import.","items":{"type":"object","additionalProperties":false,"required":["name","url"],"properties":{"name":{"type":["string","null"],"maxLength":300},"url":{"type":"string","maxLength":4096,"description":"HTTPS URL or stable notion-file-block reference."}}}},"original_properties":{"type":"object","additionalProperties":true,"description":"Complete original property snapshot, including raw amount and unknown fields. Maximum JSON depth 16; total request at most 128 KiB."},"provider":{"type":["object","null"],"additionalProperties":false,"required":["page_id","page_url","name"],"properties":{"page_id":{"type":["string","null"],"description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"page_url":{"type":["string","null"],"format":"uri","maxLength":2048},"name":{"type":["string","null"],"maxLength":300}}}}},"expense":{"type":"object","additionalProperties":false,"required":["transaction_date","amount_cents","currency","merchant_name","category","description"],"properties":{"transaction_date":{"type":["string","null"],"format":"date"},"amount_cents":{"type":["integer","null"],"minimum":-9007199254740991,"maximum":9007199254740991,"description":"Reviewed amount in Xpense minor units (x100). Must be null if currency is unknown; preserve raw value in original_properties."},"currency":{"type":["string","null"],"pattern":"^[A-Z]{3}$"},"merchant_name":{"type":["string","null"],"maxLength":200},"category":{"type":["string","null"],"maxLength":100},"description":{"type":["string","null"],"maxLength":2000}}}}}}}},"x-resource":"https://api.expense.xgi.io","x-scope":"explicit project in body or source","x-workspace-keys":false}},"/v1/finance/projects":{"get":{"summary":"Discover explicitly accessible canonical expense projects","security":[{"bearer":[]}],"x-permission":"projects.read","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"cursor","in":"query","required":false,"description":"Directory pagination cursor.","schema":{"type":"string"}}],"responses":{"200":{"description":"Accessible project directory.","content":{"application/json":{"schema":{"type":"object","properties":{"projects":{"type":"array","items":{"type":"object","required":["id","workspaceId","name","lifecycle","capabilities","revision"],"properties":{"id":{"type":"string"},"workspaceId":{"type":"string"},"name":{"type":"string"},"code":{"type":["string","null"]},"lifecycle":{"enum":["active","archived"]},"capabilities":{"type":"array","items":{"enum":["expenses","finance"]}},"revision":{"type":"integer","minimum":1}}}},"next_cursor":{"type":["string","null"]}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"explicit project in body or source","x-workspace-keys":false}},"/v1/finance/expenses":{"post":{"summary":"Capture an expense in a canonical project","description":"Any of the editable fields; the rest are null. Status is computed: ready when entity_id, transaction_date, merchant_name, currency and amount_cents are all set, needs_review otherwise.","requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["project_id"],"properties":{"project_id":{"type":"string"},"entity_id":{"type":["string","null"],"description":"An active entity of the workspace; null is unattributed."},"transaction_date":{"type":["string","null"],"format":"date"},"merchant_name":{"type":["string","null"],"maxLength":200},"merchant_tax_id":{"type":["string","null"],"maxLength":64},"currency":{"type":["string","null"],"pattern":"^[A-Z]{3}$","description":"ISO 4217 (the form is checked, not the list)."},"amount_cents":{"type":["integer","null"],"description":"Minor units ×100."},"tax_amount_cents":{"type":["integer","null"],"description":"Minor units ×100."},"category":{"type":["string","null"],"maxLength":100},"project_code":{"type":["string","null"],"maxLength":64},"description":{"type":["string","null"],"maxLength":2000}}}}}},"security":[{"bearer":[]}],"x-permission":"expenses.write","parameters":[{"$ref":"#/components/parameters/Workspace"}],"responses":{"201":{"description":"Created, with source manual.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExpenseResponse"}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"415":{"$ref":"#/components/responses/UnsupportedType"},"422":{"$ref":"#/components/responses/Unprocessable"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"x-resource":"https://api.expense.xgi.io","x-scope":"explicit project in body or source","x-workspace-keys":false}},"/v1/finance/expenses/{expense_id}/project":{"patch":{"summary":"Explicitly link or move a source record within its workspace","description":"Requires new target and old project grants; an unlinked record requires an explicit workspace expenses.write grant. Names/codes never establish ownership.","security":[{"bearer":[]}],"x-permission":"expenses.write","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"expense_id","in":"path","required":true,"description":"Source expense ID.","schema":{"type":"string"}}],"responses":{"200":{"description":"Linked source with increased version.","content":{"application/json":{"schema":{"type":"object","properties":{"expense":{"$ref":"#/components/schemas/Expense"}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["project_id","version"],"properties":{"project_id":{"type":"string"},"version":{"type":"integer","minimum":1}}}}}},"x-resource":"https://api.expense.xgi.io","x-scope":"explicit project in body or source","x-workspace-keys":false}},"/v1/finance/expenses/{expense_id}/submit":{"post":{"summary":"Submit the latest source snapshot to Finance as the current user","description":"Uses XAuth delegated service token, immutable source versions and idempotent Finance receipt. An approval remains source evidence only.","security":[{"bearer":[]}],"x-permission":"expenses.write","parameters":[{"$ref":"#/components/parameters/Workspace"},{"name":"expense_id","in":"path","required":true,"description":"Source expense ID.","schema":{"type":"string"}}],"responses":{"200":{"description":"Confirmed source delivery.","content":{"application/json":{"schema":{"type":"object","properties":{"expense_id":{"type":"string"},"project_id":{"type":"string"},"source_version":{"type":"integer"},"outcome":{"enum":["created","updated","replayed"]}}}}}},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"$ref":"#/components/responses/Unauthorized"},"403":{"$ref":"#/components/responses/Forbidden"},"409":{"$ref":"#/components/responses/Conflict"},"502":{"$ref":"#/components/responses/Upstream"},"503":{"description":"Strict dependency bindings are not configured."}},"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false}}}},"x-resource":"https://api.expense.xgi.io","x-scope":"explicit project in body or source","x-workspace-keys":false}}},"components":{"securitySchemes":{"bearer":{"type":"http","scheme":"bearer","bearerFormat":"XAuth token (JWT) or XAuth API key (xak_…)","description":"An XAuth token (EdDSA, typ at+jwt, aud https://xgi.io), or an XAuth API key: `xak_` and 43 characters, opaque, decided by XAuth at each request. A person's key acts as that person, capped by its policies; a workspace's key only where a route says `x-workspace-keys: true`."}},"parameters":{"Workspace":{"name":"X-Workspace","in":"header","required":true,"description":"The workspace the request works in: its guid or slug. Without it, 400 workspace_required.","schema":{"type":"string"}}},"responses":{"BadRequest":{"description":"Invalid input.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"Unauthorized":{"description":"Not signed in: no XAuth token or API key, a token XAuth did not sign or from a session that has ended, or an API key XAuth does not accept now — unknown, disabled, expired or not yet valid (WWW-Authenticate: Bearer error=\"invalid_token\").","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"Forbidden":{"description":"Missing the permission, or not a member of the workspace; for an API key, also outside its policies, from an address its condition refuses (`ip_not_allowed`), or a workspace's key where the route acts for a person (`workspace_key_not_accepted`, also passed on from XWorkspace when its hop does not act for one).","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"NotFound":{"description":"Not found in this workspace.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"Conflict":{"description":"Conflicting state or duplicate.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"Gone":{"description":"Expired, or closed after too many wrong codes.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"TooLarge":{"description":"Larger than 10 MB.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"UnsupportedType":{"description":"Not a type this route takes.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"Unprocessable":{"description":"Failed validation (e.g. an entity that is not an active one of this workspace).","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"TooManyRequests":{"description":"A limit on verification messages is reached: nothing was sent and nothing changed.","headers":{"Retry-After":{"description":"Seconds until a message may be sent again.","schema":{"type":"integer","minimum":1}}},"content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"NotSent":{"description":"XChange did not accept the message: nothing is pending.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"Upstream":{"description":"XWorkspace did not answer what the workspace is: nothing was read or changed.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"NotImplemented":{"description":"Not available yet.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}}},"schemas":{"Error":{"type":"object","description":"An RFC 9457 problem.","required":["type","title","status","detail","code"],"properties":{"type":{"type":"string","format":"uri","description":"<this API's origin>/problems/<code>: on the origin the request reached"},"title":{"type":"string","description":"The status's name."},"status":{"type":"integer"},"detail":{"type":"string","description":"A sentence for a person."},"code":{"type":"string","description":"A short code."},"trace":{"type":"string","description":"The request's trace id."}}},"DuplicateError":{"allOf":[{"$ref":"#/components/schemas/Error"},{"type":"object","required":["expense_id"],"properties":{"code":{"const":"duplicate"},"expense_id":{"type":["string","null"]}}}]},"WrongCodeError":{"allOf":[{"$ref":"#/components/schemas/Error"},{"type":"object","required":["remaining_attempts"],"properties":{"code":{"const":"wrong_code"},"remaining_attempts":{"type":"integer","minimum":1,"maximum":4}}}]},"Health":{"type":"object","required":["status","service","version","time","timestamp"],"additionalProperties":false,"properties":{"status":{"const":"ok"},"service":{"const":"xtnd-expense-api"},"version":{"type":"string"},"time":{"type":"string","format":"date-time"},"timestamp":{"type":"string","format":"date-time"}}},"Version":{"type":"object","required":["service","version","sha","committed_at"],"additionalProperties":false,"properties":{"service":{"const":"xtnd-expense-api"},"version":{"type":"string","description":"package.json version."},"sha":{"type":"string","description":"Short commit SHA of the build."},"committed_at":{"type":"string","format":"date-time","description":"The commit's author date."}}},"Me":{"type":"object","required":["user_guid","email","roles","permissions"],"properties":{"user_guid":{"type":"string"},"email":{"type":"string"},"roles":{"type":"array","items":{"type":"string"}},"permissions":{"type":"array","items":{"type":"string","enum":["expenses.read","expenses.write","expenses.approve","expenses.export","receipts.upload","entities.manage","senders.manage"]}}}},"Workspace":{"type":"object","required":["guid","slug","name","role"],"properties":{"guid":{"type":"string"},"slug":{"type":"string"},"name":{"type":"string"},"role":{"type":"string"},"parent":{"description":"The workspace above it, when the caller can see it; null otherwise. As XWorkspace answers it.","oneOf":[{"type":"object","required":["guid","slug","name"],"properties":{"guid":{"type":"string"},"slug":{"type":"string"},"name":{"type":"string"}}},{"type":"null"}]},"code":{"type":["string","null"],"description":"The workspace's own short code, set in XWorkspace (a switcher's badge). Absent or null when none is set."},"system":{"type":"boolean","description":"True for a system workspace, as XWorkspace marks it. Absent otherwise."}}},"Entity":{"type":"object","required":["entity_id","name","code","jurisdiction","base_currency","tax_id","tax_rate","tax_label","is_active","created_at","updated_at"],"properties":{"entity_id":{"type":"string"},"name":{"type":"string"},"code":{"type":"string","pattern":"^[A-Z0-9-]{2,16}$"},"jurisdiction":{"type":"string","pattern":"^[A-Z]{2}$","description":"ISO 3166-1 alpha-2."},"base_currency":{"type":"string","pattern":"^[A-Z]{3}$","description":"ISO 4217."},"tax_id":{"type":["string","null"]},"tax_rate":{"type":["number","null"],"minimum":0,"exclusiveMaximum":1},"tax_label":{"type":["string","null"]},"is_active":{"type":"boolean"},"created_at":{"type":"string","format":"date-time"},"updated_at":{"type":"string","format":"date-time"}}},"EntityCreate":{"type":"object","required":["name","code","jurisdiction","base_currency"],"additionalProperties":false,"properties":{"name":{"type":"string","maxLength":200},"code":{"type":"string","pattern":"^[A-Z0-9-]{2,16}$","description":"Unique in the workspace."},"jurisdiction":{"type":"string","pattern":"^[A-Z]{2}$"},"base_currency":{"type":"string","pattern":"^[A-Z]{3}$"},"tax_id":{"type":["string","null"],"maxLength":64},"tax_rate":{"type":["number","null"],"minimum":0,"exclusiveMaximum":1},"tax_label":{"type":["string","null"],"maxLength":32}}},"EntityPatch":{"type":"object","additionalProperties":false,"properties":{"name":{"type":"string","maxLength":200},"tax_id":{"type":["string","null"],"maxLength":64},"tax_rate":{"type":["number","null"],"minimum":0,"exclusiveMaximum":1},"tax_label":{"type":["string","null"],"maxLength":32},"is_active":{"type":"boolean"}}},"ExpenseFields":{"type":"object","additionalProperties":false,"properties":{"entity_id":{"type":["string","null"],"description":"An active entity of the workspace; null is unattributed."},"transaction_date":{"type":["string","null"],"format":"date"},"merchant_name":{"type":["string","null"],"maxLength":200},"merchant_tax_id":{"type":["string","null"],"maxLength":64},"currency":{"type":["string","null"],"pattern":"^[A-Z]{3}$","description":"ISO 4217 (the form is checked, not the list)."},"amount_cents":{"type":["integer","null"],"description":"Minor units ×100."},"tax_amount_cents":{"type":["integer","null"],"description":"Minor units ×100."},"category":{"type":["string","null"],"maxLength":100},"project_code":{"type":["string","null"],"maxLength":64},"description":{"type":["string","null"],"maxLength":2000}}},"Expense":{"type":"object","required":["expense_id","version","status","entity_id","transaction_date","merchant_name","merchant_tax_id","currency","amount_cents","tax_amount_cents","category","project_code","description","source","source_detail","sender_email","document","spender_user_guid","created_at","updated_at","approved_by","approved_at","rejected_by","rejected_at","rejection_reason"],"properties":{"expense_id":{"type":"string"},"project_id":{"type":"string","description":"Explicit canonical project ID, present on scoped records only."},"finance_delivery":{"type":["object","null"],"description":"Latest confirmed delivery in this project. Compare source_version to version; delivery is not payment.","properties":{"project_id":{"type":"string"},"source_version":{"type":"integer"},"outcome":{"enum":["created","updated","replayed"]},"delivered_at":{"type":"string","format":"date-time"}}},"version":{"type":"integer","minimum":1,"readOnly":true,"description":"Source revision. Increases when expense fields or approval status change. Consumers can use it to reject older deliveries and identify retries."},"status":{"type":"string","enum":["needs_review","ready","approved","rejected"]},"entity_id":{"type":["string","null"],"description":"An active entity of the workspace; null is unattributed."},"transaction_date":{"type":["string","null"],"format":"date"},"merchant_name":{"type":["string","null"],"maxLength":200},"merchant_tax_id":{"type":["string","null"],"maxLength":64},"currency":{"type":["string","null"],"pattern":"^[A-Z]{3}$","description":"ISO 4217 (the form is checked, not the list)."},"amount_cents":{"type":["integer","null"],"description":"Minor units ×100."},"tax_amount_cents":{"type":["integer","null"],"description":"Minor units ×100."},"category":{"type":["string","null"],"maxLength":100},"project_code":{"type":["string","null"],"maxLength":64},"description":{"type":["string","null"],"maxLength":2000},"source":{"type":"string","enum":["upload","manual","email","dropbox","notion"]},"source_detail":{"type":["string","null"]},"provenance":{"type":"object","additionalProperties":false,"required":["provider","source_id","database_id","data_source_id","page_url","last_edited_time","status","paid_by","receipt_references","original_properties","provider_details","source_hash","imported_by","imported_at"],"properties":{"provider":{"const":"notion"},"source_id":{"type":"string","description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"database_id":{"type":"string","description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"data_source_id":{"type":["string","null"],"description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"page_url":{"type":"string","format":"uri","maxLength":2048,"description":"HTTPS Notion page URL that identifies page_id."},"last_edited_time":{"type":["string","null"],"format":"date-time"},"status":{"type":["string","null"],"maxLength":200},"paid_by":{"type":["object","null"],"additionalProperties":false,"required":["page_id","page_url","name"],"properties":{"page_id":{"type":["string","null"],"description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"page_url":{"type":["string","null"],"format":"uri","maxLength":2048},"name":{"type":["string","null"],"maxLength":300}}},"receipt_references":{"type":"array","maxItems":100,"description":"Original attachment references only. These are not archived receipt files and are never fetched by import.","items":{"type":"object","additionalProperties":false,"required":["name","url"],"properties":{"name":{"type":["string","null"],"maxLength":300},"url":{"type":"string","maxLength":4096,"description":"HTTPS URL or stable notion-file-block reference."}}}},"original_properties":{"type":"object","additionalProperties":true,"description":"Complete original property snapshot, including raw amount and unknown fields. Maximum JSON depth 16; total request at most 128 KiB."},"provider_details":{"type":["object","null"],"additionalProperties":false,"required":["page_id","page_url","name"],"properties":{"page_id":{"type":["string","null"],"description":"Notion UUID, hyphenated or compact. Stored in lowercase hyphenated form."},"page_url":{"type":["string","null"],"format":"uri","maxLength":2048},"name":{"type":["string","null"],"maxLength":300}}},"source_hash":{"type":"string","pattern":"^[0-9a-f]{64}$"},"imported_by":{"type":"string"},"imported_at":{"type":"string","format":"date-time"}},"description":"Immutable historical evidence. Source status/payer are not native approval, reimbursement or an XAuth identity. Importer attribution is recorded separately."},"sender_email":{"type":["string","null"]},"document":{"oneOf":[{"type":"object","required":["document_id","filename","mime_type","byte_size","extraction"],"properties":{"document_id":{"type":"string"},"filename":{"type":"string"},"mime_type":{"type":"string"},"byte_size":{"type":"integer"},"extraction":{"oneOf":[{"$ref":"#/components/schemas/Extraction"},{"type":"null"}],"description":"null: never read (stored before reading existed)."}}},{"type":"null"}]},"spender_user_guid":{"type":["string","null"]},"created_at":{"type":"string","format":"date-time"},"updated_at":{"type":"string","format":"date-time"},"approved_by":{"type":["string","null"]},"approved_at":{"type":["string","null"],"format":"date-time"},"rejected_by":{"type":["string","null"]},"rejected_at":{"type":["string","null"],"format":"date-time"},"rejection_reason":{"type":["string","null"]}}},"Extraction":{"type":"object","description":"What Workers AI read from the receipt: suggestions for a person to apply, never the expense's own values. A value that failed its check (a date that is not YYYY-MM-DD, a currency that is not three upper-case letters, an amount that is not a decimal with at most two places) is null — never guessed. A reading cut off before it finished is shown as failed.","required":["status","merchant_name","merchant_tax_id","transaction_date","currency","amount_cents","tax_amount_cents","error"],"properties":{"status":{"type":"string","enum":["pending","done","failed"]},"merchant_name":{"type":["string","null"]},"merchant_tax_id":{"type":["string","null"]},"transaction_date":{"type":["string","null"],"format":"date"},"currency":{"type":["string","null"],"pattern":"^[A-Z]{3}$"},"amount_cents":{"type":["integer","null"]},"tax_amount_cents":{"type":["integer","null"]},"error":{"type":["string","null"],"description":"Why the reading failed."}}},"ExpenseResponse":{"type":"object","required":["expense"],"properties":{"expense":{"$ref":"#/components/schemas/Expense"}}},"ExpenseList":{"type":"object","required":["expenses","next_cursor"],"properties":{"expenses":{"type":"array","items":{"$ref":"#/components/schemas/Expense"}},"next_cursor":{"type":["string","null"]}}},"Summary":{"type":"object","required":["totals","unattributed_count","needs_review_count"],"properties":{"totals":{"type":"array","items":{"type":"object","required":["status","currency","count","amount_cents","tax_amount_cents"],"properties":{"status":{"type":"string","enum":["needs_review","ready","approved","rejected"]},"currency":{"type":"string"},"count":{"type":"integer"},"amount_cents":{"type":"integer"},"tax_amount_cents":{"type":["integer","null"],"description":"The sum of the known tax amounts; null when none is known."}}}},"unattributed_count":{"type":"integer"},"needs_review_count":{"type":"integer"}}},"Sender":{"type":"object","required":["email","entity_id","project_code","category","status","verified_at","created_by","created_at","pending"],"properties":{"email":{"type":"string"},"entity_id":{"type":["string","null"]},"project_code":{"type":["string","null"]},"category":{"type":["string","null"]},"status":{"type":"string","enum":["verified","pending"]},"verified_at":{"type":["string","null"],"format":"date-time"},"created_by":{"type":"string","description":"The user guid that asked for the verification."},"created_at":{"type":"string","format":"date-time"},"pending":{"oneOf":[{"type":"object","required":["verification_id","expires_at","sent_at"],"properties":{"verification_id":{"type":"string"},"expires_at":{"type":"string","format":"date-time"},"sent_at":{"type":"string","format":"date-time"}}},{"type":"null"}]}}},"SenderCreate":{"type":"object","required":["email"],"additionalProperties":false,"properties":{"email":{"type":"string","format":"email"},"entity_id":{"type":["string","null"],"description":"An active entity of the workspace; null is unattributed."},"project_code":{"type":["string","null"],"maxLength":64},"category":{"type":["string","null"],"maxLength":100}}},"SenderCreated":{"type":"object","required":["sender","sent","dispatch_id"],"properties":{"sender":{"$ref":"#/components/schemas/Sender"},"sent":{"const":true},"dispatch_id":{"type":["string","null"],"description":"XChange's id for the message."}}},"Adapter":{"type":"object","required":["id","kind","displayName","description","available","configSchema"],"properties":{"id":{"type":"string","enum":["xmail","dropbox"]},"kind":{"const":"push"},"displayName":{"type":"string"},"description":{"type":"string"},"available":{"type":"boolean"},"configSchema":{"type":"object"}}},"Source":{"type":"object","required":["source_id","adapter_id","name","config","created_at"],"properties":{"source_id":{"type":"string"},"adapter_id":{"type":"string"},"name":{"type":"string"},"config":{"type":"object"},"created_at":{"type":"string","format":"date-time"}}},"SourceCreate":{"type":"object","required":["adapter_id","name"],"additionalProperties":false,"properties":{"adapter_id":{"type":"string","enum":["xmail","dropbox"]},"name":{"type":"string","maxLength":100},"config":{"type":"object"}}},"SourceCreated":{"type":"object","required":["source","hook_url"],"properties":{"source":{"$ref":"#/components/schemas/Source"},"hook_url":{"type":"string","format":"uri","description":"Shown once."}}}}}}