Using TypeScript
Learn how to implement a CommonGrants API in TypeScript using the Hono API template and the CommonGrants TypeScript SDK.
Quickstart
Section titled “Quickstart”This guide walks you through creating a new project from the CommonGrants Hono template, then using the SDK to validate your data and define custom fields.
First steps
Section titled “First steps”Get the Hono template up and running with the following steps:
-
Open the template repository and click Use this template to create your own repository from it.
-
Clone your new repository:
Terminal window git clone https://github.com/<you>/<your-api>.gitcd <your-api> -
Install the dependencies:
Terminal window pnpm install -
Run the project:
Terminal window pnpm run dev -
Open the API docs at
http://localhost:3000/docs. PressCtrl+Cin the terminal to stop the server when you’re done.
Project structure
Section titled “Project structure”The template includes the following files and directories:
- .nvmrc Pins Node.js 24
- PORTING.md How to connect your own data
Directorysrc/
- index.ts Node entrypoint — the only file that knows about Node
- app.ts
createApp({ repository })builds the app - http.ts Shared error shapes
Directoryroutes/
- opportunities.ts Route definitions, handlers, request/response validation
Directorydata/
- schema.ts The one opportunity schema — extend custom fields here
- repository.ts The interface your data source implements: list / get / search
- fixtures.ts The sample-data implementation of that interface
- opportunities.json The bundled sample data
Directorytest/ Vitest suites: fixtures, app, routes, OpenAPI
- …
Next steps
Section titled “Next steps”Once the template is running, connect the API to your data. If your project started from an earlier version of the template, upgrade its SDK first.
Upgrading from an earlier SDK
Section titled “Upgrading from an earlier SDK”The @common-grants/sdk package provides Zod schemas, TypeScript types, and an extension framework that match the current protocol version. The template depends on SDK 0.9. If your project uses an earlier version, upgrade the SDK and Zod, deduplicate Zod so the SDK shares your project’s copy, and run the checks:
pnpm add @common-grants/sdk@^0.9.0 zod@^4.6.6pnpm dedupepnpm run ciSDK 0.9 updates the schemas to protocol v0.5 and is published as ESM only. The template is already an ES module project, so it needs no configuration changes.
The SDK is split into modules, each with its own import path:
| Import path | Description |
|---|---|
@common-grants/sdk/schemas |
Zod schemas for data validation |
@common-grants/sdk/types |
Inferred TypeScript types |
@common-grants/sdk/constants |
Runtime enum constants |
@common-grants/sdk/extensions |
Custom field and plugin framework |
@common-grants/sdk/client |
HTTP client with auth, pagination, and search |
The SDK exposes these modules through package.json exports and has no root entrypoint, so import from the paths above.
Validate data with the SDK
Section titled “Validate data with the SDK”The template and the SDK split the work. The template owns HTTP: routing, request and response validation, error bodies, the OpenAPI document, and the API docs at /docs. The SDK owns the CommonGrants schemas: the template builds its opportunity model, filters, and response envelopes from them, so when you update the SDK, your API picks up that release’s schema changes.
Use the same schemas to validate records from your data source:
import { OpportunityBaseSchema } from "@common-grants/sdk/schemas";import type { OpportunityBase } from "@common-grants/sdk/types";
// rawData is a record from your data source// Throws a ZodError if it doesn't match the protocolconst opportunity: OpportunityBase = OpportunityBaseSchema.parse(rawData);Connect your own data
Section titled “Connect your own data”The template reads the bundled sample data through the OpportunityRepository interface in src/data/repository.ts. The interface has three methods, list, get, and search, behind these routes:
GET /common-grants/opportunitiesGET /common-grants/opportunities/{oppId}POST /common-grants/opportunities/search
To serve your own data:
- Implement
OpportunityRepositoryagainst your database or upstream API, in place of the sample-data implementation,fixtureRepositoryinsrc/data/fixtures.ts. Parse each record with an SDK schema, or map it with a plugin’stoCommon. - Pass your implementation to
createAppinsrc/index.ts.
The routes, validation, and OpenAPI document keep working without changes. The template’s porting guide walks through these steps.
Adding custom fields
Section titled “Adding custom fields”When adopting the CommonGrants protocol, you may need to include information about a funding opportunity that is not explicitly defined by the CommonGrants model for opportunities. The protocol supports this through the customFields property on the OpportunityBase model, and the SDK’s extensions module gives those fields types.
For example, let’s say you need to add a legacyId field to map opportunities to an existing ID system.
Define a plugin
Section titled “Define a plugin”Create a plugin that declares the custom field. The plugin compiles an Opportunity schema with a typed customFields property:
import { z } from "zod";import { definePlugin } from "@common-grants/sdk/extensions";
/** Custom fields for this API's data source */export const myPlugin = definePlugin({ meta: { name: "my-system", sourceSystem: "grants.example.gov", capabilities: ["customFields"], }, schemas: { Opportunity: { customFields: { legacyId: { fieldType: "integer", description: "Maps to the opportunity_id in the legacy system", }, }, }, },} as const);
/** Opportunity schema with typed custom fields */export const OpportunitySchema = myPlugin.schemas.Opportunity.commonSchema;export type Opportunity = z.infer<typeof OpportunitySchema>;The as const is only required when the specs are assigned to a variable before the call, but the SDK keeps it by convention.
Use the plugin’s schema
Section titled “Use the plugin’s schema”Parse your data with the plugin’s schema instead of the base schema, for example in a new file under src/. The custom field is validated and fully typed:
import { OpportunitySchema, type Opportunity } from "./data/plugin.js";
const opportunity: Opportunity = OpportunitySchema.parse({ id: "573525f2-8e15-4405-83fb-e6523511d893", title: "STEM Education Grant Program", description: "Improving STEM education in underserved communities", status: { value: "open" }, createdAt: "2025-01-01T00:00:00Z", lastModifiedAt: "2025-01-15T00:00:00Z", customFields: { legacyId: { name: "legacyId", fieldType: "integer", value: 12345 }, },});
opportunity.customFields?.legacyId?.value; // numberTo serve the custom field from the API, set OpportunitySchema in src/data/schema.ts to the plugin’s schema. Response validation, the repository’s types, and the OpenAPI document all pick it up. A plugin’s custom filters don’t come with it: the template’s search route ignores them and reports them in filterInfo.errors. To support them, update the search route and your repository.
For a one-off extension without a plugin, use withCustomFields() from the same module. See the extensions guide for custom filters, transforms, and publishing plugins.