# Host it yourself

> The one route a clayjs host implements, a complete Express server in a couple dozen lines, and who writes the file.

Source: https://clayjs.com/docs/endpoint/

A clayjs server implements one route, and a complete server is a couple dozen lines of Express.

## The endpoint spec

| Part | Value |
| --- | --- |
| Route | `POST /_/save` |
| Body | The file's full HTML, as plain text. Always: this route has exactly one body shape, on every host. |
| Header | `Document-URL`: the full URL of the page being saved. |
| Success | `200` with JSON `{ "msg": "Saved" }` |
| Failure | Any non-2xx. The status is authoritative; host-generated failures use JSON `{ "msg": "why" }`, but clients tolerate non-JSON intermediary errors. |

A complete server in a couple dozen lines of Express:

```js
import express from "express";
import fs from "node:fs/promises";
import path from "node:path";

const root = process.cwd();
const app = express();
app.use(express.static(root));
app.use(express.text({ type: "*/*", limit: "10mb" }));

app.post("/_/save", async (req, res) => {
  try {
    const origin = req.get("Origin");
    if (origin && new URL(origin).host !== req.get("Host"))
      return res.status(403).json({ msg: "Cross-origin save refused", code: "forbidden" });
    const page = decodeURIComponent(new URL(req.get("Document-URL")).pathname);
    const target = path.join(root, path.normalize(page.endsWith("/") ? page + "index.html" : page));
    if (!target.startsWith(root + path.sep))
      return res.status(403).json({ msg: "Outside the folder", code: "forbidden" });
    if (!/^\s*<!doctype html>/i.test(req.body))
      return res.status(422).json({ msg: "Not a complete HTML document", code: "invalid-document" });
    await fs.writeFile(target, req.body);
    res.json({ msg: "Saved" });
  } catch {
    res.status(500).json({ msg: "Couldn't save" });
  }
});

app.listen(4600);
```

The server half is only half of it: on your own host nothing arms [edit mode](https://clayjs.com/docs/edit-mode/) for you, so a page served this way is read-only until you set `window.clayEditMode` before the script tag. A real deployment adds authorization and atomic writes. The same contract powers hyperclay.com, Hyperclay Local, and HTML Clay. Details you may ignore: every save also carries `Save-Trigger: user|auto` (whether a person triggered it), `Save-ID` (an id for this attempt, see [conditional saves](https://clayjs.com/docs/conditional-saves/)), `Page-URL` (the older spelling of `Document-URL`) and `X-Clay-Page-Format: 1` (the page format this client writes, which a host that holds a format floor checks before it writes), plus `If-Match` once your `/_/meta` announces `conditional`. HTML Clay uses a token variant, `POST /_/save/{token}`, read from `<html savetoken>`.

## Hosts (who writes the file)

clayjs is client-side; a host accepts the save and writes bytes:

- HTML Clay, a small desktop app (https://htmlclay.com): serves local `.htmlclay` files at http://127.0.0.1, writes saves back to disk, keeps automatic version history.
- hyperclay.com: hosts the same files online.
- Your own server: implement one route (see "The endpoint spec" above, ~20 lines).

## Hosting on your own server

Hyperclay apps are just HTML files, so they work on any web server.

**Basic hosting:**

1. Download your malleable document
2. Upload to any static file host (GitHub Pages, Netlify, Vercel, etc.)
3. The app works immediately in view mode

**Advanced hosting with persistence:**

To enable edit/save functionality on your own server, you'll need:

- A simple backend to handle save requests
- Basic authentication for edit permissions
- File write capabilities
