# API Routes \[Add server-rendered endpoints to your docs site]

## Overview

Vocs supports server endpoints alongside your docs pages. Files placed in `src/pages/_api/` are mounted as routes, with the `_api` prefix stripped from the URL.

:::file-tree

* +src
  * +pages
    * +\_api
      * +api
        * hello.ts → /api/hello
        * og.tsx → /api/og
      * healthz.ts → /healthz
    * index.mdx → /
      :::

Vocs (via Waku) walks every file under `_api/` and registers it as a route at the matching path, minus the `_api` segment.

Each handler module either exports a `default` function (handles all methods) or named exports per HTTP method.

## Recipes

### Handle All Methods

Export a `default` function to handle every HTTP method through one entry point.

```ts [src/pages/_api/api/hello.ts]
export default function handler(request: Request) {
  const search = new URL(request.url).searchParams

  return Response.json({
    message: 'Hello from Vocs!',
    name: search.get('name') ?? 'world'
  })
}
```

### Handle Specific HTTP Methods

Export functions named after the HTTP method you want to handle:

```ts [src/pages/_api/api/echo.ts]
export const GET = async () => {
  return new Response('I am a server!', { status: 200 })
}

export const POST = async (request: Request) => {
  const body = await request.json()
  return Response.json({ echoed: body })
}
```

Valid method exports are `GET`, `HEAD`, `POST`, `PUT`, `DELETE`, `CONNECT`, `OPTIONS`, `TRACE`, and `PATCH`. Any other named export throws a build-time error.

### Return Custom Responses

Handlers return a standard [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response). Use `Response.json()` for JSON, `new Response()` for raw bodies, or stream responses with [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API).

```ts
return new Response('plain text', {
  headers: { 'content-type': 'text/plain' }
})
```

### Use Server Helpers

The `vocs/server` module exports helpers you can compose inside handlers — including `Handler.og` for building OG endpoints and `Actions.fetchChangelog` for server components.

```ts [src/pages/_api/api/og.tsx]
import { Handler } from 'vocs/server'

export default function handler(request: Request) {
  return Handler.og(({ title, description }) => (
    <div style={{ display: 'flex', width: '100%', height: '100%' }}>
      <h1>{title}</h1>
      <p>{description}</p>
    </div>
  )).fetch(request)
}
```

### Override A Built-in Route

Vocs ships a handful of API routes out of the box:

* `/api/og` — Dynamic Open Graph image generator
* `/api/mcp` — Model Context Protocol server endpoint
* `/api/feedback` — Feedback adapter endpoint

Override any of them by placing your own file at the matching path under `_api/`.

:::file-tree

* +src
  * +pages
    * +\_api
      * +api
        * **og.tsx** ← overrides /api/og
          :::

## See More

<Cards>
  <Card title="Dynamic OG Images" description="Generate Open Graph images per page using Handler.og." icon="image" to="/features/dynamic-og-images" />

  <Card title="MCP Server" description="Expose docs and tools to AI clients via Model Context Protocol." icon="plug" to="/features/mcp-server" />

  <Card title="Changelog Generation" description="Fetch and render release notes with Actions.fetchChangelog." icon="clock" to="/features/changelog-generation" />

  <Card title="Page Feedback" description="Customize the /api/feedback endpoint and adapter." icon="message-square" to="/features/feedback" />
</Cards>
