# Page data endpoints

> Read a document's HTML as JSON over the URL or a published endpoint, with cached responses and documented limits.

Source: https://hyperclay.com/docs/page-data/

Every Hyperclay page is already structured data: the HTML is the database. This page is the API for reading and writing that data. You describe what counts as data with a small map from field names to CSS selectors, and that one mapping works three ways:

- **From the URL**, as an ad-hoc query (`?data=`), read only, no setup.
- **As a named endpoint** the page publishes about itself (`/_/api`), read only.
- **In the browser**, as `extractData` and `applyData`, which read and write the live page.

The selector syntax is identical across all three, so once you can write the mapping you can use any of them. The rules syntax and the browser helpers are documented on the ClayJS [data page](https://clayjs.com/docs/plugins/data/).

## Read any page from the URL

Append `?data=` to any site URL with a mapping, and Hyperclay returns JSON. The rules live in the URL, nothing is set up in advance, and it is read only.

```html
<h1>My Tech Blog</h1>
<article class="post">
  <h2 class="post-title">Understanding JavaScript</h2>
  <span class="author">Alice Smith</span>
</article>
<article class="post">
  <h2 class="post-title">Python for Data Science</h2>
  <span class="author">Bob Johnson</span>
</article>
```

```
?data={title:"h1",posts:[".post",{heading:".post-title",author:".author"}]}
```

```json
{
  "title": "My Tech Blog",
  "posts": [
    { "heading": "Understanding JavaScript", "author": "Alice Smith" },
    { "heading": "Python for Data Science", "author": "Bob Johnson" }
  ]
}
```

A rule ending in `@--name`, such as `html@--color-accent`, returns one CSS custom property from the element's `style` attribute.

See it on a real site: [panphora.hyperclay.com/core/panphora.html?data=...](https://panphora.hyperclay.com/core/panphora.html?data=%7BsiteName%3A%22.site-name%22%2Cdescription%3A%22.site-description%22%2CnewsletterDescription%3A%22.newsletter-description%22%2Cposts%3A%5B%22%5Bpost%5D%22%2C%7Bdate%3A%22.post-date%40date%22%2Cdescription%3A%22.post-description%22%2Cnote%3A%22.post-note%22%2Cprojects%3A%5B%22%5Bproject%5D%22%2C%7Btype%3A%22.project-type%40project_type%22%2Cname%3A%22.project-name%22%2Curl%3A%22.project-name%40href%22%2Cdescription%3A%22.project-description%22%7D%5D%7D%5D%7D)

Responses are cached for five minutes. The `X-Cache` header is `HIT` from cache, `MISS` for a fresh extraction.

## Publish a named endpoint

Instead of putting rules in the URL, bake them into the page so it advertises its own stable shape. Add a rules tag with the token `api`:

```html
<script data-rules-name="api" data-rules-version="1" type="application/json">
{ "title": "h1", "posts": [".post", { "heading": ".post-title", "author": ".author" }] }
</script>
```

The page now serves that JSON at its own URL under `/_/api`:

```
GET https://you.hyperclay.com/_/api/<path>/<sitename>.html
```

For a site named `blog` that is `https://you.hyperclay.com/_/api/blog.html`. The response is the bare JSON, public, GET only, and CORS-enabled, so any app on any origin can read it. `data-rules-version` must be `"1"`, and one tag can serve several consumers with space-separated tokens, for example `data-rules-name="api cms"`.

It is the same engine as `?data=`. The only difference is where the rules come from: `?data=` is a question you ask from outside, `/_/api` is the answer the page publishes about itself.

The one exception is a site's `site.json`: it is the only address under `/_/api` that takes writes, and it holds the values every page in a [multi-page site](https://hyperclay.com/docs/multi-page-sites/) shares. In a page's `api` rules tag, the key `site` is reserved for those shared values: its rules say where each value goes on the page.

## Responses and limits

- A matched value comes back as text, a missing element as `null`, an empty array as `[]`.
- Failures return an HTTP error status with a JSON `{ "error": "..." }` message.
- `?data=` and `/_/api` read the **saved** page on the server: static content only, no JavaScript execution, text and attributes rather than raw HTML.
- `extractData` and `applyData` run in the browser against the **live** page and can address element properties such as `@value` and `@checked`. `applyData` writes the local DOM only.
