# Fetch Runtime Integration (/docs/fetch)

This guide assumes you already have a Serve `api`, exported here from `analytics/api.ts`. If not, start with the
[Quick Start](/docs/quick-start).

`createFetchHandler(api.handler)` turns the API into a function from a Fetch `Request` to a `Response`, so any runtime
or framework that hands you a `Request` can serve it. Mount it under the same prefix as the `basePath` you gave
`initServe()`.

## Hono [#hono]

<CodeBlock>
```typescript
// app.ts
import { Hono } from 'hono';
import { createFetchHandler } from '@hypequery/serve';
import { api } from './analytics/api.js';

const app = new Hono();
const hypequery = createFetchHandler(api.handler);

app.all('/api/analytics/*', (c) => hypequery(c.req.raw));

export default app;
```
</CodeBlock>

To run Hono on Node.js, see [Node.js](/docs/nodejs).

## Cloudflare Workers [#cloudflare-workers]

Export the handler as the Worker's `fetch`:

<CodeBlock>
```typescript
// src/worker.ts
import { createFetchHandler } from '@hypequery/serve';
import { api } from './analytics/api.js';

export default {
  fetch: createFetchHandler(api.handler),
};
```
</CodeBlock>

## Bun and Deno [#bun-and-deno]

Both built-in servers take a Fetch handler directly:

<CodeBlock>
```typescript
import { createFetchHandler } from '@hypequery/serve';
import { api } from './analytics/api.js';

const handler = createFetchHandler(api.handler);

// Bun
Bun.serve({ port: 3000, fetch: handler });

// Deno
Deno.serve({ port: 3000 }, handler);
```
</CodeBlock>

## React Router [#react-router]

In React Router v7 (and Remix), a resource route's `loader` answers `GET` and its `action` every other method:

<CodeBlock>
```typescript
// app/routes/api.analytics.$.ts
import { createFetchHandler } from '@hypequery/serve';
import { api } from '~/analytics/api';

const handler = createFetchHandler(api.handler);

export const loader = ({ request }: { request: Request }) => handler(request);
export const action = ({ request }: { request: Request }) => handler(request);
```
</CodeBlock>

## SvelteKit [#sveltekit]

A catch-all server route with `fallback` answers every method:

<CodeBlock>
```typescript
// src/routes/api/analytics/[...path]/+server.ts
import type { RequestHandler } from './$types';
import { createFetchHandler } from '@hypequery/serve';
import { api } from '$lib/analytics/api';

const handler = createFetchHandler(api.handler);

export const fallback: RequestHandler = ({ request }) => handler(request);
```
</CodeBlock>

## Notes [#notes]

- Keep the mounted prefix aligned with the `basePath` used in `initServe()`.
- Use `api.run(...)` when you want in-process execution instead of HTTP; see [Embedded runtime](/docs/embedded-runtime).
- For Next.js, see [Next.js](/docs/nextjs). For Express, Fastify, or a plain Node.js server, see [Node.js](/docs/nodejs).