Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
Avalara Integration Foundation — Implementation Plan
Overview
- Design Spec:
.ai/specs/2026-07-04-avalara-integration-foundation.md(status: in-progress; one open question gates Task 5 internals only) - Tracking issue: crbnos/carbon#1061
- Tasks: 11 tasks + gated verification
- Branch:
feature/avalara-integration-foundation
Ground rules for the executor:
- No DB schema changes beyond one idempotent seed row — do NOT run
pnpm run generate:typesexpecting changes, and never commit a regeneratedpackages/database/src/types.tsdiff. - Do NOT rebuild the database; apply the seed migration with
pnpm db:migrateonly, when the local stack is up. - Typecheck per package (
pnpm --filter @carbon/ee typecheck,--filter @carbon/erp typecheck) — whole-repo typecheck OOMs. config.tsxmust stay browser-safe: no imports ofservice.server.ts,hooks.server.ts, or the client (seepackages/ee/AGENTS.md"Never").- Never log or flash the license key; error messages include taxonomy
kind+ Avalara message only. - Commit only at the marked checkpoints, after verification passes (check-and-commit gate).
Dependencies
Task 1 (env vars) ──┬─ Task 3 (client core) ─┬─ Task 4 (avatax surface) ─┬─ Task 6 (hooks + service seam)
│ └─ Task 5 (einvoicing surface — OQ-gated)
└─ Task 2 (config.tsx registry) ────────────────────┘
Task 7 (seed migration) — independent; needed before browser verification
Task 8 (API routes + dynamic options) — after Task 6
Task 9 (unit tests + sandbox smoke) — after Tasks 4–5
Task 10 (docs/rules) — after Task 8
Task 11 (validation gate) — last
Task 1: Env plumbing — AVALARA_ACCOUNT_ID / AVALARA_LICENSE_KEY
Files:
- Modify:
packages/env/src/index.ts - Modify:
.env.example(repo root; also checkapps/erp/.env.exampleif present)
Steps:
- Next to the
XERO_CLIENT_IDblock (~line 313), add:
export const AVALARA_ACCOUNT_ID = getEnv("AVALARA_ACCOUNT_ID", {
isRequired: false
});
export const AVALARA_LICENSE_KEY = getEnv("AVALARA_LICENSE_KEY", {
isRequired: false,
isSecret: true
});
- Add
AVALARA_ACCOUNT_IDto the same server-env aggregation object that listsXERO_CLIENT_ID(~line 436) if that object gates availability; do NOT add the license key to anything client-visible. - Add both to the
ProcessEnvinterface declaration at the top of the file (whereXERO_CLIENT_ID: stringis declared, ~line 23). - Trace how
XERO_CLIENT_IDreaches@carbon/auth(the Xeroconfig.tsximports it from there) — mirror the export for the two Avalara vars. - Document both in
.env.examplewith a comment:# Avalara (tax + e-invoicing) — integration hidden unless both set.
Verification: pnpm --filter @carbon/env typecheck && pnpm --filter @carbon/auth typecheck
Task 2: Registry entry — packages/ee/src/avalara/config.tsx
Files:
- Create:
packages/ee/src/avalara/config.tsx - Modify:
packages/ee/src/index.ts(import +integrationsarray + re-exportAvalara)
Steps:
- Model on
packages/ee/src/xero/config.tsx.defineIntegration({ id: "avalara", name: "Avalara", category: "Tax & Compliance", active: !!AVALARA_ACCOUNT_ID && !!AVALARA_LICENSE_KEY, logo: Logo, images: [], ... })— nooauthblock (Basic auth is env-side). Import env vars from the same source Xero uses (@carbon/auth). AvalaraSettingsSchema(zod, reuse the file-localcoerceBooleanpattern from Xero's config):companyCode: z.string().min(1)environment: z.enum(["sandbox", "production"]).default("sandbox")taxDetermination: coerceBoolean.optional().default(false)eInvoicing: coerceBoolean.optional().default(false)avalaraCompanyId: z.coerce.number().optional()(not a form field; written by install hook)
settingGroups:Connection("Which Avalara company and environment this Carbon company uses") andFeatures("Enable the Avalara capabilities this company consumes").settings:companyCode(typeoptions,listOptions: []— populated dynamically, Xero account-code precedent;required: true),environment(typeoptionswith sandbox/production descriptions, defaultsandbox),taxDetermination+eInvoicing(typeswitch, defaultfalse, descriptions pointing at #1044/#1054 features).actions:[{ id: "test-connection", label: "Test Connection", description: "Verify credentials and company code against Avalara", endpoint: "/api/integrations/avalara/test" }].- Inline
LogoSVG component (simple monochrome "A" mark placeholder is fine; note for design pass). - Register in
packages/ee/src/index.ts: import, add tointegrationsarray (alphabetical — beforeEmail), addexport { Avalara } from "./avalara/config";.
Verification: pnpm --filter @carbon/ee typecheck
Task 3: Client core — auth, base URLs, retry/timeout, error taxonomy
Files:
- Create:
packages/ee/src/avalara/lib/client.ts - Create:
packages/ee/src/avalara/lib/types.ts
Steps:
types.ts:namespace Avalararequest/response models —PingResult,CompanyModel,TransactionModel(withdetails[]: jurisdiction, rate, tax, taxableAmount — the shape the tax spec maps totaxLedger),AddressResolutionModel,NexusModel,ErrorBody(error.code,error.details[]), e-invoicingDocumentSubmitResponse,DocumentStatusEvent,Mandate. Keep fields to what the spec's consumers need; do not transcribe the whole AvaTax swagger.client.ts:export class AvalaraError extends Error { kind: "auth" | "validation" | "not_found" | "conflict" | "rate_limit" | "transient"; status?: number; avalaraCode?: string; retryable: boolean; details?: unknown }+toAvalaraError(status, body)mapper per the spec's taxonomy table (409 and committed-document conflicts →conflict).- Base-URL maps:
AVATAX_BASE = { sandbox: "https://sandbox-rest.avatax.com", production: "https://rest.avatax.com" };EINVOICING_BASE = { sandbox: "https://api.sbx.avalara.com/einvoicing", production: "https://api.avalara.com/einvoicing" }. AvalaraHttpcore: constructor takes{ environment, accountId, licenseKey };request<T>(surface: "avatax" | "einvoicing", method, path, { body?, timeoutMs?, retryable? })→{ data: T | null, error: AvalaraError | null }. Basic auth header for avatax;X-Avalara-Client: Carbon; 1.0; REST; v2; carbonon avatax calls;avalara-versionheader on einvoicing calls;AbortSignal.timeout(timeoutMs ?? 10_000).- Retry loop: max 2 retries with exponential backoff + jitter on
rate_limit(honorRetry-Afterseconds) andtransient, only when the call is markedretryable(default true for GET, false for POSTs unless the method opts in). Do NOT reuseaccounting/core/utils.ts'sHTTPClient(no retry/timeout; throwsRatelimitErrorfor Inngest semantics we don't want here) — but keep the same{ data, error }return philosophy.
Verification: pnpm --filter @carbon/ee typecheck
Task 4: AvaTax surface
Files:
- Create:
packages/ee/src/avalara/lib/avatax.ts - Create:
packages/ee/src/avalara/index.ts(barrel: client, surfaces, types, schema type) - Modify:
packages/ee/package.json(exports:"./avalara": "./src/avalara/index.ts")
Steps:
AvataxApi(constructed with anAvalaraHttp+companyCode):ping()→GET /api/v2/utilities/ping; returns{ authenticated: boolean, version }— treatauthenticated: falseasAvalaraError(kind: "auth").listCompanies()→GET /api/v2/companies?$top=200;getCompanyByCode(code)→ same with$filter=companyCode eq '<escaped>', returns the single match ornot_found.createTransaction(model)→POST /api/v2/transactions/create(30s timeout;retryable: falsewhenmodel.commit === true— document the caller idempotent-codecontract from the tax spec in the JSDoc).commitTransaction(transactionCode)→POST /api/v2/companies/{companyCode}/transactions/{transactionCode}/commitbody{ commit: true }.voidTransaction(transactionCode, reason = "DocVoided")→POST .../voidbody{ code: reason }.resolveAddress(address)→POST /api/v2/addresses/resolve.listNexus(avalaraCompanyId)→GET /api/v2/companies/{avalaraCompanyId}/nexus.
- Verify each path/verb against the current AvaTax REST v2 docs (developer.avalara.com) before finalizing — record any corrections in the spec changelog.
Verification: pnpm --filter @carbon/ee typecheck
Task 5: E-invoicing surface (OQ-gated auth)
Files:
- Create:
packages/ee/src/avalara/lib/einvoicing.ts
Steps:
- FIRST: resolve the spec's open question against current Avalara e-invoicing (ELR) API docs: auth mechanism (Avalara Identity OAuth2 client-credentials vs license key) and current
avalara-versionvalue. If OAuth2 is confirmed: pause, get Brad's approval to addAVALARA_CLIENT_ID/AVALARA_CLIENT_SECRET(extends Task 1), implement a token-cachingAuthStrategyinclient.ts; tick the spec checkbox with the resolution. EinvoicingApi:submitDocument(payload, meta)→POST /documents;getDocumentStatus(documentId)→GET /documents/{id}/status;listDocuments(query);listMandates()→GET /mandates. Verify exact paths in the same docs pass.- If the OQ answer is delayed, ship the method contract with a
not_configuredAvalaraErrorwhen e-invoicing creds are absent so the e-invoicing spec (.ai/specs/2026-07-04-e-invoicing.md) can compile against the interface — do NOT block Tasks 6–11.
Verification: pnpm --filter @carbon/ee typecheck
Task 6: Hooks + consumer seam
Files:
- Create:
packages/ee/src/avalara/service.server.ts - Create:
packages/ee/src/avalara/hooks.server.ts - Modify:
packages/ee/src/hooks.server.ts(registeravalara) - Modify:
packages/ee/package.json(exports:"./avalara/hooks.server": "./src/avalara/hooks.server.ts")
Steps:
service.server.ts(spec §Consumer contract — the ONLY entry point #1044/#1054 use):getAvalaraConfig(client, companyId)— readcompanyIntegrationrowid = 'avalara', parse metadata withAvalaraSettingsSchema(safeParse; parse failure → error), includeinstalled = active.getAvalaraClient(client, companyId)— env creds present + installed + parsed config →{ data: { avatax: AvataxApi, einvoicing: EinvoicingApi, config } }; else descriptive error ("Avalara is not configured"/"not installed for this company").isAvalaraFeatureEnabled(client, companyId, feature)— false on any error path, never throws.
hooks.server.ts(model:packages/ee/src/xero/hooks.server.ts, usinggetCarbonServiceRole()):avalaraHealthcheck(companyId, metadata): parse metadata; build client from env + metadata directly (healthcheck receives metadata — don't re-read the row);ping()authenticated ANDgetCompanyByCode(companyCode)resolves → true; any error → false (log kind, never the key).avalaraOnInstall(companyId): best-effort resolvecompanyCode→ writeavalaraCompanyIdintocompanyIntegration.metadata(merge, don't clobber). No event-system subscriptions (spec decision).avalaraOnUninstall(companyId): delete the Redis health keyintegrations:{companyId}:avalara:healthif the redis client is importable here; otherwise no-op with comment (cache self-expires in 5 min).
- Register in
packages/ee/src/hooks.server.tsserverHooksmap:avalara: { onHealthcheck, onInstall, onUninstall }.
Verification: pnpm --filter @carbon/ee typecheck
COMMIT CHECKPOINT 1: feat(ee): avalara integration foundation — client, registry entry, lifecycle hooks
Task 7: Seed migration
Files:
- Create via
pnpm db:migrate:new avalara-integration→packages/database/supabase/migrations/<ts>_avalara-integration.sql
Steps:
- Contents (Jira precedent
20260215000000_add_jira_integration.sql, plus idempotency guard):
INSERT INTO "integration" ("id", "jsonschema")
VALUES ('avalara', '{"type": "object", "properties": {}}'::json)
ON CONFLICT ("id") DO NOTHING;
- Confirm
integration.idis the PK/unique target (read20240119095150_integrations.sql); adjust the conflict target if needed. - Apply only when the local stack is up:
pnpm db:migrate. No type regeneration expected;git statusmust show nopackages/database/src/types.tsdiff.
Verification: psql ... -c "SELECT id FROM \"integration\" WHERE id = 'avalara'" returns one row; re-running the migration file is a no-op.
Task 8: API routes + dynamic company options
Files:
- Create:
apps/erp/app/routes/api+/integrations.avalara.test.ts - Create:
apps/erp/app/routes/api+/integrations.avalara.companies.ts - Modify:
apps/erp/app/modules/settings/settings.server.ts(dynamic options wiring)
Steps:
integrations.avalara.test.ts— action,requirePermissions(request, { update: "settings" }):getAvalaraClient→ping()+getCompanyByCode()uncached; flash successConnected to Avalara — company "<name>" (<environment>)or the taxonomy-mapped failure (auth→ "Invalid Avalara credentials",not_found→ "Company code not found",transient→ "Avalara unreachable"); redirect back topath.to.integration("avalara")(check the exact path helper used by the Xero backfill route and mirror it).integrations.avalara.companies.ts— loader, same permissions:listCompanies()→[{ value: companyCode, label: name }].- In
settings.server.ts, where the Xero integration branch populates dynamiclistOptions(thedynamicOptionspath feedingintegrations.$id.tsx), add theavalarabranch: when env creds present, populatecompanyCodeoptions fromlistCompanies(); on error leave options empty (field degrades to free entry only if the renderer supports it — otherwise show empty list; do not crash the page). - Grep
path.tofor how existingapi+/integrations.*endpoints are referenced from configactions(Xero's is the literal string/api/integrations/xero/backfill) — keep the literal-string convention.
Verification: pnpm --filter @carbon/erp typecheck
Task 9: Tests — unit + sandbox smoke
Files:
- Create:
packages/ee/src/avalara/lib/client.test.ts - Create:
packages/ee/src/avalara/lib/avatax.test.ts
Steps:
- Check how existing
@carbon/eevitest suites stubfetch(grepvi.stubGlobal("fetch"/mswunderpackages/ee); follow that pattern. client.test.ts: Basic auth +X-Avalara-Clientheaders present; 429 withRetry-After: 1retried then succeeds; 400 with AvaTaxerror.details[]→kind: "validation", no retry, details preserved; 503×3 →kind: "transient"after max retries; abort at timeout budget →transient; POST withretryable: falsenever retried.avatax.test.ts(mocked):createTransaction(SalesOrder)sendstype/companyCode/commit:falseand parsestotalTax+details[];createTransaction(..., commit: true)is marked non-retryable;voidTransactionhits the companies-scoped path;getCompanyByCodeescapes quotes in the$filter.- Sandbox smoke (spec AC): a
describe.skipIf(!process.env.AVALARA_SANDBOX_SMOKE)block running live sandboxping+createTransaction(SalesOrder)+resolveAddress— never runs in CI without the flag; document the flag in the test header.
Verification: pnpm --filter @carbon/ee test — all pass (smoke skipped).
COMMIT CHECKPOINT 2: feat(erp): avalara settings wiring — seed row, test-connection, dynamic company codes, tests
Task 10: Docs + rules
Files:
- Create:
.ai/rules/avalara-integration.md(frontmatterpaths: ["packages/ee/src/avalara/**", "apps/erp/app/routes/api+/integrations.avalara.*"]) - Modify:
packages/ee/AGENTS.md(exports list + key patterns) - Modify:
AGENTS.md(Task Router → Integrations:Avalara (tax + e-invoicing substrate)row)
Steps:
- Rule file: package layout, consumer contract (
getAvalaraClient/isAvalaraFeatureEnabled— consumers never readcompanyIntegrationdirectly), error taxonomy table, env vars, sandbox/production switching, the commit-idempotency contract, pointers to the three specs (#1044 tax Phase 3, #1054 e-invoicing, this foundation). packages/ee/AGENTS.md: add./avalara+./avalara/hooks.serverto Exports; note "Avalara is NOT an accounting-sync provider — no syncers, noexternalIntegrationMapping".
Task 11: Validation + gated browser verification
Steps:
pnpm --filter @carbon/ee typecheck pnpm --filter @carbon/erp typecheck pnpm --filter @carbon/env typecheck pnpm --filter @carbon/ee test pnpm run lint # Expected: all pass- Browser verify (requires user go-ahead, stack up, sandbox env vars set;
/auth+/testskills):- Env unset → no Avalara card in
/x/settings/integrations; env set → card under Tax & Compliance. - Install flow: company-code dropdown lists sandbox companies; save persists metadata (inspect row — NO secrets); health badge goes green; Test Connection flashes company + environment.
- Break the license key in env → healthcheck red + Test Connection shows the
authmessage. - Toggle
taxDeterminationon and confirm via a scratch loader/console thatisAvalaraFeatureEnabledflips. - Screenshot the card + Test Connection result for the PR.
- Env unset → no Avalara card in
- Update the spec: tick acceptance criteria as verified; changelog entry; resolve/annotate the e-invoicing-auth open question with the Task 5 outcome.
COMMIT CHECKPOINT 3: docs(ai): avalara integration rule + agents updates — PR references Tracking spec: .ai/specs/2026-07-04-avalara-integration-foundation.md + Closes crbnos/carbon#1061.
Deferred (explicit non-goals, per spec)
- AvaTax determination connector (#1044 Phase 3:
AvalaraTaxEngine,resolveLineTaxesdispatch,taxLedgermapping, outage fallback/reconcile job, nexus-drift UI). - E-invoicing flows (#1054: document building, submission orchestration, status jobs, mandate UX).
- Per-company secret storage (Vault/AES-GCM) — nothing secret per company in v1; Plaid precedent reserved.
- Custom Avalara logo/design pass; category grouping polish if "Tax & Compliance" renders oddly in the integrations list.