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 { getAuditRequestContext, logAuditEvent } from "@/lib/compliance/index";
import {
getProviderAuditTarget,
summarizeProviderConnectionForAudit,
} from "@/lib/compliance/providerAudit";
import { getCachedProviderConnectionById } from "@/lib/db/readCache";
import { updateProviderConnection } from "@/lib/db/providers";
import { clearRequestRejectedStreak } from "@omniroute/open-sse/services/requestRejectedStreak.ts";
import { deleteProviderConnection } from "@/lib/db/providers/deletion";
import { isCloudEnabled } from "@/lib/db/settings";
import { getConsistentMachineId } from "@/shared/utils/machineId";
import { syncToCloud } from "@/lib/cloudSync";
import { updateProviderConnectionSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import {
normalizeProviderSpecificData,
sanitizeProviderSpecificDataForResponse,
} from "@/lib/providers/requestDefaults";
import {
buildClaudeExtraUsageStateClearUpdate,
isClaudeExtraUsageBlockEnabled,
} from "@/lib/providers/claudeExtraUsage";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { isApiKeyRevealEnabled, maskStoredApiKey } from "@/lib/apiKeyExposure";
import { reconcileMaskedExtraApiKeys } from "@/shared/utils/apiKeyMask";
import { cleanupProviderModelsAfterConnectionDelete } from "@/lib/db/models";
import { canUpdateProviderApiKey } from "@/shared/providers/webSessionCredentials";
import {
refreshConnectionRateLimits,
enableRateLimitProtection,
disableRateLimitProtection,
} from "@/../open-sse/services/rateLimitManager";
// Dynamically imported below, inside the one `provider === "chatgpt-web-codex"`
// branch that needs it -- same fix as #12355 (src/app/api/providers/route.ts),
// which missed this identical pattern in the by-id route. This module's
// transitive chain pulls in tiktoken's WASM tokenizer, which Turbopack dev
// mode fails to resolve for this graph even with `tiktoken` listed in
// serverExternalPackages. A static top-level import evaluates that whole
// chain on EVERY PUT to this route regardless of provider, turning an
// unrelated-provider bundling bug into a route-wide 500 (observed live: PUT
// on a plain openai-compatible connection's rename failed with "Missing
// tiktoken_bg.wasm" after 17-50s, never touching chatgpt-web-codex at all).
import { rejectRetiredCommonChatGptWebProvider } from "@/lib/providers/chatgptWebRetirementResponse";
import { chatGptWebStorageStateFromCookieHeader } from "@omniroute/open-sse/utils/chatgptWebExecutorAdapter.ts";
import { applyOperatorActivationIntent } from "@/lib/providers/operatorDisable";
function normalizeCodexLimitPolicy(
incoming: unknown,
existing: unknown
): { use5h: boolean; useWeekly: boolean } {
const incomingRecord =
incoming && typeof incoming === "object" && !Array.isArray(incoming)
? (incoming as Record<string, unknown>)
: {};
const existingRecord =
existing && typeof existing === "object" && !Array.isArray(existing)
? (existing as Record<string, unknown>)
: {};
const existingUse5h = typeof existingRecord.use5h === "boolean" ? existingRecord.use5h : true;
const existingUseWeekly =
typeof existingRecord.useWeekly === "boolean" ? existingRecord.useWeekly : true;
return {
use5h: typeof incomingRecord.use5h === "boolean" ? incomingRecord.use5h : existingUse5h,
useWeekly:
typeof incomingRecord.useWeekly === "boolean" ? incomingRecord.useWeekly : existingUseWeekly,
};
}
// GET /api/providers/[id] - Get single connection
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
try {
const { id } = await params;
const connection = await getCachedProviderConnectionById(id);
if (!connection) {
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
}
const revealKeys = isApiKeyRevealEnabled();
// Hide or mask sensitive fields
const result: Record<string, any> = { ...connection };
if (!revealKeys) {
result.apiKey = result.apiKey ? maskStoredApiKey(result.apiKey) : undefined;
}
delete result.accessToken;
delete result.refreshToken;
delete result.idToken;
if (result.providerSpecificData) {
result.providerSpecificData = sanitizeProviderSpecificDataForResponse(
result.providerSpecificData,
{ revealKeys }
);
}
return NextResponse.json({ connection: result });
} catch (error) {
console.log("Error fetching connection:", error);
return NextResponse.json({ error: "Failed to fetch connection" }, { status: 500 });
}
}
// PUT /api/providers/[id] - Update connection
export async function PUT(request: Request, { params }: { params: Promise<{ id: string }> }) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
const auditContext = getAuditRequestContext(request);
let rawBody;
try {
rawBody = await request.json();
} catch {
return NextResponse.json(
{
error: {
message: "Invalid request",
details: [{ field: "body", message: "Invalid JSON body" }],
},
},
{ status: 400 }
);
}
try {
const { id } = await params;
const validation = validateBody(updateProviderConnectionSchema, rawBody);
if (isValidationFailure(validation)) {
// never drop an operator's intent silently. Surface the rejected
// keys (field paths and unrecognized-key names) alongside the existing
// error envelope so clients and the UI can tell exactly what was refused.
const rejected = [
...validation.error.details.map((d) => d.field).filter(Boolean),
...validation.error.details.flatMap((d) => d.keys ?? []),
];
return NextResponse.json({ error: { ...validation.error, rejected } }, { status: 400 });
}
const body = validation.data;
const {
name,
priority,
globalPriority,
defaultModel,
isActive,
apiKey,
testStatus,
lastError,
lastErrorAt,
lastErrorType,
lastErrorSource,
errorCode,
rateLimitedUntil,
lastTested,
healthCheckInterval,
group,
maxConcurrent,
quotaWindowThresholds: incomingWindowThresholds,
proxyEnabled,
perKeyProxyEnabled,
quotaVisible,
projectId,
providerSpecificData: incomingPsd,
rateLimitOverrides,
} = body;
const existing = (await getCachedProviderConnectionById(id)) as Record<string, any> | null;
if (!existing) {
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
}
const retirementResponse = rejectRetiredCommonChatGptWebProvider(existing.provider);
if (retirementResponse) return retirementResponse;
const updateData: Record<string, any> = {};
if (name !== undefined) updateData.name = name;
if (priority !== undefined) updateData.priority = priority;
if (globalPriority !== undefined) updateData.globalPriority = globalPriority;
if (defaultModel !== undefined) updateData.defaultModel = defaultModel;
if (isActive !== undefined) updateData.isActive = isActive;
if (apiKey && canUpdateProviderApiKey(existing.authType, existing.provider)) {
if (existing.provider === "chatgpt-web-codex") {
const validationId =
incomingPsd && typeof incomingPsd.validationId === "string"
? incomingPsd.validationId
: "";
try {
const {
finalizeValidatedChatGptWebCodexSecrets,
decodeChatGptWebCodexSecrets,
encodeChatGptWebCodexSecrets,
} = await import("@omniroute/open-sse/services/chatgptWebCodexAdmin.ts");
const incomingSecrets = decodeChatGptWebCodexSecrets(apiKey);
const existingSecrets = decodeChatGptWebCodexSecrets(existing.apiKey || "");
const encoded = encodeChatGptWebCodexSecrets({
cookie: incomingSecrets.cookie,
runtimeKey: incomingSecrets.runtimeKey || existingSecrets.runtimeKey,
});
updateData.apiKey = finalizeValidatedChatGptWebCodexSecrets(
encoded,
validationId
).encodedCredential;
} catch (error) {
return NextResponse.json(
{
error:
error instanceof Error
? error.message
: "Die ChatGPT-Browserprüfung konnte nicht abgeschlossen werden.",
},
{ status: 400 }
);
}
} else if (existing.provider === "chatgpt-web") {
try {
JSON.parse(apiKey);
updateData.apiKey = apiKey;
} catch {
try {
updateData.apiKey = JSON.stringify(chatGptWebStorageStateFromCookieHeader(apiKey));
} catch {
return NextResponse.json(
{ error: "ChatGPT Web storage state JSON or Cookie header is invalid" },
{ status: 400 }
);
}
}
} else {
updateData.apiKey = apiKey;
}
}
if (testStatus !== undefined) updateData.testStatus = testStatus;
if (lastError !== undefined) updateData.lastError = lastError;
if (lastErrorAt !== undefined) updateData.lastErrorAt = lastErrorAt;
if (lastErrorType !== undefined) updateData.lastErrorType = lastErrorType;
if (lastErrorSource !== undefined) updateData.lastErrorSource = lastErrorSource;
if (errorCode !== undefined) updateData.errorCode = errorCode;
if (rateLimitedUntil !== undefined) updateData.rateLimitedUntil = rateLimitedUntil;
// Clearing the cooldown by hand also forgets the refusal streak (#12859).
if (rateLimitedUntil === null || testStatus === "active") clearRequestRejectedStreak(id);
if (lastTested !== undefined) updateData.lastTested = lastTested;
// healthCheckInterval PATCH semantics: undefined = leave as-is; null = clear
// the override (connection follows the global default); 0-1440 = explicit
// per-connection minutes (0 opts this connection out of the sweep).
if (healthCheckInterval === null) updateData.healthCheckInterval = null;
else if (healthCheckInterval !== undefined)
updateData.healthCheckInterval = healthCheckInterval;
if (group !== undefined) updateData.group = group;
if (maxConcurrent !== undefined) updateData.maxConcurrent = maxConcurrent;
if (incomingWindowThresholds !== undefined) {
// PATCH semantics:
// • null → clear every per-window override on this connection
// • {} (empty map) → no-op (no keys to merge); existing overrides preserved
// • partial map → merge into the existing map; a `null` value at any
// key clears just that window's override
if (incomingWindowThresholds === null) {
updateData.quotaWindowThresholds = null;
} else {
const existingMap =
existing.quotaWindowThresholds && typeof existing.quotaWindowThresholds === "object"
? { ...(existing.quotaWindowThresholds as Record<string, number>) }
: {};
for (const [window, value] of Object.entries(incomingWindowThresholds)) {
if (value === null) {
delete existingMap[window];
} else if (typeof value === "number") {
existingMap[window] = value;
}
}
updateData.quotaWindowThresholds =
Object.keys(existingMap).length === 0 ? null : existingMap;
}
}
if (projectId !== undefined) updateData.projectId = projectId;
if (rateLimitOverrides !== undefined) updateData.rateLimitOverrides = rateLimitOverrides;
if (proxyEnabled !== undefined) updateData.proxyEnabled = proxyEnabled;
if (perKeyProxyEnabled !== undefined) updateData.perKeyProxyEnabled = perKeyProxyEnabled;
if (quotaVisible !== undefined) updateData.quotaVisible = quotaVisible;
// Merge providerSpecificData (partial update — preserve existing keys not sent by caller)
if (incomingPsd !== undefined && incomingPsd !== null && typeof incomingPsd === "object") {
const existingPsd =
existing.providerSpecificData && typeof existing.providerSpecificData === "object"
? existing.providerSpecificData
: {};
// The GET routes hand the client masked `extraApiKeys` placeholders (same mask as
// apiKey). Map them back to the stored tokens — and drop orphans — before merging,
// so a placeholder never reaches the database. The health-reset check below reads
// the resolved list.
if ("extraApiKeys" in incomingPsd) {
incomingPsd.extraApiKeys = reconcileMaskedExtraApiKeys(
incomingPsd.extraApiKeys,
(existingPsd as Record<string, unknown>).extraApiKeys
);
}
const mergedPsd = { ...existingPsd, ...incomingPsd };
delete mergedPsd.validationId;
delete mergedPsd.runtimeKey;
// Deep-merge and normalize Codex limit policy defaults.
if (existing.provider === "codex") {
const incomingRecord = incomingPsd as Record<string, unknown>;
if ("codexLimitPolicy" in incomingRecord || "codexLimitPolicy" in existingPsd) {
mergedPsd.codexLimitPolicy = normalizeCodexLimitPolicy(
incomingRecord.codexLimitPolicy,
(existingPsd as Record<string, unknown>).codexLimitPolicy
);
}
}
updateData.providerSpecificData =
normalizeProviderSpecificData(existing.provider, mergedPsd) || {};
const psd = updateData.providerSpecificData as Record<string, any>;
if (psd.apiKeyHealth) {
const health = psd.apiKeyHealth as Record<string, any>;
// If the primary API key was explicitly replaced in this request,
// clear stale health.primary — it no longer corresponds to the
// current key. The next health check will regenerate it.
if (updateData.apiKey !== undefined && updateData.apiKey !== existing.apiKey) {
delete health.primary;
}
// Stale primary guard: no valid primary key → no primary health.
const currentApiKey = updateData.apiKey ?? existing.apiKey ?? null;
if (typeof currentApiKey !== "string" || currentApiKey.length === 0) {
delete health.primary;
}
// Detect whether the extras list was explicitly changed by the caller.
// The index-based mapping (extra_0, extra_1, …) drifts when a key is
// inserted or removed mid-list, so we clear ALL extra health entries
// when the list actually changes and let the next health check regen.
const existingExtras = existingPsd.extraApiKeys;
const incomingExtras = incomingPsd?.extraApiKeys;
const extrasChanged =
Array.isArray(incomingExtras) &&
(!Array.isArray(existingExtras) ||
existingExtras.length !== incomingExtras.length ||
existingExtras.some((v: string, i: number) => v !== incomingExtras[i]));
const extras = psd.extraApiKeys;
const maxExtraIdx = Array.isArray(extras) ? extras.length : 0;
for (const key of Object.keys(health)) {
if (key.startsWith("extra_")) {
if (extrasChanged) {
// Extras modified — index drift possible. Clear all to be safe.
delete health[key];
} else {
// Extras unchanged: only clean out-of-range indices.
const idx = parseInt(key.slice(6), 10);
if (isNaN(idx) || idx >= maxExtraIdx) {
delete health[key];
}
}
}
}
if (Object.keys(health).length === 0) {
delete psd.apiKeyHealth;
}
}
if (!isClaudeExtraUsageBlockEnabled(existing.provider, updateData.providerSpecificData)) {
const clearExtraUsageUpdate = buildClaudeExtraUsageStateClearUpdate({
provider: existing.provider,
testStatus: existing.testStatus,
lastError: existing.lastError,
lastErrorAt: existing.lastErrorAt,
lastErrorType: existing.lastErrorType,
lastErrorSource: existing.lastErrorSource,
errorCode: existing.errorCode,
rateLimitedUntil: existing.rateLimitedUntil,
backoffLevel: existing.backoffLevel,
});
if (clearExtraUsageUpdate) {
Object.assign(updateData, clearExtraUsageUpdate);
}
}
}
// Fields the caller changed, for the audit trail. Captured before the
// operator-intent marker below, which is bookkeeping for isActive and not a
// providerSpecificData edit by the caller.
const changedFields = Object.keys(updateData);
// Record the operator's explicit on/off intent so automated activation paths
// (the connection test) do not turn a deliberately disabled connection back on.
if (typeof isActive === "boolean") {
updateData.providerSpecificData = applyOperatorActivationIntent(
updateData.providerSpecificData ?? existing.providerSpecificData,
isActive
);
}
const updated = await updateProviderConnection(id, updateData);
if (isActive === true && existing.provider === "codex") {
// Await catalog repair so the next request can use a newly enabled account.
// A discovery failure does not undo the operator's activation intent.
try {
const { restoreActivatedConnectionCatalog } =
await import("@/lib/providerModels/prepareConnectionModelTest");
await restoreActivatedConnectionCatalog(existing.provider, id, isActive);
} catch {
console.warn("[Providers] Activated account model catalog could not be refreshed");
}
}
// If rateLimitOverrides was included in the request, refresh the in-memory
// rate limiter state so the change takes effect without a server restart.
// Only (re)enable enforcement when rate limit protection is actually
// persisted for this connection — this route never lets a caller flip
// `rateLimitProtection` itself, so any drift here would silently start
// queuing requests through Bottleneck for a connection whose DB row (and
// the dashboard toggle reading it) both still say "off" (#11278).
if (rateLimitOverrides !== undefined) {
refreshConnectionRateLimits(id, updated?.rateLimitOverrides ?? null);
if (updated?.rateLimitProtection === true) {
enableRateLimitProtection(id);
} else {
disableRateLimitProtection(id);
}
}
// Hide sensitive fields
const result: Record<string, any> = { ...updated };
delete result.apiKey;
delete result.accessToken;
delete result.refreshToken;
delete result.idToken;
if (result.providerSpecificData) {
result.providerSpecificData = sanitizeProviderSpecificDataForResponse(
result.providerSpecificData
);
}
// Auto sync to Cloud if enabled
await syncToCloudIfEnabled();
logAuditEvent({
action: "provider.credentials.updated",
actor: "admin",
target: getProviderAuditTarget(updated || existing),
resourceType: "provider_credentials",
status: "success",
ipAddress: auditContext.ipAddress || undefined,
requestId: auditContext.requestId,
metadata: {
provider: existing.provider,
changedFields,
before: summarizeProviderConnectionForAudit(existing),
after: summarizeProviderConnectionForAudit(updated),
},
});
return NextResponse.json({ connection: result });
} catch (error) {
console.log("Error updating connection:", error);
return NextResponse.json({ error: "Failed to update connection" }, { status: 500 });
}
}
// PATCH /api/providers/[id] - Update connection (partial)
// The OpenAPI spec and the CLI (`omniroute providers rotate`, generated
// api-commands) both use PATCH, but only PUT was implemented — PATCH requests
// 405'd. PATCH and PUT share the same update semantics here (the schema only
// applies provided fields), so delegate to the PUT handler.
export async function PATCH(request: Request, ctx: { params: Promise<{ id: string }> }) {
return PUT(request, ctx);
}
// DELETE /api/providers/[id] - Delete connection
export async function DELETE(request: Request, { params }: { params: Promise<{ id: string }> }) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
const auditContext = getAuditRequestContext(request);
try {
const { id } = await params;
// Fetch connection before deleting to check provider type
const connection = (await getCachedProviderConnectionById(id)) as Record<string, any> | null;
if (!connection) {
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
}
const deleted = await deleteProviderConnection(id);
if (!deleted) {
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
}
// Remove this connection's synced models. Provider-level imported models
// are removed only when this was the provider's final connection.
try {
await cleanupProviderModelsAfterConnectionDelete(connection.provider, id);
} catch (e) {
console.error(`Failed to clean up models for deleted ${connection.provider} connection:`, e);
}
// Auto sync to Cloud if enabled
await syncToCloudIfEnabled();
logAuditEvent({
action: "provider.credentials.revoked",
actor: "admin",
target: getProviderAuditTarget(connection),
resourceType: "provider_credentials",
status: "success",
ipAddress: auditContext.ipAddress || undefined,
requestId: auditContext.requestId,
metadata: {
provider: connection.provider,
connection: summarizeProviderConnectionForAudit(connection),
},
});
return NextResponse.json({ message: "Connection deleted successfully" });
} catch (error) {
console.log("Error deleting connection:", error);
return NextResponse.json({ error: "Failed to delete connection" }, { status: 500 });
}
}
/**
* Sync to Cloud if enabled
*/
async function syncToCloudIfEnabled() {
try {
const cloudEnabled = await isCloudEnabled();
if (!cloudEnabled) return;
const machineId = await getConsistentMachineId();
await syncToCloud(machineId);
} catch (error) {
console.log("Error syncing providers to cloud:", error);
}
}