Files
Kaname/src/app/api/api-tokens/route.ts
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

24 lines
1020 B
TypeScript

import { NextResponse } from "next/server";
import { z } from "zod";
import { withAuth } from "@/lib/api-auth";
import { apiError, handleApiError } from "@/lib/api-errors";
import { createToken, listTokens, TokenError } from "@/lib/api-tokens";
export const dynamic = "force-dynamic";
/** API tokens for machine clients (Settings → API tokens). Never includes the tokens themselves. */
export const GET = withAuth(async () => NextResponse.json(await listTokens()));
const Body = z.object({ name: z.string().max(40), scopes: z.array(z.string()).min(1).max(10) });
/** Create a token. The response is the only time the token is shown. */
export const POST = withAuth(async (req) => {
try {
const body = Body.parse(await req.json());
return NextResponse.json(await createToken(body.name, body.scopes), { status: 201, headers: { "Cache-Control": "no-store" } });
} catch (err) {
if (err instanceof TokenError) return apiError(err.status, err.code, err.message);
return handleApiError(err);
}
});