1**Example Hono-Zod-OpenAPI app with a Fiberplane API explorer.**
2
3> For an example with regular-old Hono, see: https://www.val.town/v/fiberplane/fiberplaneHonoStarter
8 ```
9
102. Expose your OpenAPI spec
11 ```ts
12 app.doc("/doc", {
13 openapi: "3.0.0",
14 info: {
15 title: "User Management API",
16 version: "v1.0.0",
17 },
19 ```
20
213. Mount the api explorer
22
23 This will mount it at the root `/*`, but you can mount it to another route, like `/fp/*` if you
24 are using `/` for your main app. We recommend `/` if your Hono app is an API without a frontend.
25
26 ```ts
28 "/*",
29 createFiberplane({
30 openapi: { url: "/doc" },
31 }),
32 );
33 ```
34
354. Visit your Val's root route to play with the API explorer!
36
37## How it Works
38
39`createFiberplane` mounts Fiberpalne at the root route (`/`), which can be used to explore the api's routes and make requests.
40Think of it like an embedded, lightweight postman.
12app.get("./static/**/*", c => serveFile(c.req.path, import.meta.url));
13
14// Add your API routes here
15// app.get("/api/data", c => c.json({ hello: "world" }));
16
17// Unwrap and rethrow Hono errors as the original error
8## Hono
9
10This app uses [Hono](https://hono.dev/) has the a API framework. You can think of Hono as a replacement for [ExpressJS](https://expressjs.com/) that works in serverless environments like Val Town or Cloudflare Workers. If you come from Python or Ruby, Hono is also a lot like [Flask](https://github.com/pallets/flask) or [Sinatra](https://github.com/sinatra/sinatra), respectively.
11
12## Serving assets to the frontend
20### `index.html`
21
22The most complicated part of this backend API is serving index.html. In this app (like most apps) we serve it at the root, ie `GET /`.
23
24We *bootstrap* `index.html` with some initial data from the server, so that it gets dynamically injected JSON data without having to make another round-trip request to the server to get that data on the frontend. This is a common pattern for client-side rendered apps.
25
26## CRUD API Routes
27
28This app has two CRUD API routes: for reading and inserting into the messages table. They both speak JSON, which is standard. They import their functions from `/backend/database/queries.ts`. This routes are called from the React app to refresh and update data.
29
30## Errors
31
32Hono and other API frameworks have a habit of swallowing up Errors. We turn off this default behavior by re-throwing errors, because we think most of the time you'll want to see the full stack trace instead of merely "Internal Server Error". You can customize how you want errors to appear.
1# Research Agent over email
2
3This is a lightweight wrapper around Perplexity's web search API that you can interact with via email.
16 );
17
18 const client = new OpenAI({ apiKey: process.env.PERPLEXITY_API_KEY, baseURL: "https://api.perplexity.ai" });
19 const response = await client.chat.completions.create({
20 model: "sonar",
9
101. Add ability for users to input their own username
112. Add ability for users to input their API token so they can see private projects
7## Hono
8
9This app uses [Hono](https://hono.dev/) as the API framework. You can think of Hono as a replacement for [ExpressJS](https://expressjs.com/) that works in serverless environments like Val Town or Cloudflare Workers. If you come from Python or Ruby, Hono is also a lot like [Flask](https://github.com/pallets/flask) or [Sinatra](https://github.com/sinatra/sinatra), respectively.
10
11## Serving assets to the frontend
19### `index.html`
20
21The most complicated part of this backend API is serving index.html. In this app (like most apps) we serve it at the root, ie `GET /`.
22
23We *bootstrap* `index.html` with some initial data from the server, so that it gets dynamically injected JSON data without having to make another round-trip request to the server to get that data on the frontend. This is a common pattern for client-side rendered apps.
24
25## CRUD API Routes
26
27This app has two CRUD API routes: for reading and inserting into the messages table. They both speak JSON, which is standard. They import their functions from `/backend/database/queries.ts`. These routes are called from the React app to refresh and update data.
28
29## Errors
30
31Hono and other API frameworks have a habit of swallowing up Errors. We turn off this default behavior by re-throwing errors, because we think most of the time you'll want to see the full stack trace instead of merely "Internal Server Error". You can customize how you want errors to appear.
57
58 try {
59 // Make the API call to OpenAI
60 const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY");
61
62 const response = await fetch("https://api.openai.com/v1/chat/completions", {
63 method: "POST",
64 headers: {
65 "Content-Type": "application/json",
66 "Authorization": `Bearer ${OPENAI_API_KEY}`,
67 },
68 body: JSON.stringify({
85 if (!response.ok) {
86 const errorData = await response.json();
87 console.error("OpenAI API error:", errorData);
88 throw new Error(`OpenAI API error: ${errorData.error?.message || "Unknown error"}`);
89 }
90
24 setIsLoading(true);
25 setError(null);
26 const response = await fetch("/api/reviews");
27 if (!response.ok) {
28 throw new Error(`HTTP error! status: ${response.status}`);
41 e.preventDefault();
42 try {
43 const response = await fetch("/api/submit-review", {
44 method: "POST",
45 headers: { "Content-Type": "application/json" },
64 setIsAnalyzing(true);
65 setError(null);
66 const response = await fetch("/api/analyze-reviews", { method: "POST" });
67 if (!response.ok) {
68 throw new Error(`HTTP error! status: ${response.status}`);
82 setIsClearing(true);
83 setError(null);
84 const response = await fetch("/api/clear-reviews", { method: "POST" });
85 if (!response.ok) {
86 throw new Error(`HTTP error! status: ${response.status}`);
37
38// Provide an endpoint for the frontend to get reviews as JSON
39app.get("/api/reviews", async (c) => {
40 const reviews = await getReviews();
41 return c.json({ reviews });
43
44// Provide an endpoint for the frontend to insert a new review
45app.post("/api/submit-review", async (c) => {
46 const { name, cake1Review, cake2Review } = await c.req.json();
47 if (
56
57// Provide an endpoint for analyzing reviews
58app.post("/api/analyze-reviews", async (c) => {
59 const winner = await analyzeReviews();
60 return c.json({ winner });
62
63// Provide an endpoint for clearing all reviews
64app.post("/api/clear-reviews", async (c) => {
65 await clearReviews();
66 return c.json({ success: true });