Hono

03 Route Handler

Return a non-streaming Anvia response from a Hono route.

Validate JSON at the Hono boundary with zValidator(...), then read the typed body with c.req.valid("json").

1. Add /api/support

import { Hono } from "hono";
import { zValidator } from "@hono/zod-validator";
import { z } from "zod";
import { supportAgent } from "./ai/support-agent";

export const app = new Hono();

const SupportRequest = z.object({
  message: z.string().trim().min(1, "message is required"),
});

app.post("/api/support", zValidator("json", SupportRequest), async (c) => {
  const { message } = c.req.valid("json");
  const response = await supportAgent.prompt(message).send();

  return c.json({
    output: response.output,
    usage: response.usage,
    messages: response.messages,
  });
});

2. Call The Route

curl -X POST http://localhost:3000/api/support \
  -H "Content-Type: application/json" \
  -d '{"message":"How long does a reset link last?"}'

3. Return Structured Failures

app.post("/api/support", zValidator("json", SupportRequest), async (c) => {
  try {
    const { message } = c.req.valid("json");
    const response = await supportAgent.prompt(message).send();
    return c.json({ output: response.output });
  } catch (error) {
    console.error(error);
    return c.json({ error: "agent_failed" }, 500);
  }
});

Next

Return live events in Streaming. For prompt response fields, read Prompt Responses.