Skip to Content

First API route

Add a Hono route and call it with the typed client.

Add the route

Chain it next to /api/health. Chaining is what types the client.

api/app.ts
import { Hono } from "hono"

const base = new Hono()

const app = base
  .get("/api/health", (c) => c.json({ ok: true }))
  .get("/api/hello", (c) => c.json({ message: "Hello" }))

Call it

src/lib/api.ts already exports the client. The path and response type come from the route.

src/routes/_app/index.tsx
import { useQuery } from "@tanstack/react-query"
import { api } from "../../lib/api"

const hello = useQuery({
  queryKey: ["hello"],
  queryFn: async () => (await api.hello.$get()).json(),
})

// <p>{hello.data?.message}</p>

Check it

Save. The page shows Hello. Rename the field in the route and bun run typecheck fails in the page.