HMRC API Design
OAuth routes
GET /api/hmrc/userRestrictedAuth/authoriseredirects the user to the HMRC sandbox authorisation page with the configured self-assessment read and write scopes.GET /api/hmrc/userRestrictedAuth/callbackvalidates the one-time state and signed-in Auth0 subject, exchanges each new code, stores encrypted tokens against the app user's database row, and redirects to the account page.GET /api/hmrc/userRestrictedAuth/refreshchecks the stored access-token expiry and refreshes when needed. User-restricted HMRC routes use the same token helper automatically.
Application sign-in uses a separate Auth0 integration; it is not the HMRC OAuth flow. HMRC tokens are not stored in browser cookies or returned to the browser. The callback also provisions the app user idempotently using the Auth0 sub.
Business and obligation routes
These are POST routes that receive request data from the app and call corresponding HMRC sandbox resources:
/api/hmrc/userRestrictedAuth/businessList/api/hmrc/userRestrictedAuth/businessDetails/api/hmrc/userRestrictedAuth/obligations/quarterly/api/hmrc/userRestrictedAuth/obligations/yearEnd/api/hmrc/userRestrictedAuth/quarterlyUpdateHistory/api/hmrc/userRestrictedAuth/listSelfAssessmentTaxCalculations/api/hmrc/userRestrictedAuth/retrieveSelfAssessmentTaxCalculation/api/hmrc/userRestrictedAuth/individualTaxCalculation
The route handlers validate request JSON, NINOs, business IDs, tax years, and calculation identifiers before calling HMRC. Successful HMRC responses are checked against the required response fields for Business Details v2.0, Obligations v3.0, and Individual Calculations v9.0; malformed success responses produce a safe 502 response. HMRC error status codes are preserved, with error codes and messages returned after removing request identifiers and credentials. Server logs contain status, correlation ID, and error codes only, not upstream response bodies or customer data.
The quarterly update history route treats the documented Sandbox MATCHING_RESOURCE_NOT_FOUND summary/detail responses as no data and NO_OBLIGATIONS_FOUND as an empty obligations list. Other unsuccessful summary, obligations, or period-detail responses preserve HMRC's status and error details instead of returning partial history.
The calculation-list route calls the Sandbox Individual Calculations v9.0 endpoint GET /individuals/calculations/{nino}/self-assessment/{taxYear} with Accept: application/vnd.hmrc.9.0+json. HMRC's optional calculationType query filter is omitted when listing all calculations.
The quarterly and year-end obligations routes use HMRC Sandbox's Gov-Test-Scenario: DYNAMIC so simulated dates respond to the selected tax year. The quarterly dynamic scenario requires HMRC's documented test business IDs. This is Sandbox-only and must not be sent to a Production API. The quarterly update history route continues to use its existing Sandbox scenario.
The API versions currently used by the HMRC routes, their Sandbox status, and the pre-release changelog review procedure are tracked in api-versions.md.
Submission routes
POST /api/hmrc/userRestrictedAuth/submitQuarterlyUpdatevalidates dates and tax-year-specific income/expense fields, then submits either a period update or a cumulative update according to the tax year.taxTakenOffTradingIncomeis sent from tax year 2023–24 onward; earlier tax years omit it.POST /api/hmrc/userRestrictedAuth/annualSubmissionacceptsaction: "retrieve"oraction: "submit"for a self-employment annual summary.POST /api/hmrc/userRestrictedAuth/individualTaxCalculationtriggers an HMRC Self Assessment calculation and returns its calculation ID. The quarterly page requests an in-year calculation; the year-end page requests an intent-to-finalise calculation. Neither action submits a final declaration.
These routes call HMRC's test API. The annual summary route does not implement a complete Self Assessment return or final declaration.
Digital record routes
These are same-origin application routes; they do not call HMRC directly. Record businesses are synchronized from the authenticated user's valid HMRC business-list response. A client cannot create a record against a free-form HMRC business ID.
GET /api/records/businesseslists the account's active synchronized HMRC businesses.?includeInactive=truealso includes inactive businesses for export; theisActivefield distinguishes them and inactive businesses cannot receive new records.GET /api/recordslists active records withbusinessId,fromDate,toDate,limit, andoffsetfilters.POST /api/recordscreates a record for a verified business.GET /api/records/{recordId}reads a record.PATCHupdates it andDELETEarchives it; both writes require the current version in anIf-Matchheader. Each successful create, update, and archive adds a versioned history entry.GET /api/records/{recordId}/historyreturns paginated record history and linked submission metadata. UseoffsetandsubmissionOffsetindependently with the returnednextOffsetandnextSubmissionOffset; each page contains at most 100 entries. Submission links are populated only when the submission snapshot integration is implemented.POST /api/records/importacceptsmode: "preview"ormode: "commit". Preview requires a CSV column map and returns validated rows, row counts, existing-duplicate warnings, a batch ID, and a content hash. Commit must submit the same content hash; invalid rows are rejected as a whole, possible existing duplicates require confirmation, and retrying an already committed batch is idempotent.GET /api/records/import/{batchId}reviews its row outcomes;DELETEwith{ "confirm": true }archives unchanged records from an unwanted import and reports records it skipped because they were edited or already archived.GET /api/records/exportstreams CSV with record history and submission-reference metadata from a read-only repeatable-read database snapshot. It includes archived records and supports a business ID combined with either a UK tax year or a date range. A filtered export is not a complete account export. The transaction holds a database connection until the download finishes or is cancelled.
The digital-records migration has been applied to the linked Supabase project. Submission references remain empty until record-to-quarterly-submission snapshots are implemented. See the CSV guide for file formats, categories, example mapping, and the current API-only workflow.
Test and error behavior
POST /api/hmrc/applicationRestrictedAuth/createTestUserrequests an HMRC Sandbox individual test user using Create Test User v1.0. The services-list action uses the same application-restricted API. Both call onlytest-api.service.hmrc.gov.ukwith an application token and require a signed-in email inTESTING_ALLOWED_USER_EMAILS(comma-separated). Production-mode deployments also require the server-onlyHMRC_ENABLE_SANDBOX_TEST_TOOLS=trueopt-in; it defaults to disabled. Configure these in the Pi's protected PM2 environment and restart the process with its updated environment. Do not expose the opt-in or credentials to the browser. The returned test-user password is sensitive; store it securely and do not share it.POST /api/hmrc/fraud/testforwards fraud-prevention headers to HMRC Sandbox'sGET /test/fraud-prevention-headers/validateendpoint.- HMRC errors are returned with an error message and, where available, the upstream status. Network failures are handled by the route, but response shapes are not yet a versioned public contract.
app/api/hmrc/userRestrictedAuth/helper.ts is shared route code, not an endpoint: only files named route.ts expose App Router routes.
