cd /news/developer-tools/add-model-fallback-to-an-openai-comp… · home topics developer-tools article
[ARTICLE · art-96245] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Add Model Fallback to an OpenAI-Compatible Node.js App

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.

read2 min views1 publishedAug 14, 2026

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.

This 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.

npm install openai

On macOS or Linux:

export JINZEAI_API_KEY="your_api_key_here"

On PowerShell:

$env:JINZEAI_API_KEY="your_api_key_here"

Never commit a real API key. Rotate it immediately if it appears in a public repository, screenshot, or support message.

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://jinzeai.cc/v1",
  apiKey: process.env.JINZEAI_API_KEY,
});
js
const models = ["deepseek-chat", "qwen-flash"];

async function completeWithFallback(messages) {
  let lastError;

  for (const model of models) {
    try {
      const response = await client.chat.completions.create({
        model,
        messages,
      });

      return {
        model,
        text: response.choices[0].message.content,
      };
    } catch (error) {
      lastError = error;
      console.warn(`${model} failed: ${error.status ?? "unknown status"}`);
    }
  }

  throw lastError;
}

const result = await completeWithFallback([
  {
    role: "user",
    content: "Explain model fallback in one sentence.",
  },
]);

console.log(`Model: ${result.model}`);
console.log(result.text);

The minimal example retries on every error so the control flow is easy to see. A production application should be more selective.

Fallback may be reasonable for:

Do 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.

Available beta models can change. Use your key with GET /v1/models

rather than assuming every model is enabled for every account.

curl https://jinzeai.cc/v1/models \
  -H "Authorization: Bearer $JINZEAI_API_KEY"

JinzeAI 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.

Model availability and test limits may change during beta. Feedback about SDK compatibility, latency, and failure handling is especially useful.

── more in #developer-tools 4 stories · sorted by recency
── more on @openai 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/add-model-fallback-t…] indexed:0 read:2min 2026-08-14 ·