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 { KiroService } from "@/lib/oauth/services/kiro";
import {
createProviderConnection,
getProviderConnections,
updateProviderConnection,
isCloudEnabled,
} from "@/models";
import { syncToCloud } from "@/lib/cloudSync";
import { getConsistentMachineId } from "@/shared/utils/machineId";
import { kiroApiKeyImportSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { buildKiroImportError } from "../import/route";
import { buildKiroApiKeyConnectionName, isKiroApiKeyImportClientError } from "./helpers";
import { findKiroConnectionByIdentity } from "@/lib/oauth/kiroConnectionIdentity";
async function requireKiroApiKeyImportAuth(request: Request) {
return requireManagementAuth(request, { invalidApiKeyStatus: 401 });
}
/**
* POST /api/oauth/kiro/api-key
*
* Imports a long-lived Kiro / AWS CodeWhisperer API key. API-key auth has no
* refresh token. The key is validated on the Amazon Q model catalog (the surface
* inference uses) and pinned to the detected region (`auto` probes us-east-1 then
* eu-central-1); profile discovery is best-effort because AWS rejects
* ListAvailableProfiles for API keys while still accepting generation calls.
*/
export async function POST(request: Request) {
const authResponse = await requireKiroApiKeyImportAuth(request);
if (authResponse) return authResponse;
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 { searchParams } = new URL(request.url);
const targetProvider = searchParams.get("targetProvider") === "amazon-q" ? "amazon-q" : "kiro";
const validation = validateBody(kiroApiKeyImportSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const { apiKey, region } = validation.data;
const kiroService = new KiroService();
const credential = await kiroService.validateApiKey(apiKey, region || "auto");
const email = kiroService.extractEmailFromJWT(credential.accessToken);
const name = buildKiroApiKeyConnectionName(targetProvider, credential.region, apiKey);
const record = {
name,
apiKey: credential.accessToken,
accessToken: credential.accessToken,
refreshToken: null,
// Long-lived key with no scheduled refresh. Keep a future timestamp so
// health/token paths do not treat the connection as immediately expired.
expiresAt: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString(),
email: email || null,
providerSpecificData: {
profileArn: credential.profileArn,
region: credential.region,
authMethod: "api_key",
provider: "API Key",
},
testStatus: "active",
isActive: true,
};
const existing = await getProviderConnections({ provider: targetProvider });
const match = findKiroConnectionByIdentity(existing, {
authType: "apikey",
profileArn: credential.profileArn,
email,
name,
});
const connection: any =
typeof match?.id === "string"
? await updateProviderConnection(match.id, record)
: await createProviderConnection({
provider: targetProvider,
authType: "apikey",
...record,
});
await syncToCloudIfEnabled();
return NextResponse.json({
success: true,
connection: {
id: connection.id,
provider: connection.provider,
email: connection.email,
},
});
} catch (error) {
console.error("Kiro API key import error:", error);
return NextResponse.json(
{ error: buildKiroImportError(error) },
{ status: isKiroApiKeyImportClientError(error) ? 400 : 500 }
);
}
}
async function syncToCloudIfEnabled() {
try {
const cloudEnabled = await isCloudEnabled();
if (!cloudEnabled) return;
const machineId = await getConsistentMachineId();
await syncToCloud(machineId);
} catch (error) {
console.log("Error syncing to cloud after Kiro API key import:", error);
}
}