Hyperclay
  1. Hyperclay
  2. Docs
  3. Data and APIs
  4. Page data endpoints

Page data endpoints

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

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.

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>
plaintext
?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=…

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:

plaintext
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 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.