# Seed

A seed puts starter content into the store so the client sees the real site on day one. It carries records only, of the types the connected site manages, in the [import record format](/api/build-a-site/seed#the-records). It runs as a queued job: the request answers at once with a `job_id`, and the progress endpoint reports the images stored so far (x of y), then the records created.

A seed is create-only: a record whose `external_id` already exists is left exactly as it is, whoever made it, and a record the client deleted is never brought back (not even with `overwrite=1`). Seeded records are **drafts until published**, unless the seed sets `publish=1`; either way each created record gets its missing addresses from its slug source, so it can be published as it is. The client can publish drafts later in the dashboard, several at once, through the records list's bulk **Publish** action (`POST /v1/client/objects/types/{key}/records/bulk`, a dashboard action, not part of this API).

Both endpoints live under `/v1/api/objects`, need the `objects` module and the `objects:write` scope, which the push key carries.

## POST /v1/api/objects/seed

Queues a seed run. The body is `multipart/form-data`.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `dry_run` | query, `0` or `1` | no | `1` checks every image and record and creates nothing. The run still reports progress and `created` says how many records it would create. Default `0`. |
| `publish` | query, `0` or `1` | no | `1` publishes every record the seed creates of a publishable type. Default `0`: drafts. Refused with `409` once a person has edited content. |
| `overwrite` | query, `0` or `1` | no | `1` also updates records whose `external_id` already exists: it replaces their values (and their slugs, position and parent when the record gives them), never their status. Default `0`: create-only. Refused with `409` once a person has edited content. |
| `records` | form field, JSON string | yes | A JSON list of at most 1000 records, see [The records](#the-records). |
| `files[<path>]` | form file | no | One image per local path the records use as `src`, named by that path, for example `files[images/rye.jpg]`. jpeg, png, webp, avif or gif, at most 20 MB each, at most 200 files. |

```bash title="Request"
curl -X POST "https://api.borkol.com/v1/api/objects/seed" \
  -H "Authorization: Bearer {push_key}" \
  -F 'records=[{"external_id":"rye","type":"bread","slugs":{"en":"rye"},"values":{"name":"Rye","photo":{"src":"images/rye.jpg","alt":"A rye loaf"}}}]' \
  -F "files[images/rye.jpg]=@images/rye.jpg"
```

Status: `202 Accepted`.

| Field | Type | Description |
| --- | --- | --- |
| `job_id` | string | The run's id (UUID). Poll it with `GET /v1/api/objects/seed/{job_id}`. |

```json title="Response"
{ "job_id": "0193a7c4-5d2e-7b61-9a0f-3c8e2d1f4b57" }
```

### The records

Each record is an object in the import format: `external_id` (1 to 191 characters, the key a later seed matches on), `type` (the key of a type the connected site manages), `values` (keyed by property key), and optionally `slugs` (keyed by locale), `parent` (`{"ref": "<external_id>"}` on hierarchical types) and `position`. A record that points at another uses `{"ref": "<external_id>"}`, also in a reference field inside a section or a repeater row, so a section can list records the same seed creates. `status` and `published_at` are ignored: `publish` decides.

An image is `{"src": "...", "alt": "..."}` anywhere in `values`, inside sections and repeater rows too. In a list of images each item becomes `{media_id, alt, caption}`. `src` is either:

- a local path such as `images/rye.jpg`, sent as the file part `files[images/rye.jpg]`, or
- an `http` or `https` URL, fetched once with redirects off, the 20 MB cap and private addresses refused.

Images are read only from `media` and `media_list` fields (also inside sections and repeater rows); an object with a `src` in any other field is left as it is. Local paths are never read on the server: a `src` without a matching file part and without an http(s) URL fails, for example `/etc/passwd`. The same `src` used twice is stored once, a URL already in the store's media library is reused, and the images of records the seed leaves out (they exist, or were deleted) are never fetched or stored.

### Errors

- `422` `{"code": "no_site", "message": "No website is connected. Connect one under Settings > Website first."}` when the store has no connected site.
- `422` `{"message": "...", "errors": {"records.0.type": ["Seed records must be of a type the site manages."]}}` for a record of any other type, and `errors.records` when `records` is not a JSON list or carries more than 1000.
- `422` `{"message": "...", "errors": {"files": ["a.png: must be a jpeg, png, webp, avif or gif image."]}}` for a file part that is not an image, is over 20 MB, or is not sent as `files[<path>]`.
- `409` `{"code": "seed_locked", "message": "..."}` for `publish=1` or `overwrite=1` once a person has changed any record of the site's types in the dashboard (an edit, a publish or unpublish, a move, a delete or restore, a bulk action or a CSV import). A seed without both flags still answers `202` and adds only what is missing.
- `403` `{"message":"Invalid ability provided."}` when the key lacks `objects:write`.

## GET /v1/api/objects/seed/{job_id}

The progress of one seed run. Poll it until `finished` is `true`. Requires the `objects:write` scope.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `job_id` | path, string | yes | The `job_id` from `POST /v1/api/objects/seed`. |

```bash title="Request"
curl https://api.borkol.com/v1/api/objects/seed/0193a7c4-5d2e-7b61-9a0f-3c8e2d1f4b57 \
  -H "Authorization: Bearer {push_key}"
```

Status: `200 OK`.

| Field | Type | Description |
| --- | --- | --- |
| `phase` | string | `media` while images are stored, `records` while records are created, then `done` or `failed`. |
| `total` | integer | Distinct images in the records the seed will write. |
| `done` | integer | Images stored so far. |
| `failed` | integer | Images that could not be stored. Their values are left out (a single image becomes `null`, a list drops the item) and the reason is in `errors`. |
| `finished` | boolean | `true` once `phase` is `done` or `failed`. |
| `created` | integer | Records created. `0` until the records phase ends, and `0` when it failed. |
| `errors` | array of strings | What went wrong, each naming the image `src` or the record path (`records[1].values.name: ...`). |

```json title="Response"
{
  "phase": "done",
  "total": 3,
  "done": 2,
  "failed": 1,
  "finished": true,
  "created": 12,
  "errors": ["https://cdn.example.com/missing.jpg: Unreachable: HTTP 404."]
}
```

The records phase runs in one transaction. When any record fails (a required value missing, a ref that does not resolve), no record is created, the images this run stored are removed, `phase` is `failed` and `errors` names each record. `done` with entries in `errors` means the records were created and some images were skipped.

### Errors

- `404` when no run of this store has that id.
- `403` `{"message":"Invalid ability provided."}` when the key lacks `objects:write`.
