Skip to content

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

fix: use GLM Coding API endpoints for model import with region-aware URLs

Chris Staleyfe3f9c86d5c1
diff --git a/src/app/api/providers/[id]/models/route.ts b/src/app/api/providers/[id]/models/route.ts
index d7b419ffea..ef8a8d1a36 100644
--- a/src/app/api/providers/[id]/models/route.ts
+++ b/src/app/api/providers/[id]/models/route.ts
@@ -19,6 +19,16 @@ function getProviderBaseUrl(providerSpecificData: unknown): string | null {
return typeof baseUrl === "string" && baseUrl.trim().length > 0 ? baseUrl : null;
}
+const GLM_MODELS_URLS = {
+ international: "https://api.z.ai/api/coding/paas/v4/models",
+ china: "https://open.bigmodel.cn/api/coding/paas/v4/models",
+} as const;
+
+function getGlmApiRegion(providerSpecificData: unknown): keyof typeof GLM_MODELS_URLS {
+ const data = asRecord(providerSpecificData);
+ return data.apiRegion === "china" ? "china" : "international";
+}
+
type ProviderModelsConfigEntry = {
url: string;
method: "GET" | "POST";
@@ -469,6 +479,32 @@ export async function GET(
});
}
+ if (provider === "glm") {
+ const region = getGlmApiRegion(connection.providerSpecificData);
+ const url = GLM_MODELS_URLS[region];
+ const token = apiKey || accessToken;
+
+ const response = await fetch(url, {
+ method: "GET",
+ headers: {
+ "Content-Type": "application/json",
+ ...(token ? { Authorization: `Bearer ${token}` } : {}),
+ },
+ });
+
+ if (!response.ok) {
+ return NextResponse.json(
+ { error: `Failed to fetch models: ${response.status}` },
+ { status: response.status }
+ );
+ }
+
+ const data = await response.json();
+ const models = data.data || data.models || [];
+
+ return buildResponse({ provider, connectionId, models });
+ }
+
if (isAnthropicCompatibleProvider(provider)) {
let baseUrl = getProviderBaseUrl(connection.providerSpecificData);
if (!baseUrl) {
diff --git a/tests/unit/glm-provider-model-import-route.test.mjs b/tests/unit/glm-provider-model-import-route.test.mjs
new file mode 100644
index 0000000000..9d98d8993c
--- /dev/null
+++ b/tests/unit/glm-provider-model-import-route.test.mjs
@@ -0,0 +1,224 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+
+const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-glm-models-"));
+process.env.DATA_DIR = TEST_DATA_DIR;
+
+const core = await import("../../src/lib/db/core.ts");
+const providersDb = await import("../../src/lib/db/providers.ts");
+const modelsRoute = await import("../../src/app/api/providers/[id]/models/route.ts");
+
+async function resetStorage() {
+ core.resetDbInstance();
+ fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
+ fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
+}
+
+test.after(() => {
+ core.resetDbInstance();
+ fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
+});
+
+test("GLM import uses international coding endpoint when apiRegion is international", async () => {
+ await resetStorage();
+ const connection = await providersDb.createProviderConnection({
+ provider: "glm",
+ authType: "apikey",
+ name: "glm-intl",
+ apiKey: "glm-key",
+ providerSpecificData: { apiRegion: "international" },
+ });
+
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = async (url, init = {}) => {
+ assert.equal(String(url), "https://api.z.ai/api/coding/paas/v4/models");
+ assert.equal(init.headers.Authorization, "Bearer glm-key");
+ assert.equal(init.headers["x-api-key"], undefined);
+ assert.equal(init.headers["anthropic-version"], undefined);
+ return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
+ };
+
+ try {
+ const response = await modelsRoute.GET(
+ new Request(`http://localhost/api/providers/${connection.id}/models`),
+ { params: { id: connection.id } }
+ );
+ assert.equal(response.status, 200);
+ assert.deepEqual(await response.json(), {
+ provider: "glm",
+ connectionId: connection.id,
+ models: [{ id: "glm-5", name: "GLM 5" }],
+ });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+});
+
+test("GLM import uses China coding endpoint when apiRegion is china", async () => {
+ await resetStorage();
+ const connection = await providersDb.createProviderConnection({
+ provider: "glm",
+ authType: "apikey",
+ name: "glm-cn",
+ apiKey: "glm-cn-key",
+ providerSpecificData: { apiRegion: "china" },
+ });
+
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = async (url, init = {}) => {
+ assert.equal(String(url), "https://open.bigmodel.cn/api/coding/paas/v4/models");
+ assert.equal(init.headers.Authorization, "Bearer glm-cn-key");
+ return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
+ };
+
+ try {
+ const response = await modelsRoute.GET(
+ new Request(`http://localhost/api/providers/${connection.id}/models`),
+ { params: { id: connection.id } }
+ );
+ assert.equal(response.status, 200);
+ const body = await response.json();
+ assert.equal(body.provider, "glm");
+ assert.equal(body.models.length, 1);
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+});
+
+test("GLM import defaults to international endpoint when apiRegion is missing", async () => {
+ await resetStorage();
+ const connection = await providersDb.createProviderConnection({
+ provider: "glm",
+ authType: "apikey",
+ name: "glm-default",
+ apiKey: "glm-key",
+ providerSpecificData: {},
+ });
+
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = async (url) => {
+ assert.equal(String(url), "https://api.z.ai/api/coding/paas/v4/models");
+ return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
+ };
+
+ try {
+ const response = await modelsRoute.GET(
+ new Request(`http://localhost/api/providers/${connection.id}/models`),
+ { params: { id: connection.id } }
+ );
+ assert.equal(response.status, 200);
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+});
+
+test("GLM import defaults to international endpoint when apiRegion is invalid", async () => {
+ await resetStorage();
+ const connection = await providersDb.createProviderConnection({
+ provider: "glm",
+ authType: "apikey",
+ name: "glm-bogus",
+ apiKey: "glm-key",
+ providerSpecificData: { apiRegion: "bogus" },
+ });
+
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = async (url) => {
+ assert.equal(String(url), "https://api.z.ai/api/coding/paas/v4/models");
+ return Response.json({ data: [{ id: "glm-5", name: "GLM 5" }] });
+ };
+
+ try {
+ const response = await modelsRoute.GET(
+ new Request(`http://localhost/api/providers/${connection.id}/models`),
+ { params: { id: connection.id } }
+ );
+ assert.equal(response.status, 200);
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+});
+
+test("GLM import prefers apiKey over accessToken and sends only Authorization Bearer", async () => {
+ await resetStorage();
+ const connection = await providersDb.createProviderConnection({
+ provider: "glm",
+ authType: "apikey",
+ name: "glm-both-tokens",
+ apiKey: "glm-api-key",
+ accessToken: "glm-access-token",
+ providerSpecificData: { apiRegion: "international" },
+ });
+
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = async (url, init = {}) => {
+ assert.equal(init.headers.Authorization, "Bearer glm-api-key");
+ assert.equal(init.headers["x-api-key"], undefined);
+ return Response.json({ data: [] });
+ };
+
+ try {
+ const response = await modelsRoute.GET(
+ new Request(`http://localhost/api/providers/${connection.id}/models`),
+ { params: { id: connection.id } }
+ );
+ assert.equal(response.status, 200);
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+});
+
+test("GLM import falls back to accessToken when apiKey is absent", async () => {
+ await resetStorage();
+ const connection = await providersDb.createProviderConnection({
+ provider: "glm",
+ authType: "apikey",
+ name: "glm-access-only",
+ accessToken: "glm-access-token",
+ providerSpecificData: { apiRegion: "international" },
+ });
+
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = async (url, init = {}) => {
+ assert.equal(init.headers.Authorization, "Bearer glm-access-token");
+ return Response.json({ data: [] });
+ };
+
+ try {
+ const response = await modelsRoute.GET(
+ new Request(`http://localhost/api/providers/${connection.id}/models`),
+ { params: { id: connection.id } }
+ );
+ assert.equal(response.status, 200);
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+});
+
+test("GLM import surfaces upstream non-OK status codes", async () => {
+ await resetStorage();
+ const connection = await providersDb.createProviderConnection({
+ provider: "glm",
+ authType: "apikey",
+ name: "glm-error",
+ apiKey: "glm-key",
+ providerSpecificData: { apiRegion: "international" },
+ });
+
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = async () => new Response("bad gateway", { status: 502 });
+
+ try {
+ const response = await modelsRoute.GET(
+ new Request(`http://localhost/api/providers/${connection.id}/models`),
+ { params: { id: connection.id } }
+ );
+ assert.equal(response.status, 502);
+ assert.deepEqual(await response.json(), { error: "Failed to fetch models: 502" });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+});