# Using the collectr API

Instructions for AI agents and the people who build them. The API lives at
`https://the.collectr.eu`. The full reference is the OpenAPI spec at `/openapi.yaml`
(or `/openapi.json`); browse it at `/api-docs`. `/api` and `/.well-known/api-catalog`
point to all of these.

collectr is an online inventory for museums, private collections and archives. Every
item is an **object** with a flat set of text fields, files (**attachments**), and
links to other objects. Objects belong to an **owner**, a user or an organisation, and
can be grouped into **collections**. Objects in a public collection, and their files,
are visible to everyone; everything else only to the owner's members and guests.

## Authentication

Use HTTP Basic auth on every request:

```
Authorization: Basic base64(username:password)
```

Don't sign in to a session and don't keep cookies. Sessions are for the browser
app. Use an account made for your integration. It can do exactly what that
user can do in the web app, so give it only the organisation memberships it needs.

## Before you change anything

1. `GET /owners` lists the owners you can act for. Use the organisation id as the
   `o` parameter from then on.
2. `GET /templates/defaultobjtemplate` (objects) and
   `GET /templates/defaultmediatemplate` (photos, documents, films) return the field
   definitions as XML. Each field has an `id`, a `type` (`text`, `multitext` or
   `selection`) and German/English labels. `selection` fields list their allowed
   values.
3. `GET /maintenance/fields?o=<owner>` lists the fields the owner actually uses and
   how many objects fill each one. Organisations sometimes use fields beyond the
   template.

Convert the template into a compact JSON list once and keep it in your context:

```json
[
  {"id": "title", "type": "text", "label": "Titel / Title"},
  {"id": "condition", "type": "selection", "label": "Zustand / Condition",
   "values": ["gut/stabil", "mittel", "schlecht/gefährdet", "Fragment/unvollständig", "unvollendet"]}
]
```

## Common tasks

| Task | Calls |
|---|---|
| Find objects | `GET /objects/search?o=<owner>&q={"all":["hupe"]}` |
| Filter by field | `GET /objects/search?o=<owner>&q={"category":["zubehör"]}` |
| Read all objects | `GET /objects/search?o=<owner>&q={}&limit=200`, then pass `bookmark` as `offset` until `rows` is empty |
| Read only some fields | add `&fields=["title","inventorynumber"]` |
| Read one object | `GET /objects/{id}` |
| Create objects | `POST /objects` for one, `POST /objects/bulk` for many |
| Edit an object | `GET /objects/{id}`, change it, `PUT /objects/{id}` with the whole object |
| Add a photo or file | `POST /objects/{id}/attachments` (multipart, field `uploadFile`) |
| Reorder files | `GET /objects/{id}/attachments`, then `PUT /objects/{id}/attachments` with `{"order": [<every attachment id>]}` |
| Download a file | `GET /objects/{id}/attachments/{attId}?download=1` |
| Get an image preview | `GET /objects/{id}/attachments/{attId}?w=800` (at most 4096 px) |
| See existing spellings | `GET /objects/fieldvalues?o=<owner>&f=<field>&q=<prefix>` |
| Find inconsistent values | `GET /maintenance/values?o=<owner>&f=<field>&sort=count` |
| Objects with a value | `GET /maintenance/objects?o=<owner>&f=<field>&value=<value>` |
| Objects without images | `GET /maintenance/mediaobjects?o=<owner>&state=none` |
| Put objects in a collection | add the collection id to the object's `collections`, then `PUT` |

## Rules

1. **`PUT` replaces the whole object.** Read the object right before you write it,
   change only what you mean to change, and send everything back. A field missing
   from `data` is deleted. On `409`, read it again and retry.
2. **Mark your edits.** Set `modified` (ISO 8601) and `modifiedby` (for example
   `"Agent: <name of the integration>"`). The server does not do this for you.
3. **Use existing fields and values.** Only use field ids from the template or
   `/maintenance/fields`. For `selection` fields, use a listed value. For free-text
   fields, check `/objects/fieldvalues` and reuse the spelling the collection already
   uses.
4. **Everything is text.** Write numbers and dates as strings, in the format the
   collection already uses (`"2.2020"`, `"Februar 2020"`, `"12.5"`).
5. **Keep the language.** Most collections are in German. Do not translate values
   or labels.
6. **Prefer empty over guessed.** If you cannot verify a value, leave the field empty
   and report it to the person instead.
7. **Do not delete** objects, attachments, collections or organisations unless a
   person explicitly asked for that exact deletion. Deletion cannot be undone.
8. **Stay out of accounts.** Don't call endpoints tagged *Account* (sign-up,
   passwords). If the integration account's password
   changes, every session of that account ends; with Basic auth on every request
   you only need the new password.
9. **Go slowly.** A few requests per second at most. Use `fields=` and `limit=` to
   keep responses small.

## Working in batches

For larger edits, don't write each change straight away. Produce a list of proposed
changes first, one JSON object per line, and let a person or a review step approve
them:

```json
{"id": "034d51ac…", "field": "category", "old": "Zubehör ", "new": "Zubehör", "reason": "trailing space", "confidence": 0.99}
```

Then apply the approved lines with `GET` → change → `PUT`, and keep the file as a
record of what changed.

## Errors

| Status | Meaning |
|---|---|
| 400 | Missing or invalid input; the body says what is wrong |
| 401 | Missing or wrong credentials |
| 403 | Your account may not do this for this owner |
| 404 | Not found, not visible to you, or the owner in `o` does not exist |
| 409 | Someone else saved in between; read again and retry |
| 500 | Server error, for example invalid JSON in `q` |
