Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import { NextResponse } from "next/server";
import { getCostSummary, checkBudget } from "@/domain/costRules";
import { getApiKeys } from "@/lib/db/apiKeys";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
/**
* GET /api/usage/budget/bulk — Bulk budget summary for every API key.
*
* Avoids N+1 in dashboard views that need a per-key snapshot of spend and
* limits in one round-trip. Returns a record keyed by apiKeyId so callers
* can lookup by id without scanning.
*
* Requires management auth: this exposes spend + budget limits for ALL keys,
* so it must not be reachable without the dashboard/management token.
*/
export async function GET(request: Request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
try {
const keys = await getApiKeys();
const budgets: Record<
string,
ReturnType<typeof getCostSummary> & { budgetCheck: ReturnType<typeof checkBudget> }
> = {};
for (const k of keys) {
const id = (k as { id?: string }).id;
if (typeof id !== "string" || !id) continue;
const summary = getCostSummary(id);
budgets[id] = { ...summary, budgetCheck: checkBudget(id) };
}
return NextResponse.json({ budgets });
} catch (error) {
console.error("Error fetching bulk budget summary:", error);
return NextResponse.json({ error: "Failed to fetch bulk budget summary" }, { status: 500 });
}
}