
# Pages and blocks

A page is a list of blocks: a hero, a rich text section, a gallery, a form. Each block has a `type`, its `props` (the words, images and links it shows), an optional `variant`, and an `id` unique within the page. You edit a page's **draft**; visitors see the **published** version until you publish again.

## Address a page

A page is addressed by its id (`pg_…`) or its address:

| You write | Page |
|---|---|
| `index` | the homepage, `/` |
| `about` | `/about` |
| `insights/my-post` (or `insights%2Fmy-post`) | `/insights/my-post` |

Sub-resources (`/blocks`, `/publish`, `/unpublish`, `/duplicate`, `/revisions`) take the id or a one-segment address. Use the id for deeper pages.

## Read a page

```sh
curl -i https://api.unvop.com/v1/sites/site_…/pages/index \
  -H "Authorization: Bearer $UNVOP_KEY"
```

The answer is the page with every block of its draft, and an `ETag` header to send back when you save ([ETags](https://developers.unvop.com/guides/etags.md)). Add `?version=published` to read what is live instead. `GET /sites/{siteId}/pages` lists every page and article, the homepage first.

## Know the block types

`GET /sites/{siteId}/blocks` lists every block the site's pages can use, with its fields (name, type, label, options), defaults and variants. Read it before you write blocks: props are checked against these fields, and a page that doesn't fit is refused with `422 invalid_spec` and errors that name the exact path, like `pages[0].blocks[2].props.heading`.

Rich text props take simple HTML, like `<p>Open every day from 8.</p>`. Image props take a URL from your [media library](https://developers.unvop.com/guides/media.md).

## Change one block

`PATCH` a block with the props you want to change. Props you leave out are kept.

```sh tab="curl"
curl -X PATCH https://api.unvop.com/v1/sites/site_…/pages/index/blocks/b_hero \
  -H "Authorization: Bearer $UNVOP_KEY" \
  -H "Content-Type: application/json" \
  -H 'If-Match: "…the ETag…"' \
  -d '{ "props": { "heading": "Fresh bread from 7am" } }'
```

```js tab="JavaScript"
const page = await fetch(`https://api.unvop.com/v1/sites/${siteId}/pages/index`, { headers });
const etag = page.headers.get('ETag');
const { blocks } = await page.json();

await fetch(`https://api.unvop.com/v1/sites/${siteId}/pages/index/blocks/${blocks[0].id}`, {
  method: 'PATCH',
  headers: { ...headers, 'Content-Type': 'application/json', 'If-Match': etag },
  body: JSON.stringify({ props: { heading: 'Fresh bread from 7am' } }),
});
```

```python tab="Python"
page = requests.get(f"https://api.unvop.com/v1/sites/{site_id}/pages/index", headers=headers)
etag = page.headers["ETag"]
blocks = page.json()["blocks"]

requests.patch(
    f"https://api.unvop.com/v1/sites/{site_id}/pages/index/blocks/{blocks[0]['id']}",
    headers={**headers, "If-Match": etag},
    json={"props": {"heading": "Fresh bread from 7am"}},
)
```

A PATCH can also set `variant`, `hidden` (keep the block but don't show it) and `anchor` (the id people can link to with `#`).

## Add, move and remove blocks

`POST /sites/{siteId}/pages/{page}/blocks` inserts a block at the end, at a `position`, or `before` or `after` another block:

```json title="POST …/pages/index/blocks"
{ "type": "richText", "props": { "body": "<p>Open every day from 8.</p>" }, "after": "b_hero" }
```

`POST …/blocks/{blockId}/move` takes the same `position`, `before` or `after`. `DELETE …/blocks/{blockId}` removes one. Each answers with the page as it now is, plus any `warnings`.

## Write a whole page

`PUT /sites/{siteId}/pages/{page}` writes a page in one call: its title, SEO and every block. It creates the page when nothing is at that address. Send `"publish": true` to make it live in the same call.

```json title="PUT …/pages/about"
{
  "title": "About us",
  "blocks": [
    { "id": "b1", "type": "richText", "props": { "body": "<p>We bake every morning.</p>" } }
  ],
  "publish": false
}
```

`POST /sites/{siteId}/pages` is the lighter way to start: it adds a draft page from a `title` and `kind` (`page` or `post`), optionally from one of the theme's templates.

## Publish, unpublish and restore

- `POST …/pages/{page}/publish` makes the draft live and keeps a revision. Send a `note` to say what changed.
- `POST /sites/{siteId}/publish` publishes every page at once.
- `POST …/pages/{page}/unpublish` takes a page off the live site and keeps its draft. The homepage can't be unpublished.
- `GET …/pages/{page}/revisions` lists the last 50 published versions; `POST …/revisions/{revisionId}/restore` puts one back as the draft. Publish it to make it live again.
- `POST …/pages/{page}/duplicate` copies the draft to a new page at a free address.

Deleting a page (`DELETE …/pages/{page}`) takes it off the live site. The homepage can't be deleted; replace it with `PUT` instead.

## The look

Blocks carry content; the look lives in the site's design. `GET /sites/{siteId}/design` returns the brand tokens (colours, fonts, radius), the fonts and the stylesheet. `PATCH /sites/{siteId}/design/tokens` changes tokens by name and every page updates at once. Both need `design:write` to change.
