VoltAgent Custom Endpoints Example
Learn how to extend the VoltAgent API server with your own custom REST endpoints.
Learn how to extend the VoltAgent API server with your own custom REST endpoints.
This example shows how to create custom API endpoints with VoltAgent's server-hono package. You can extend the VoltAgent API server with your own business logic, data endpoints, or integration points.
npm create voltagent-app@latest -- --example with-custom-endpointsThis example includes 4 simple endpoints:
GET /api/health- Health checkGET /api/hello/:name- Personalized greeting with URL parametersPOST /api/calculate- Simple calculator with JSON bodyDELETE /api/delete-all- Delete all data example
Custom endpoints are registered through the configureApp callback, which gives you full access to the Hono app instance:
import { honoServer } from "@voltagent/server-hono";
import { jwtAuth } from "@voltagent/server-core";
new VoltAgent({
agents: { agent },
server: honoServer({
port: 3141,
// Optional: Configure CORS
cors: {
origin: "https://your-domain.com",
credentials: true,
},
// Optional: Add authentication
auth: jwtAuth({
secret: process.env.JWT_SECRET,
defaultPrivate: true, // Protect all routes by default
publicRoutes: ["GET /api/health"], // Except these
}),
configureApp: (app) => {
// Add custom routes with full Hono API
// Note: Routes are automatically protected when auth is configured with defaultPrivate: true
app.get("/api/health", (c) => c.json({ status: "ok" }));
// Use route parameters
app.get("/api/user/:id", (c) => {
const id = c.req.param("id");
return c.json({ userId: id });
});
// Add middleware
app.use("/api/admin/*", authMiddleware);
// Use route groups
const api = app.basePath("/api/v2");
api.get("/users", getUsersHandler);
},
}),
});With configureApp, you get:
- Full access to Hono's routing API
- Type-safe route handlers with proper IntelliSense
- Ability to use middleware and route groups
- Support for all Hono features (OpenAPI, validation, etc.)
- Automatic auth protection when
authis configured (routes are registered AFTER auth middleware)
- Install dependencies:
npm install- Create
.envfile:
echo "OPENAI_API_KEY=your_api_key_here" > .envnpm run dev# Health check
curl http://localhost:3141/api/health
# Greeting
curl http://localhost:3141/api/hello/john
# Calculator
curl -X POST http://localhost:3141/api/calculate \
-H "Content-Type: application/json" \
-d '{"a": 10, "b": 5, "operation": "add"}'
# Delete all data
curl -X DELETE http://localhost:3141/api/delete-all
# Admin stats (middleware logs the access)
curl http://localhost:3141/api/admin/statscurl -X POST http://localhost:3141/agents/agent/text \
-H "Content-Type: application/json" \
-d '{"input": "What endpoints are available?"}'- Pass a
configureAppcallback to thehonoServerconfiguration - The callback receives the Hono app instance as a parameter
- Use Hono's native API to register routes, middleware, and plugins
- The server calls your
configureAppfunction after registering built-in routes - Your custom routes are now part of the API server
That's it! 🎉