llms.txt 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # smartbotic-vectorapi
  2. > General-purpose REST API over smartbotic-database. **Multi-project**: every
  3. > collection and document lives under a named project supplied as a URL path
  4. > segment (e.g. `/api/v1/projects/{project}/collections`). **Multi-key**: each
  5. > API key carries a list of project grants (MySQL-style; `["*"]` for all); admin
  6. > keys additionally manage projects and keys. Auth: `Authorization: Bearer <KEY>`.
  7. > Machine-readable spec: `/openapi.json` (OpenAPI 3.1).
  8. ## Projects
  9. Requires a valid key. Admin-only operations are noted.
  10. - `GET /api/v1/projects` — List projects the key may access (admin sees all).
  11. - `POST /api/v1/projects` `{name}` — Create a project (admin).
  12. - `GET /api/v1/projects/{project}` — Get project info (collection/document counts).
  13. - `DELETE /api/v1/projects/{project}` — Drop a project (admin; the `default` project cannot be dropped).
  14. ## Keys (admin)
  15. All key-management endpoints require an admin key.
  16. - `GET /api/v1/keys` — List keys. Secret value is masked; returns `id`, `key_prefix`, `label`, `projects`, `admin`, `created_at`, and `scope` (if present). Use `id` to reference the key in PATCH/DELETE.
  17. - `POST /api/v1/keys` `{label, projects:[], admin?, scope?}` — Generate a new key. Returns the secret key value **once** (store it immediately) plus the stable `id`. Pass a `scope` object (see below) to create a capability-scoped key.
  18. - `PATCH /api/v1/keys/{id}` `{label?, projects?, admin?, scope?}` — Update grants for an existing key, identified by its `id` (not the secret). Pass `scope: null` to clear an existing scope.
  19. - `DELETE /api/v1/keys/{id}` — Revoke a key by `id`. Active sessions using it are immediately invalidated.
  20. ### Capability-scoped keys
  21. A non-admin key may carry a `scope` object that restricts what it can do. Scoped keys are designed to be safely embedded in untrusted clients (browser frontends, mobile apps, n8n workflows). A scoped key cannot manage collections or projects regardless of its project grants.
  22. `scope` fields:
  23. - `rules` (required, non-empty array) — Each rule specifies a `collection` name and the permitted `ops` array (`read`, `list`, `search`, `insert`, `update`, `delete`). A request succeeds only if a matching `{collection, op}` rule exists. A rule may also set `require_human_token: true` (see CAPTCHA below).
  24. - `origins` (array of strings, default empty) — Allowed `Origin` header values. When non-empty, requests without a matching `Origin` header are rejected with 403 (**fails closed** — no origin header means denied). Use this to pin a key to a specific domain.
  25. - `rate_limit_per_min` (integer, default 0 = unlimited) — Per-key request rate limit. When the limit is exceeded the server returns **429** with a `Retry-After: 60` header.
  26. - `expires_at` (integer, default 0 = never) — Unix epoch seconds after which the key is treated as expired; auth returns 401.
  27. ### CAPTCHA-gated operations
  28. A scope rule may set `require_human_token: true`. When this flag is active, the client must supply an `X-Captcha-Token` request header containing the CAPTCHA challenge response. The server verifies the token via the configured provider endpoint and returns **403** `captcha_required` if the token is absent, invalid, or if no provider is configured. Configure the provider via the settings API: `captcha_provider` (`"turnstile"` or `"hcaptcha"`), `captcha_secret` (server-side secret), and `captcha_verify_url` (full verify endpoint URL).
  29. Example scope (insert-only, origin-pinned, rate-limited, expiring):
  30. ```json
  31. {
  32. "rules": [{"collection": "designs", "ops": ["insert"]}],
  33. "origins": ["https://app.example.com"],
  34. "rate_limit_per_min": 60,
  35. "expires_at": 1893456000
  36. }
  37. ```
  38. ## Collections
  39. Project-scoped. A key must be granted the project.
  40. - `POST /api/v1/projects/{project}/collections` `{name, kind:"json"|"vector", vector_dimension?, embedding_model?}` — Create a collection. `vector_dimension` is required for `kind=vector`. `embedding_model` defaults to the server's `default_embedding_model` setting.
  41. - `GET /api/v1/projects/{project}/collections` — List collections with metadata (incl. `document_count`, `size_bytes`). Query params (all applied server-side): `q` (case-insensitive name substring filter), `sort` (`name`|`kind`|`documents`|`size`|`created_at`, default `name`), `desc` (bool).
  42. - `GET /api/v1/projects/{project}/collections/{name}` — Get collection metadata.
  43. - `DELETE /api/v1/projects/{project}/collections/{name}` — Drop a collection and all its documents.
  44. Collection names may not start with `_` or the reserved prefix `vectorapi_`.
  45. ## JSON documents
  46. Arbitrary JSON CRUD under a `kind=json` collection.
  47. - `POST /api/v1/projects/{project}/collections/{name}/documents` `{data:{…}}` — Insert a document. Returns `{id}`.
  48. - `GET /api/v1/projects/{project}/collections/{name}/documents/{id}` — Fetch a document by ID.
  49. - `PUT /api/v1/projects/{project}/collections/{name}/documents/{id}` — Replace a document (full upsert).
  50. - `PATCH /api/v1/projects/{project}/collections/{name}/documents/{id}` — Partially update a document (merge fields).
  51. - `DELETE /api/v1/projects/{project}/collections/{name}/documents/{id}` — Delete a document.
  52. - `GET /api/v1/projects/{project}/collections/{name}/documents` — Find documents.
  53. **Filter grammar**: `?filter=field:op:value` (repeatable; ANDed). Supported ops: `eq`, `ne`, `lt`, `lte`, `gt`, `gte`, `contains`. Example: `?filter=age:gte:30&filter=name:eq:Alice`.
  54. Additional query params: `limit` (default 20), `offset` (default 0), `sort` (field name), `desc` (bool).
  55. ## RAG vectors
  56. Requires a `kind=vector` collection. Vectors are stored with cosine-similarity indexing.
  57. - `POST /api/v1/projects/{project}/collections/{name}/vectors` `{id?, text?, vector?, metadata?}` — Store a vector. Supply either `text` (the server embeds it using the collection's `embedding_model`) or a pre-computed `vector` array. `vector` dimension must match the collection's `vector_dimension`. `metadata` is an arbitrary JSON object stored alongside the vector.
  58. - `POST /api/v1/projects/{project}/collections/{name}/search` `{query_text?, query_vector?, top_k, min_score?, filters?}` — Cosine-similarity search. Supply either `query_text` or `query_vector`. `top_k` (required) limits results. `min_score` (0–1) filters low-confidence matches. `filters` apply the same `field:op:value` grammar to vector metadata.
  59. **Latency tip:** `query_text` triggers a synchronous server-side embedding call to the configured provider, which usually dominates request time (seconds for large models). Cosine search itself is sub-millisecond. If your client issues many searches (e.g. an LLM/n8n agent doing RAG), embed the query once on your side and pass `query_vector` to skip server-side embedding entirely.
  60. **Cache bypass:** Send `Cache-Control: no-store` on a `/vectors` or `/search` request to skip the server-side embedding cache — the text is re-embedded fresh and the result is not stored in the cache.
  61. ## Settings (admin)
  62. All settings endpoints require an admin key.
  63. - `GET /api/v1/settings` — Return current settings as JSON. `openai_api_key` is masked: the field is absent and `openai_api_key_set` (bool) indicates whether a key is configured.
  64. - `PUT /api/v1/settings` `{openai_api_base?, openai_api_key?, default_embedding_model?, cors_origins?, session_ttl_minutes?, webui_enabled?, default_project?, embedding_connect_timeout_sec?, embedding_read_timeout_sec?, embedding_cache_size?, embedding_cache_ttl_sec?, embedding_cache_max_bytes?, embedding_cache_normalize?, embedding_client_pool_size?, captcha_provider?, captcha_secret?, captcha_verify_url?}` — Merge the supplied fields into the current settings and persist. Omit `openai_api_key` or `captcha_secret` (or pass an empty string) to keep the existing secret. Changes are hot-reloaded immediately; cache settings are applied instantly via `reconfigure()`.
  65. ## Stats
  66. - `GET /api/v1/projects/{project}/stats` — Per-project stats (collections, document counts). Accessible to any key granted the project.
  67. - `GET /api/v1/stats` — Server-wide stats including `memory_pressure_level` and an `embedding` object with cache telemetry (`cache_size`, `cache_capacity`, `cache_hits`, `cache_misses`, `cache_hit_ratio`, `cache_bytes`). Admin only.
  68. ## Ops
  69. Public (no auth required):
  70. - `GET /healthz` — Liveness probe; always 200 while the process is running.
  71. - `GET /readyz` — Readiness probe; 200 when the database is reachable, 503 otherwise.
  72. - `GET /openapi.json` — OpenAPI 3.1 machine-readable spec.
  73. - `GET /llms.txt` — This file.
  74. Session login (used by the web UI):
  75. - `POST /ui/login` `{key}` — Exchange an API key for an HttpOnly session cookie (`svapi_session`). The cookie may be used in place of the `Authorization: Bearer` header for all `/api/v1/*` endpoints.