{"slug": "add-model-fallback-to-an-openai-compatible-node-js-app", "title": "Add Model Fallback to an OpenAI-Compatible Node.js App", "summary": "A developer demonstrates how to add model fallback to an OpenAI-compatible Node.js application using the official OpenAI JavaScript package. The approach tries one model first and switches to a second only on failure, with code examples and guidance on handling errors and checking available models via the JinzeAI endpoint.", "body_md": "A single model can be unavailable, rate-limited, or temporarily slow. If your application already uses an OpenAI-compatible API, a simple fallback can make testing more resilient without introducing another SDK.\n\nThis tutorial uses Node.js and the official OpenAI JavaScript package. It tries one model first and switches to a second model only when the first request fails.\n\n```\nnpm install openai\n```\n\nOn macOS or Linux:\n\n```\nexport JINZEAI_API_KEY=\"your_api_key_here\"\n```\n\nOn PowerShell:\n\n```\n$env:JINZEAI_API_KEY=\"your_api_key_here\"\n```\n\nNever commit a real API key. Rotate it immediately if it appears in a public repository, screenshot, or support message.\n\n``` python\nimport OpenAI from \"openai\";\n\nconst client = new OpenAI({\n  baseURL: \"https://jinzeai.cc/v1\",\n  apiKey: process.env.JINZEAI_API_KEY,\n});\njs\nconst models = [\"deepseek-chat\", \"qwen-flash\"];\n\nasync function completeWithFallback(messages) {\n  let lastError;\n\n  for (const model of models) {\n    try {\n      const response = await client.chat.completions.create({\n        model,\n        messages,\n      });\n\n      return {\n        model,\n        text: response.choices[0].message.content,\n      };\n    } catch (error) {\n      lastError = error;\n      console.warn(`${model} failed: ${error.status ?? \"unknown status\"}`);\n    }\n  }\n\n  throw lastError;\n}\n\nconst result = await completeWithFallback([\n  {\n    role: \"user\",\n    content: \"Explain model fallback in one sentence.\",\n  },\n]);\n\nconsole.log(`Model: ${result.model}`);\nconsole.log(result.text);\n```\n\nThe minimal example retries on every error so the control flow is easy to see. A production application should be more selective.\n\nFallback may be reasonable for:\n\nDo not silently retry authentication errors. An HTTP 401 usually means the key is missing, inactive, or formatted incorrectly. Fix the key instead of sending the same invalid request to another model.\n\nAvailable beta models can change. Use your key with `GET /v1/models`\n\nrather than assuming every model is enabled for every account.\n\n```\ncurl https://jinzeai.cc/v1/models \\\n  -H \"Authorization: Bearer $JINZEAI_API_KEY\"\n```\n\nJinzeAI is testing an OpenAI-compatible endpoint for users outside mainland China. No payment is required during the public beta, and limited free test credit may be available.\n\nModel availability and test limits may change during beta. Feedback about SDK compatibility, latency, and failure handling is especially useful.", "url": "https://wpnews.pro/news/add-model-fallback-to-an-openai-compatible-node-js-app", "canonical_source": "https://dev.to/jinzewang/add-model-fallback-to-an-openai-compatible-nodejs-app-4634", "published_at": "2026-08-14 03:35:25+00:00", "updated_at": "2026-08-14 03:45:21.469772+00:00", "lang": "en", "topics": ["developer-tools", "large-language-models"], "entities": ["OpenAI", "Node.js", "JinzeAI", "deepseek-chat", "qwen-flash"], "alternates": {"html": "https://wpnews.pro/news/add-model-fallback-to-an-openai-compatible-node-js-app", "markdown": "https://wpnews.pro/news/add-model-fallback-to-an-openai-compatible-node-js-app.md", "text": "https://wpnews.pro/news/add-model-fallback-to-an-openai-compatible-node-js-app.txt", "jsonld": "https://wpnews.pro/news/add-model-fallback-to-an-openai-compatible-node-js-app.jsonld"}}