# Publishing surfaces

> Create a surface and upload versions through the API.

All calls use an API key with the `surfaces:write` scope (bearer token). Reading works with `traces:read` or `surfaces:write`. A key acts with the real role of its creator in the key's namespace: members manage their own private surfaces, admins can also publish.

## HTML rules

One file, at most 16 MiB, containing a `<head>`. These are rejected: `<iframe>`, `<link>`, `@import` and `<script src=...>`. Inline all CSS and JavaScript.

## Flow

1. Create the surface (once):

```bash
curl -X POST https://adtraces.adfactory.app/v1/namespaces/<slug>/surfaces \
  -H "Authorization: Bearer $TRACES_API_KEY" -H "Content-Type: application/json" \
  -d '{"key":"cost-chart","name":"Cost chart","description":"Token cost per turn","icon":"chart"}'
```

2. Ask for an upload target. The version label must be new, `byteSize` is the exact size of your HTML in bytes:

```bash
curl -X POST https://adtraces.adfactory.app/v1/surfaces/cost-chart/versions/uploads \
  -H "Authorization: Bearer $TRACES_API_KEY" -H "Content-Type: application/json" \
  -d '{"version":"1.0.0","byteSize":12345}'
```

The answer holds `upload.url` (single use, valid for 10 minutes, no token needed).

3. Send the file to that URL. The body must have exactly the announced size. The answer holds `data.artifactId`:

```bash
curl -X POST "<upload.url>" -H "Content-Type: text/html" --data-binary @surface.html
```

4. Complete the upload with that ID. This creates the immutable version but does not make it current:

```bash
curl -X POST https://adtraces.adfactory.app/v1/surfaces/cost-chart/versions/uploads/complete \
  -H "Authorization: Bearer $TRACES_API_KEY" -H "Content-Type: application/json" -d '{"version":"1.0.0","artifactId":"<artifactId>"}'
```

The response returns the version and the surface with its versions. Make it current with the PATCH below.

## Manage

```bash
# Rename, change the current version, publish (admins only) or archive
curl -X PATCH https://adtraces.adfactory.app/v1/surfaces/cost-chart \
  -H "Authorization: Bearer $TRACES_API_KEY" -H "Content-Type: application/json" \
  -d '{"currentVersion":"1.0.0","publishStatus":"public"}'

curl https://adtraces.adfactory.app/v1/surfaces/cost-chart -H "Authorization: Bearer $TRACES_API_KEY"
curl https://adtraces.adfactory.app/v1/surfaces -H "Authorization: Bearer $TRACES_API_KEY"          # marketplace
curl https://adtraces.adfactory.app/v1/pinned-surfaces -H "Authorization: Bearer $TRACES_API_KEY"   # your pins
```

`PUT /v1/pinned-surfaces` replaces the whole pin list: `{"keys":["cost-chart"]}`, up to 20 keys. Versions are immutable; to change the HTML, upload a new version.

Patchable fields: `name`, `description`, `icon`, `currentVersion`, `publishStatus` (`private` or `public`), `archived` (boolean).
