
# Builds and progress

A build is Unvop's whole pipeline run for one business: research, a design reference, the design system, the copy, pages and images, then deploy, a proof pass over every section and an audit. It runs on its own for several minutes, durably: a build that goes quiet is picked back up, and finished stages are never paid for twice.

Builds belong to accounts. They need an account key, an OAuth connection, or a platform key made by an account, and the `generate` scope.

## Start one

`POST /builds` takes a `prompt` and answers `202` with the build. Everything else is optional, but real details make a better site.

```json title="POST /builds"
{
  "prompt": "Harbour Bakery is a family bakery in Falmouth. Sourdough, pastries and celebration cakes, open 7 to 3 every day, with a cafe upstairs looking over the harbour.",
  "name": "Harbour Bakery",
  "subdomain": "harbour-bakery",
  "website": "https://harbourbakery.example",
  "locale": "en-GB",
  "phone": "01326 555 010"
}
```

| Field | What it does |
|---|---|
| `prompt` | What the business does and who for. The only required field. |
| `name` | The business name. |
| `website` | The current site. Research reads it for real details. |
| `subdomain` | The free address, like `harbour-bakery.unvop.dev`. Check it with `GET /subdomains/{name}`. |
| `locale`, `address`, `phone`, `email` | Facts the site should use as given. |
| `brand` | `{ tokens, notes }`: colours and a note on the look you want. |
| `logo`, `icon`, `inspiration` | Upload ids from `POST /builds/uploads/{kind}`. |
| `siteId` | Rebuild a site you own instead of making a new one. |
| `source` | `{ url, ownSite: true }`: rebuild your own live website as an editable Unvop site. |
| `screenshot` | `{ upload, viewport }`: rebuild a screenshot as an editable site. |

The same request sent again within two minutes returns the build already running, and an `Idempotency-Key` makes any repeat safe. You can have two builds running at once; a third gets `429` until one finishes, and an account can start ten builds an hour.

### Upload a logo or inspiration first

Files go up as `multipart/form-data` in a `file` field, and come back as an upload id:

```sh
curl https://api.unvop.com/v1/builds/uploads/logo \
  -H "Authorization: Bearer $UNVOP_KEY" \
  -F "file=@logo.svg"
```

```json
{ "upload": { "id": "…" } }
```

Pass it as `logo` (or `icon`) in the build. `inspiration` takes up to six ids of sites or pictures whose look you like, and `screenshot` takes the id of a screenshot to rebuild. Uploads stay private to your account.

## Follow it

A build moves through stages such as `researching`, `designing`, `writing`, `building`, `deploying` and `reviewing`, and `message` says what it is doing in a sentence. `status` is `queued`, `running`, `succeeded` or `failed`.

### Stream it

`GET /builds/{buildId}/events` is a Server-Sent Events stream. You get a `build` event with the whole build each time its stage or message changes, then `done` when it succeeds or fails. The stream closes after five minutes with a `timeout` event; connect again to keep following.

```js tab="JavaScript"
const res = await fetch(`https://api.unvop.com/v1/builds/${buildId}/events`, {
  headers: { Authorization: `Bearer ${process.env.UNVOP_KEY}`, Accept: 'text/event-stream' },
});
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
let buffer = '';
for (;;) {
  const { value, done } = await reader.read();
  if (done) break;
  buffer += value;
  const events = buffer.split('\n\n');
  buffer = events.pop();
  for (const raw of events) {
    const event = /^event: (.*)$/m.exec(raw)?.[1];
    const data = JSON.parse(/^data: (.*)$/m.exec(raw)?.[1] ?? 'null');
    if (event === 'build') console.log(data.stage, data.message);
    if (event === 'done') console.log('Finished:', data.status, data.url);
  }
}
```

```python tab="Python"
import json, os, requests

with requests.get(
    f"https://api.unvop.com/v1/builds/{build_id}/events",
    headers={"Authorization": f"Bearer {os.environ['UNVOP_KEY']}", "Accept": "text/event-stream"},
    stream=True,
) as res:
    event = None
    for line in res.iter_lines(decode_unicode=True):
        if line.startswith("event: "):
            event = line[7:]
        elif line.startswith("data: "):
            data = json.loads(line[6:])
            if event == "build":
                print(data["stage"], data["message"])
            elif event == "done":
                print("Finished:", data["status"], data["url"])
```

```sh tab="curl"
curl -N https://api.unvop.com/v1/builds/job_…/events \
  -H "Authorization: Bearer $UNVOP_KEY" \
  -H "Accept: text/event-stream"
```

### Or poll

`GET /builds/{buildId}` answers the same build. Every 20 to 30 seconds is plenty. `GET /builds` lists your 20 most recent builds.

## When it finishes

On `succeeded`, `siteId` is the new site and `url` its live address. From there everything in the API works on it: [pages](https://developers.unvop.com/guides/pages-and-blocks.md), [collections](https://developers.unvop.com/guides/collections.md), [media](https://developers.unvop.com/guides/media.md) and the rest.

On `failed`, `error` says why in a sentence and `errorCode` names it. Some failures are permanent (a refused key, a missing upload); for the others, `POST /builds/{buildId}/retry` picks the build back up where it stopped. Finished stages are skipped, so only the failed stage and those after it run again.

`POST /builds/{buildId}/cancel` stops a build in progress. Finished stages stay stored, and nothing after that point runs or spends.

## Write a site yourself instead

If you already have the content, `POST /sites` creates a site from a SiteSpec you wrote, or from the base template with just a `name`. `GET /schema` describes what a SiteSpec may contain: every block type with its fields, and the SiteSpec JSON Schema. Add `?dryRun=1` to validate without creating anything. `POST /generate`, the older one-call generator, still works for clients that use it, but new code should use builds.
