
# H3 v2

> 🎉 H3 v2 is now stable! It is a full rewrite on top of web standards, stays compatible with most v1 utilities, and is faster and smaller than v1.

::read-more{to="/guide"}
Visit the [**H3 Guide**](/guide) to get started.
::

::read-more{to="/migration"}
Upgrading from v1? Read the [**Migration Guide**](/migration).
::

```js
import { H3, serve } from "h3";

const app = new H3().get("/", () => "⚡️ Tadaa!");

serve(app, { port: 3000 });
```

## 🌐 Web Standards First

H3 v2 is built on [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request), [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response), [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) and [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers). Handlers receive `event.req` (a standard `Request`) and `event.url`, and set response headers through `event.res`.

Return any value (string, object, `Response`, stream, `Blob`, ...) and H3 converts it to a `Response`.

```js
import { defineHandler } from "h3";

const handler = defineHandler(async (event) => {
  const { pathname, searchParams } = event.url;
  const accept = event.req.headers.get("Accept");
  const body = await event.req.json();

  event.res.headers.set("x-powered-by", "h3");
  return { hello: "web" };
});
```

:read-more{to="/guide/basics/handler" title="Event Handlers"}

## 🚀 Runs Everywhere

The same app runs on Node.js, Bun, Deno, Cloudflare Workers, Service Workers and browsers. [💥 srvx](https://srvx.h3.dev) provides a universal server layer, and `event.req.runtime` gives access to runtime-specific details when you need them.

## 🌳 URLPattern-like Routing

Routing uses [🌳 rou3](https://github.com/h3js/rou3) v1, which supports [URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API)-like syntax: named params, regex constraints, optional and repeated params, groups and wildcards.

```js
app.get("/users/:id(\\d+)", (event) => event.context.params.id);
app.get("/blog/:id(\\d+){-:title}?", (event) => event.context.params);
app.get("/files/:path+", (event) => event.context.params.path);
```

rou3 still matches routes segment by segment in a tree, so lookups stay fast. It differs from URLPattern in a few intentional ways, such as ignoring one trailing slash. See the [rou3 route patterns](https://github.com/h3js/rou3#route-patterns) docs for the full syntax and differences.

:read-more{to="/guide/basics/routing" title="Routing"}

## 🧩 Middleware and Plugins

H3 v2 supports `(event, next)` middleware, built-in helpers (`onRequest`, `onResponse`, `onError`, `basicAuth`, `bodyLimit`, ...) and reusable [plugins](/guide/advanced/plugins) with `definePlugin()`.

```js
app.use(async (event, next) => {
  const body = await next();
  event.res.headers.append("x-middleware", "works");
  return body;
});
```

:read-more{to="/guide/basics/middleware" title="Middleware"}

## 🔒 Type Safety

Handlers, responses and [errors](/guide/basics/error) (`HTTPError`) are typed. Validated handlers and body/query/params validation work with any [Standard Schema](https://standardschema.dev) library.

## 📏 Route Rules

The new `h3/rules` engine adds headers, redirects, CORS, caching and proxying to groups of routes with one config object.

```js
import { routeRules } from "h3/rules";

app.use(
  routeRules({
    "/old/**": { redirect: "/new/**" },
    "/assets/**": { headers: { "cache-control": "s-maxage=31536000" } },
    "/api/**": { cors: true },
  }),
);
```

:read-more{to="/guide/rules" title="Route Rules"}

## 🧰 Built-in Utilities

H3 includes utilities for cookies (including chunked cookies), sessions, CORS, proxying, static files, cache headers, server-sent events, WebSockets via [🔌 crossws](https://crossws.h3.dev), JSON-RPC/MCP helpers and an `h3/tracing` plugin.

:read-more{to="/utils" title="Utilities"}

## ⬆️ Upgrading

Most v1 utilities keep working, and deprecated v1 names are still exported to make upgrading easier. H3 v2 requires Node.js `>= 20.19` when running on Node.js.

:read-more{to="/migration" title="Migration Guide"}

## ❤️ Special Thanks

Thank you to all [contributors](https://github.com/h3js/h3/graphs/contributors), to everyone who tested the betas and release candidates and reported issues, to the [community](https://discord.h3.dev), to the [Nitro](https://nitro.build) community for using v2 throughout its development, and to our [sponsors](/#sponsors) who make it possible to work on open source.

::callout{to="https://discord.h3.dev"}
Join our [Discord](https://discord.h3.dev) to share your experience and feedback!
::
