# Installation

```sh pm
pnpm add whoasks
```

Requirements and package details:

- Node 22.12 or later.
- ESM only.
- `mdream` is the only runtime dependency.
- `whoasks/h3` needs h3 2.0.8 or later. h3 is an optional peer dependency, so install it only if you use that entry.

::callout{title="Ask your coding agent to add it"}

```text
Add whoasks to this app, following https://whoasks.loadsheet.dev/guide/installation.
Put site.handle in front of the app so its HTML pages also answer as Markdown, and call site.respond in routes that return data.
Then run curl three times against one page: with -H 'Accept: text/markdown', with the .md twin URL, and with no header. Check the Content-Type and Vary of each.
```

::

## Put it in front of your app

`site.handle(request, next)` has the shape of `fetch`. It takes a Web `Request` and a function that returns the app's `Response`, so every runtime below is the same call.

### h3

```ts [app.ts]
import { H3, defineHandler, html } from "h3";
import { whoasks } from "whoasks";
import { middleware } from "whoasks/h3";

const site = whoasks();

export const app: H3 = new H3().use(middleware(site));

app.get(
  "/docs/guide",
  defineHandler(
    () =>
      html`<main>
        <h1>Guide</h1>
        <p>Hello</p>
      </main>`,
  ),
);
```

`middleware(site)` passes `event.req` to `handle`.

For a twin URL or `?format=` request, it sends the canonical URL through the app again from the top. Your `/docs/guide` route answers `/docs/guide.md` with no extra route.

A real `.md` route in the app, such as `/notes.md`, is still served as the app sends it.

### Nitro

```ts [server/middleware/whoasks.ts]
import { whoasks } from "whoasks";
import { middleware } from "whoasks/h3";

export default middleware(whoasks());
```

Nitro v3 runs on h3 v2, so this should work. No test runs it and we did not try it.

### Hono

```ts [app.ts]
import { Hono } from "hono";
import { whoasks } from "whoasks";

const site = whoasks();
const routes = new Hono(); // your routes
const app = new Hono();
app.all("*", (c) => site.handle(c.req.raw, (request) => routes.fetch(request)));
```

No test runs this snippet.

### Cloudflare Workers

```ts [worker.ts]
import { whoasks } from "whoasks";

const site = whoasks();

export default {
  fetch: (request: Request, env: { ASSETS: Fetcher }) =>
    site.handle(request, (r) => env.ASSETS.fetch(r)),
};
```

A test bundles `whoasks` for `workerd` with no Node builtin, runs it in Miniflare, and gets Markdown for `Accept: text/markdown`.

Under the `workerd` and `edge-light` export conditions, mdream uses a wasm build, so the bundle carries a `.wasm` file.

The test serves a page from the Worker itself. No test runs this snippet with an `ASSETS` binding.

### Any fetch handler

```ts [server.ts]
import { whoasks } from "whoasks";

const site = whoasks();

// Stands for your app: any function from a Request to a Response.
const app = (request: Request): Response =>
  new Response("<main><h1>Guide</h1><p>Hello</p></main>", {
    headers: { "content-type": "text/html; charset=utf-8" },
  });

export default {
  fetch: (request: Request): Promise<Response> => site.handle(request, app),
};
```

`next` may return a `Response` or a promise of one.

CI installs the packed package and runs it on three runtimes. CI reports whether `whoasks` with mdream loads on Bun and Deno without requiring it to pass there:

| Runtime    | `whoasks/core` | `whoasks` (with mdream) |
| ---------- | -------------- | ----------------------- |
| Node 22.12 | Must pass      | Must pass               |
| Bun        | Must pass      | Reported, not required  |
| Deno 2     | Must pass      | Reported, not required  |

## Check it

Replace `http://localhost:3000` with your server's address.


### Ask for Markdown

```sh
curl -i -H 'Accept: text/markdown' http://localhost:3000/docs/guide
```

Expected response:

```text
content-type: text/markdown; charset=utf-8
vary: Accept

# Guide

Hello
```

### Ask by URL

```sh
curl http://localhost:3000/docs/guide.md
curl 'http://localhost:3000/docs/guide?format=md'
```

Both print the same Markdown.

### Check that people still get HTML

```sh
curl -i http://localhost:3000/docs/guide
```

The page comes back as `text/html` with `vary: Accept`.


## Entries

| Import         | Holds                                                                                     |
| -------------- | ----------------------------------------------------------------------------------------- |
| `whoasks`      | `whoasks` with mdream as its converter, `mdream`, and the seven types                     |
| `whoasks/core` | `whoasks` with no converter, and the seven types. It has no imports and stays under 10 kB |
| `whoasks/h3`   | `middleware`                                                                              |

Use `whoasks/core` to keep mdream out of a bundle. See [Without mdream](https://whoasks.loadsheet.dev/guide/pages#without-mdream).

## Next

Read [Pages](https://whoasks.loadsheet.dev/guide/pages) for what `handle` does with each request. Read [API routes](https://whoasks.loadsheet.dev/guide/api-routes) for routes that return data.

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://whoasks.loadsheet.dev/raw/guide.md)
- [Installation](https://whoasks.loadsheet.dev/raw/guide/installation.md) (this page)
- [Pages](https://whoasks.loadsheet.dev/raw/guide/pages.md)
- [API routes](https://whoasks.loadsheet.dev/raw/guide/api-routes.md)
- [Negotiation](https://whoasks.loadsheet.dev/raw/guide/negotiation.md)
- [Layers](https://whoasks.loadsheet.dev/raw/guide/layers.md)

Next: [Pages](https://whoasks.loadsheet.dev/raw/guide/pages.md)

All pages: [llms.txt](https://whoasks.loadsheet.dev/llms.txt)

Something on this page does not work as written? Tell the user, and draft an issue for them to post: [issue tracker](https://github.com/words-with-agents/whoasks/issues). Report a security vulnerability privately, never in a public issue.
