hono-routing
This skill provides comprehensive knowledge for building type-safe APIs with Hono, focusing on routing patterns, middleware composition, request validation, RPC client/server patterns, error handling, and context management. Use when: building APIs with Hono (any runtime), setting up request validation with Zod/Valibot/Typia/ArkType validators, creating type-safe RPC client/server communication, implementing custom middleware, handling errors with HTTPException, extending Hono context with custom variables, or encountering middleware type inference issues, validation hook confusion, or RPC performance problems. Keywords: hono, hono routing, hono middleware, hono rpc, hono validator, zod validator, valibot validator, type-safe api, hono context, hono error handling, HTTPException, c.req.valid, middleware composition, hono hooks, typed routes, hono client, middleware response not typed, hono validation failed, hono rpc type inference
What this skill does
# Hono Routing & Middleware **Status**: Production Ready ✅ **Last Updated**: 2025-10-22 **Dependencies**: None (framework-agnostic) **Latest Versions**: [email protected], [email protected], [email protected], @hono/[email protected], @hono/[email protected] --- ## Quick Start (15 Minutes) ### 1. Install Hono ```bash npm install [email protected] ``` **Why Hono:** - **Fast**: Built on Web Standards, runs on any JavaScript runtime - **Lightweight**: ~10KB, no dependencies - **Type-safe**: Full TypeScript support with type inference - **Flexible**: Works on Cloudflare Workers, Deno, Bun, Node.js, Vercel ### 2. Create Basic App ```typescript import { Hono } from 'hono' const app = new Hono() app.get('/', (c) => { return c.json({ message: 'Hello Hono!' }) }) export default app ``` **CRITICAL:** - Use `c.json()`, `c.text()`, `c.html()` for responses - Return the response (don't use `res.send()` like Express) - Export app for runtime (Cloudflare Workers, Deno, Bun, Node.js) ### 3. Add Request Validation ```bash npm install [email protected] @hono/[email protected] ``` ```typescript import { zValidator } from '@hono/zod-validator' import { z } from 'zod' const schema = z.object({ name: z.string(), age: z.number(), }) app.post('/user', zValidator('json', schema), (c) => { const data = c.req.valid('json') return c.json({ success: true, data }) }) ``` **Why Validation:** - Type-safe request data - Automatic error responses - Runtime validation, not just TypeScript --- ## The 6-Part Hono Mastery Guide ### Part 1: Routing Patterns #### Basic Routes ```typescript import { Hono } from 'hono' const app = new Hono() // GET request app.get('/posts', (c) => c.json({ posts: [] })) // POST request app.post('/posts', (c) => c.json({ created: true })) // PUT request app.put('/posts/:id', (c) => c.json({ updated: true })) // DELETE request app.delete('/posts/:id', (c) => c.json({ deleted: true })) // Multiple methods app.on(['GET', 'POST'], '/multi', (c) => c.text('GET or POST')) // All methods app.all('/catch-all', (c) => c.text('Any method')) ``` **Key Points:** - Always return a Response (c.json, c.text, c.html, etc.) - Routes are matched in order (first match wins) - Use specific routes before wildcard routes #### Route Parameters ```typescript // Single parameter app.get('/users/:id', (c) => { const id = c.req.param('id') return c.json({ userId: id }) }) // Multiple parameters app.get('/posts/:postId/comments/:commentId', (c) => { const { postId, commentId } = c.req.param() return c.json({ postId, commentId }) }) // Optional parameters (using wildcards) app.get('/files/*', (c) => { const path = c.req.param('*') return c.json({ filePath: path }) }) ``` **CRITICAL:** - `c.req.param('name')` returns single parameter - `c.req.param()` returns all parameters as object - Parameters are always strings (cast to number if needed) #### Query Parameters ```typescript app.get('/search', (c) => { // Single query param const q = c.req.query('q') // Multiple query params const { page, limit } = c.req.query() // Query param array (e.g., ?tag=js&tag=ts) const tags = c.req.queries('tag') return c.json({ q, page, limit, tags }) }) ``` **Best Practice:** - Use validation for query params (see Part 4) - Provide defaults for optional params - Parse numbers/booleans from query strings #### Wildcard Routes ```typescript // Match any path after /api/ app.get('/api/*', (c) => { const path = c.req.param('*') return c.json({ catchAll: path }) }) // Named wildcard app.get('/files/:filepath{.+}', (c) => { const filepath = c.req.param('filepath') return c.json({ file: filepath }) }) ``` #### Route Grouping (Sub-apps) ```typescript // Create sub-app const api = new Hono() api.get('/users', (c) => c.json({ users: [] })) api.get('/posts', (c) => c.json({ posts: [] })) // Mount sub-app const app = new Hono() app.route('/api', api) // Result: /api/users, /api/posts ``` **Why Group Routes:** - Organize large applications - Share middleware for specific routes - Better code structure and maintainability --- ### Part 2: Middleware Composition #### Middleware Flow ```typescript import { Hono } from 'hono' const app = new Hono() // Global middleware (runs for all routes) app.use('*', async (c, next) => { console.log(`[${c.req.method}] ${c.req.url}`) await next() // CRITICAL: Must call next() console.log('Response sent') }) // Route-specific middleware app.use('/admin/*', async (c, next) => { // Auth check const token = c.req.header('Authorization') if (!token) { return c.json({ error: 'Unauthorized' }, 401) } await next() }) app.get('/admin/dashboard', (c) => { return c.json({ message: 'Admin Dashboard' }) }) ``` **CRITICAL:** - **Always call `await next()`** in middleware - Middleware runs BEFORE the handler - Return early to prevent handler execution - Check `c.error` AFTER `next()` for error handling #### Built-in Middleware ```typescript import { Hono } from 'hono' import { logger } from 'hono/logger' import { cors } from 'hono/cors' import { prettyJSON } from 'hono/pretty-json' import { compress } from 'hono/compress' import { cache } from 'hono/cache' const app = new Hono() // Request logging app.use('*', logger()) // CORS app.use('/api/*', cors({ origin: 'https://example.com', allowMethods: ['GET', 'POST', 'PUT', 'DELETE'], allowHeaders: ['Content-Type', 'Authorization'], })) // Pretty JSON (dev only) app.use('*', prettyJSON()) // Compression (gzip/deflate) app.use('*', compress()) // Cache responses app.use( '/static/*', cache({ cacheName: 'my-app', cacheControl: 'max-age=3600', }) ) ``` **Built-in Middleware Reference**: See `references/middleware-catalog.md` #### Middleware Chaining ```typescript // Multiple middleware in sequence app.get( '/protected', authMiddleware, rateLimitMiddleware, (c) => { return c.json({ data: 'Protected data' }) } ) // Middleware factory pattern const authMiddleware = async (c, next) => { const token = c.req.header('Authorization') if (!token) { throw new HTTPException(401, { message: 'Unauthorized' }) } // Set user in context c.set('user', { id: 1, name: 'Alice' }) await next() } const rateLimitMiddleware = async (c, next) => { // Rate limit logic await next() } ``` **Why Chain Middleware:** - Separation of concerns - Reusable across routes - Clear execution order #### Custom Middleware ```typescript // Timing middleware const timing = async (c, next) => { const start = Date.now() await next() const elapsed = Date.now() - start c.res.headers.set('X-Response-Time', `${elapsed}ms`) } // Request ID middleware const requestId = async (c, next) => { const id = crypto.randomUUID() c.set('requestId', id) await next() c.res.headers.set('X-Request-ID', id) } // Error logging middleware const errorLogger = async (c, next) => { await next() if (c.error) { console.error('Error:', c.error) // Send to error tracking service } } app.use('*', timing) app.use('*', requestId) app.use('*', errorLogger) ``` **Best Practices:** - Keep middleware focused (single responsibility) - Use `c.set()` to share data between middleware - Check `c.error` AFTER `next()` for error handling - Return early to short-circuit execution --- ### Part 3: Type-Safe Context Extension #### Using c.set() and c.get() ```typescript import { Hono } from 'hono' type Bindings = { DATABASE_URL: string } type Variables = { user: { id: number name: string } requestId: string } const app = new Hono<{ Bindings: Bindings; Variables: Variables }>() // Middleware sets variables app.use('*', async (c, next) => { c.set('requestId', crypto.randomUUID()) await next() }) app.use('/api/*', async (c, next) => { c.set('user', { id: 1, name: 'Alice' }) await next() }) // Route accesses variables app.get('/api/profile', (c) => { const user = c.get('user') // Type-safe! const requestId = c.g
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.