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 { errorResponse } from "@omniroute/open-sse/utils/error.ts";
import { getRequestedModelEndpointMismatch } from "@/lib/providerModels/modelEndpointGuard";
import { HTTP_STATUS } from "@omniroute/open-sse/config/constants.ts";
import * as log from "@/sse/utils/logger";
import { enforceApiKeyPolicy } from "@/shared/utils/apiKeyPolicy";
import {
createComboTargetKeyGate,
resolveKeyConnectionScope,
} from "@/sse/services/keyConnectionScope";
import { isRequireApiKeyEnabled } from "@/shared/utils/featureFlags";
import { v1EmbeddingsSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { createEmbeddingResponse, type EmbeddingHandlerOptions } from "@/lib/embeddings/service";
import { resolveClientApiKey } from "@/sse/services/clientApiKeyResolver";
import { withInjectionGuard } from "@/middleware/promptInjectionGuard";
import { getSpecialtyModelsResponse } from "@/app/api/v1/_shared/specialtyCatalog";
export const dynamic = "force-dynamic";
export async function OPTIONS() {
return new Response(null, {
headers: {
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "*",
},
});
}
export async function GET(request?: Request) {
return getSpecialtyModelsResponse(
request,
"/v1/embeddings",
(model) => model.type === "embedding"
);
}
type ValidatedEmbeddingBody = Record<string, unknown> & { model: string };
export async function handleValidatedEmbeddingRequestBody(
body: ValidatedEmbeddingBody,
options: EmbeddingHandlerOptions = {}
) {
return createEmbeddingResponse(body, options);
}
async function postHandler(request, context) {
let rawBody;
try {
rawBody = await request.json();
} catch {
log.warn("EMBED", "Invalid JSON body");
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid JSON body");
}
const validation = validateBody(v1EmbeddingsSchema, rawBody);
if (isValidationFailure(validation)) {
return errorResponse(HTTP_STATUS.BAD_REQUEST, validation.error.message);
}
const body = validation.data;
// Auth check — when REQUIRE_API_KEY=false, ignore presented invalid keys
// so anonymous access works the same as all other client APIs (#7785).
const { apiKey: apiKeyRaw, valid: apiKeyValid } = await resolveClientApiKey(request);
if (isRequireApiKeyEnabled() && !apiKeyRaw) {
return errorResponse(HTTP_STATUS.UNAUTHORIZED, "Authentication required");
}
if (isRequireApiKeyEnabled() && apiKeyRaw && !apiKeyValid) {
return errorResponse(HTTP_STATUS.UNAUTHORIZED, "Invalid API key");
}
// Enforce API key policies (model restrictions + budget limits)
const policy = await enforceApiKeyPolicy(request, body.model);
if (policy.rejection) return policy.rejection;
// GHSA-3w2r: credential selection (direct and per combo target) stays inside
// the key's connection scope, the same scope chat applies.
const scope = await resolveKeyConnectionScope(policy.apiKeyInfo);
if (scope.rejection) return scope.rejection;
// Respect the authenticated API key's credential scope before checking model capabilities.
const endpointMismatch = await getRequestedModelEndpointMismatch(body.model, "embeddings");
if (endpointMismatch) return errorResponse(HTTP_STATUS.BAD_REQUEST, endpointMismatch);
// Extract API key info for logging
const apiKeyMeta = policy.apiKeyInfo;
// Build client raw request for logging
const clientRawRequest = {
endpoint: "/v1/embeddings",
body: rawBody,
headers: Object.fromEntries(request.headers.entries()),
};
return handleValidatedEmbeddingRequestBody(body as ValidatedEmbeddingBody, {
clientRawRequest,
apiKeyId: apiKeyMeta?.id || null,
apiKeyName: apiKeyMeta?.name || null,
connectionId: null,
allowedConnections: scope.allowedConnections,
isComboTargetAllowed: createComboTargetKeyGate(policy, body.model),
});
}
export const POST = withInjectionGuard(postHandler);