You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发JS Mastery T-Shirt项目调用DALL·E API遇500内部服务器错误

DALL·E API调用500错误排查与修复

核心问题及修复步骤:

  • SDK方法调用错误
    OpenAI SDK v4及以上版本已废弃createImage方法,需替换为images.generate。修改代码中的API调用部分:
    原代码:

    const response = await openai.createImage({
      prompt,
      n: 1,
      size: '1024x1024',
      response_format: 'b64_json'
    });
    

    修改后:

    const response = await openai.images.generate({
      prompt,
      n: 1,
      size: '1024x1024',
      response_format: 'b64_json'
    });
    
  • 缺失body解析中间件
    主Express应用中必须配置express.json()中间件,否则无法读取req.body中的prompt参数,导致后续逻辑出错。在主文件中添加:

    app.use(express.json());
    
  • 优化错误日志定位问题
    原catch块的错误信息不够详细,修改后可以输出具体的API错误详情,便于排查:

    catch (error) {
      console.error('OpenAI API Error:', error.response?.data || error.message);
      res.status(500).json({ 
        message: "Something Went Wrong",
        error: error.response?.data?.error?.message || error.message 
      });
    }
    
  • 验证环境变量有效性
    确认.env文件中的OPENAI_API_KEY配置正确,无多余空格,可通过打印日志验证加载情况:

    console.log('Loaded API Key:', process.env.OPENAI_API_KEY ? '已加载' : '未加载');
    

内容的提问来源于stack exchange,提问作者seed9

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 22:47:34