API-Referenz

Rezepte

Einträge aus einem externen System synchronisieren

TOKEN=ic_…   # Token mit Schreibrecht
API=https://example.com/api/v1

curl -s -X POST "$API/collections/team/items" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"data": {"name": "Ada Lovelace", "rolle": "Engineering"}, "published": true}'

Bild hochladen und einem Eintrag zuweisen

FILE_ID=$(curl -s -H "Authorization: Bearer $TOKEN" -F file=@ada.jpg "$API/files" | jq '.[0].id')

curl -s -X PUT "$API/collections/team/items/12" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d "{\"data\": {\"foto\": $FILE_ID}}"

Öffentliche Collection im Browser per JavaScript laden

Collection mit public_api: true vorausgesetzt – ohne Token. Öffentliche Collections senden Access-Control-Allow-Origin: *, der Aufruf klappt also auch von einer anderen Domain (z. B. einer Anzeigetafel). Ohne Cookies und ohne Authorization-Header – cross-origin gibt es nur veröffentlichte Einträge:

const res = await fetch('https://example.org/api/v1/collections/faq/items')
const items = await res.json()
for (const it of items) console.log(it.data.frage, it.data.antwort)

Seite per API anlegen und veröffentlichen

curl -s -X POST "$API/pages" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"parent_id": 1, "title": "Impressum", "slug": "impressum", "published": true,
       "html": "{% include \"header\" %}<main><h1>{{ page.title }}</h1></main>"}'

JavaScript-Client mit Session (wie die Admin-App)

async function api(method, path, body) {
  const res = await fetch('/api/v1' + path, {
    method,
    credentials: 'same-origin',
    headers: { 'X-IC-CSRF': '1', ...(body ? { 'Content-Type': 'application/json' } : {}) },
    body: body ? JSON.stringify(body) : undefined,
  })
  if (res.status === 204) return null
  const data = await res.json()
  if (!res.ok) throw new Error(data.error)
  return data
}

await api('POST', '/auth/login', { username: 'admin', password: '…' })
const pages = await api('GET', '/pages')