On this page
DocumentationGuides

Build a server route with the SDK

Use this guide when your application owns the HTTP route and only needs TaxKit for the calculation.

Before you start

  • Use a server-side TypeScript runtime.
  • Install the SDK with Install the SDK.
  • Keep request validation at your application boundary.

Steps

  1. Parse your application request.
  2. Convert accepted fields into canonical TaxKit facts.
  3. Call the SDK helper.
  4. Return the report fields your application needs.

Example

TypeScript
import { Function as EffectFunction, Effect, Schema } from "effect";
import * as HttpServerRequest from "effect/http/HttpServerRequest";
import { AuPayTakeHomeCalculation } from "taxkit/au/effect";
import { PayPeriod, TakeHomeScenarioInputSchema } from "taxkit/au/schemas";
import { Cents, Money } from "taxkit/core";
import { calculateReport } from "taxkit/effect";
import { nonNegative } from "taxkit/money/effect";

class PayPreviewRequestError extends Schema.TaggedError<
  PayPreviewRequestError,
  { readonly brand: unique symbol }
>()("PayPreviewRequestError", { message: Schema.String }) {}

const PayPreviewRequest = Schema.Struct({
  grossPayCents: Cents,
  period: PayPeriod,
  taxFreeThresholdClaimed: Schema.Boolean,
});

const PayPreviewResponse = Schema.Struct({
  netPayCents: Cents,
  withholdingsCents: Cents,
});

export const handlePayPreview = Effect.fn("handlePayPreview")(function* (
  request: Request
) {
  const body = yield* HttpServerRequest.fromWeb(request).text.pipe(
    Effect.flatMap(
      Schema.decodeEffect(Schema.fromJsonString(PayPreviewRequest))
    ),
    Effect.mapError(
      () =>
        new PayPreviewRequestError({
          message: "Invalid pay preview request.",
        })
    )
  );
  const grossPay = yield* EffectFunction.pipe(
    Money.fromCents(body.grossPayCents),
    nonNegative,
    Effect.mapError(
      () =>
        new PayPreviewRequestError({
          message: "Invalid pay preview request.",
        })
    )
  );
  const input = TakeHomeScenarioInputSchema.make({
    grossPay,
    period: body.period,
    taxFreeThresholdClaimed: body.taxFreeThresholdClaimed,
  });
  const report = yield* calculateReport(AuPayTakeHomeCalculation, input);
  const response = yield* Schema.encodeEffect(
    Schema.fromJsonString(PayPreviewResponse)
  )({
    netPayCents: report.netPay.cents,
    withholdingsCents: report.withholdingsTotal.cents,
  });
  return new Response(response, {
    headers: { "content-type": "application/json" },
  });
});

Verify the result

Request body:

JSON
{
  "grossPayCents": 165400,
  "period": "weekly",
  "taxFreeThresholdClaimed": true
}

Expected response:

JSON
{ "netPayCents": 130100, "withholdingsCents": 35300 }

Handle errors

The handler returns an Effect. Your server host owns execution and maps the typed failure channel to its HTTP status and response shape. Invalid JSON, fractional cents and unsupported periods fail request validation before the calculator runs. Provider or request details are not included in the safe request error.