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 { NextRequest, NextResponse } from "next/server";
import { CORS_HEADERS, handleCorsOptions } from "@/shared/utils/cors";
import { type LeaderboardScope, getTopN } from "@/lib/gamification/leaderboard";
import { getConnectedServerByKeyHash } from "@/lib/db/gamification";
import crypto from "crypto";
export async function OPTIONS() {
return handleCorsOptions();
}
/**
* GET /api/gamification/federation/leaderboard — Serve leaderboard for federation
*
* Requires bearer token authentication against community_servers.
*/
export async function GET(request: NextRequest) {
// Authenticate: validate bearer token against community_servers
const authHeader = request.headers.get("Authorization");
if (!authHeader?.startsWith("Bearer ")) {
return NextResponse.json(
{ error: "Missing authorization" },
{ status: 401, headers: CORS_HEADERS }
);
}
const token = authHeader.slice(7);
const tokenHash = crypto
.pbkdf2Sync(token, "omniroute-federation-salt", 120000, 32, "sha256")
.toString("hex");
const server = getConnectedServerByKeyHash(tokenHash);
if (!server) {
return NextResponse.json(
{ error: "Invalid or unauthorized token" },
{ status: 403, headers: CORS_HEADERS }
);
}
const url = new URL(request.url);
const scope: LeaderboardScope = (url.searchParams.get("scope") || "global") as LeaderboardScope;
const rawLimit = url.searchParams.get("limit");
const limit = rawLimit === null ? 100 : Number(rawLimit);
if (!Number.isInteger(limit) || limit < 1 || limit > 200) {
return NextResponse.json(
{ error: "'limit' must be an integer between 1 and 200" },
{ status: 400, headers: CORS_HEADERS }
);
}
const entries = await getTopN(scope, limit);
return NextResponse.json(
{
entries: entries.map((e: any) => ({
apiKeyId: e.apiKeyId,
score: e.score,
})),
},
{ headers: CORS_HEADERS }
);
}