Run your h3 apps in edge runtimes with Web API compatibility.

In order to run h3 apps in web compatible edge runtimes supporting fetch API with Request and Response, use toWebHandler adapter to convert h3 app into a fetch-like function.


First, create app entry:

import { createApp, defineEventHandler } from "h3";

export const app = createApp();

app.use(defineEventHandler(() => "Hello world!"));

Create web entry:

import { toWebHandler } from "h3";
import { app } from "./app.mjs";

// Create Web Adapter
export const handler = toWebHandler(app);

// Integrate handler with your runtime.
// Input is a Request and response is Promise<Response>

Local testing

You can test adapter using any compatible JavaScript runtime by passing a Request object.

import { handler } from "./web.mjs";

const response = await handler(new Request(new URL("/", "http://localhost")));

console.log(await response.text()); // Hello world!

Run with node ./web.test.mjs.