Files
Kaname/docs/api.md
T
Pascal LinxweilerandClaude Opus 5.5 7b1c8070b3 API for agents: tokens, /api/v1, guardrails, approval
- Scoped bearer tokens (models:read, downloads:create), stored as SHA-256, managed in
  Settings -> API tokens. Only /api/v1 accepts them; the rest stays OIDC-only.
- /api/v1: Hub lookup, library list with the search filters, download requests through the
  existing add-model/queue path (idempotent), compact job status, disk space.
- Guardrails: approval mode (default approve), max size per request, minimum free space incl.
  reserved bytes, max open agent jobs, author allowlist. Approve/reject in the UI; "requested by"
  shown and filterable.
- docs/api.md (incl. Traefik forward-auth bypass), docs/agent/SKILL.md, DB-backed API tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 16:14:45 +02:00

11 KiB

Kaname API (/api/v1)

A small JSON API for programs and AI agents: look up Hugging Face repos, list the local library, request downloads and follow them. Everything an agent can do here is bounded by guardrails; it can't delete models or files, change settings, or manage runtimes or tokens.

The rest of Kaname (the web UI and its internal /api/* routes) stays behind your OIDC login. Only /api/v1 accepts tokens, and it accepts only tokens (a browser session doesn't work there).

Authentication

Create a token in Settings → API tokens. It is shown once; Kaname stores only its SHA-256. Send it with every request:

Authorization: Bearer kn_…
Scope Allows
models:read Hub lookup, list models, read jobs (and their log), disk space
downloads:create POST /api/v1/downloads, within the guardrails

A missing, unknown or revoked token gets 401; a token without the needed scope gets 403. The settings page shows when each token was last used; Revoke stops it immediately.

Everything a token requests is marked with its name ("requested by hermes") on the model and job pages and in the library, where you can filter by it.

Errors

Errors are JSON with a message meant to be read and acted on:

{ "error": { "code": "too_large", "message": "exceeds max size per request 60.0 GB: requested 74.2 GB. Pick a smaller quant or fewer files." } }
Status Codes
400 invalid_request, invalid_json, empty_selection (quant or globs match nothing), invalid_repo_id
401 / 403 unauthorized, forbidden (scope), author_not_allowed, gated_no_token, gated_no_access
404 not_found, repo_not_found, revision_not_found
409 already_exists (repo is in the library at another commit), busy (the model has an open job)
422 too_large
429 too_many_jobs, rate_limited (Hugging Face)
503 / 507 disk_unknown (worker offline), disk_space

Sizes appear as bytes plus a readable "… GB" field (decimal GB, like the Hub).

Endpoints

GET /api/v1

Any valid token. Who you are, your scopes, the current guardrails and the endpoint list.

curl -s -H "Authorization: Bearer $KANAME_TOKEN" "$KANAME_URL/api/v1"
{
  "token": { "name": "hermes", "scopes": ["models:read", "downloads:create"] },
  "guardrails": { "approvalMode": "approve", "maxRequestBytes": 60000000000, "maxRequestSize": "60.0 GB",
                  "minFreeBytes": 100000000000, "minFreeSize": "100.0 GB", "maxOpenAgentJobs": 2, "allowedAuthors": [] },
  "endpoints": ["…"]
}

GET /api/v1/hub/lookup?repo=org/name[&revision=main][&files=all]

models:read. Resolves the revision to a commit SHA and lists quants and files, the same data as the Add model dialog. repo may also be a Hub URL. Up to 200 files are listed unless files=all.

{
  "repoId": "unsloth/Qwen3-8B-GGUF", "revision": "main", "sha": "9e6855bc…", "gated": false,
  "lastModified": "2026-05-01T10:00:00.000Z",
  "inLibrary": null,
  "quants": [{ "quant": "Q4_K_M", "files": 1, "bytes": 5027783488, "size": "5.0 GB" }, "…"],
  "fileCount": 31, "totalBytes": 104000000000,
  "files": [{ "path": "Qwen3-8B-Q4_K_M.gguf", "bytes": 5027783488 }, "…"],
  "filesTruncated": false
}

inLibrary is { modelId, pinnedSha, status } when the repo is already in Kaname.

GET /api/v1/models

models:read. The library, filtered like the library page:

Parameter Meaning
q Search text; every word must match (repo, author, family, quant, tags, …)
author, family, quant, tag Facets; repeat or comma-separate values (OR within a facet, AND across)
status pending, downloading, ready, incomplete, failed, deleting, pending_approval, update_available, repo_missing, commit_missing
requested_by Token name
{ "count": 1, "models": [{ "id": "01M…", "repoId": "unsloth/Qwen3-8B-GGUF", "status": "ready", "pendingApproval": false,
  "pinnedSha": "9e68…", "quants": ["Q4_K_M"], "tags": ["agent"], "selectedFiles": 1, "selectedBytes": 5027783488,
  "onDiskBytes": 5027783488, "onDisk": "5.0 GB", "updateAvailable": false, "hubState": "available",
  "requestedBy": "hermes", "createdAt": "…", "downloadedAt": "…" }] }

GET /api/v1/models/{id}

models:read. One model with its selected files (path, bytes, state) and latest job.

POST /api/v1/downloads

downloads:create. Request a download of a repo at a pinned commit: take sha from the lookup.

Field
repoId "org/name"
sha Full 40-character commit SHA
quant e.g. "Q4_K_M": that quant's weight files (all split parts) plus the companion files ticked in Settings → Downloads (chat template, mmproj, …), as in the Add model dialog
include Instead of quant: glob patterns, e.g. ["*.safetensors", "*.json"]
tags Optional user tags for the model
revision Optional branch to track for update checks (default main)
curl -s -X POST -H "Authorization: Bearer $KANAME_TOKEN" -H "Content-Type: application/json" \
  -d '{"repoId":"unsloth/Qwen3-8B-GGUF","sha":"9e6855bc…","quant":"Q4_K_M"}' \
  "$KANAME_URL/api/v1/downloads"
{ "created": true, "modelId": "01M…", "jobId": "01M…", "state": "pending_approval",
  "requestedBytes": 5027783488, "requestedSize": "5.0 GB", "files": ["Qwen3-8B-Q4_K_M.gguf"],
  "message": "Requested; waiting for the user to approve it in Kaname. Do not retry: check GET /api/v1/jobs/01M… every few minutes." }
  • 201: a new job, state pending_approval (approval mode approve) or queued (auto).
  • 200 with "created": false: the same repo, commit and files were requested before; you get the existing model and its latest job. Nothing is queued twice.
  • If the repo is already in the library at the same commit, the request adds the files that are missing from its selection (only the added bytes count against the guardrails). At another commit it is refused: updating a model is done in the UI.

It uses the same code path as the Add model dialog (the file list is fetched again from the Hub at that commit; the client's list isn't trusted).

GET /api/v1/jobs/{id}[?log=N]

models:read. Compact job state:

{ "id": "01M…", "modelId": "01M…", "repoId": "unsloth/Qwen3-8B-GGUF", "kind": "download", "purpose": "initial",
  "state": "downloading", "cancelRequested": false, "requestedBy": "hermes",
  "progress": { "doneBytes": 3000000000, "totalBytes": 5027783488, "percent": 59.7, "done": "3.0 GB", "total": "5.0 GB",
                "speedBytesPerSecond": 52000000, "etaSeconds": 39 },
  "error": null,
  "files": [{ "path": "Qwen3-8B-Q4_K_M.gguf", "state": "downloading", "doneBytes": 3000000000, "totalBytes": 5027783488 }],
  "createdAt": "…", "startedAt": "…", "finishedAt": null,
  "next": "Downloading; check again in a minute." }

States: pending_approval, queued, downloading, verifying, completed, failed, cancelled (also when the user rejected the request), interrupted. The hf log is only included with ?log=N (the last N lines, at most 200).

GET /api/v1/disk

models:read. Space on the models folder, as reported by the worker every few seconds:

{ "workerOnline": true, "freeBytes": 945600000000, "totalBytes": 2000000000000, "usedBytes": 1054400000000,
  "reservedBytes": 105454144, "minFreeBytes": 100000000000, "availableForRequestsBytes": 845494545856,
  "free": "945.6 GB", "total": "2000.0 GB", "availableForRequests": "845.5 GB" }

reservedBytes is what open downloads still have to fetch; availableForRequests is what a request may use before hitting the minimum-free guardrail.

Guardrails

Settings → API tokens → Guardrails for API downloads. They apply to every token together; downloads started in the UI aren't affected.

Setting Default Rejection
Approval Ask me: requests wait as pending approval until you approve or reject them on the model or job page (not an error; state: pending_approval)
Max size per request 60 GB 422 too_large
Keep free on the models folder 100 GB, after the download and after what other open downloads still need 507 disk_space (503 disk_unknown if the worker is offline)
Agent jobs at once 2 (pending approval, queued or running) 429 too_many_jobs
Allowed authors empty = any; e.g. unsloth, ggml-org, Qwen (case-insensitive) 403 author_not_allowed

Rejecting a request cancels its job. A model that only that request added leaves the library again; for a request that added files to an existing model, the previous selection is restored.

Behind a forward-auth proxy (Traefik)

If Kaname sits behind forward auth (e.g. Traefik with Tinyauth or Pocket ID), the proxy would answer API calls with a login redirect. Let /api/v1/ through without forward auth; Kaname checks the bearer token itself there. Everything else keeps forward auth.

Two routers for the same host, the API one with a higher priority and no auth middleware:

# docker compose labels on the kaname "web" service
labels:
  - traefik.enable=true
  - traefik.http.services.kaname.loadbalancer.server.port=3000

  # UI and internal API: forward auth as before
  - traefik.http.routers.kaname.rule=Host(`models.example.com`)
  - traefik.http.routers.kaname.entrypoints=websecure
  - traefik.http.routers.kaname.tls=true
  - traefik.http.routers.kaname.middlewares=tinyauth
  - traefik.http.routers.kaname.service=kaname

  # Token API: no forward auth (bearer tokens are checked by Kaname)
  - traefik.http.routers.kaname-api.rule=Host(`models.example.com`) && PathPrefix(`/api/v1/`)
  - traefik.http.routers.kaname-api.entrypoints=websecure
  - traefik.http.routers.kaname-api.tls=true
  - traefik.http.routers.kaname-api.priority=100
  - traefik.http.routers.kaname-api.service=kaname

The same in a file provider:

http:
  routers:
    kaname-api:
      rule: Host(`models.example.com`) && PathPrefix(`/api/v1/`)
      entryPoints: [websecure]
      tls: {}
      priority: 100
      service: kaname

Notes:

  • Use PathPrefix(/api/v1/) with the trailing slash, so nothing else that starts with /api/v1 slips past forward auth. Kaname's own login gate also excludes exactly /api/v1 and /api/v1/….
  • Don't exempt /api as a whole: the internal routes rely on the login session.
  • Agents on the same network can also call Kaname directly (http://<host>:3000/api/v1/…) without going through the proxy.