api-proxy icon indicating copy to clipboard operation
api-proxy copied to clipboard

api-proxy

Open c7kybf2h26-wq opened this issue 6 months ago • 0 comments

export default async function handler(req, res) { // 允许跨域 res.setHeader("Access-Control-Allow-Origin", ""); res.setHeader("Access-Control-Allow-Headers", "");

if (req.method === "OPTIONS") { return res.status(200).end(); }

try { // 你要代理的上游 API(示例:OpenAI/DeepSeek/...) const upstream = "https://api.example.com/v1/chat/completions";

const response = await fetch(upstream, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": `Bearer YOUR_API_KEY`
  },
  body: JSON.stringify(req.body),
});

const data = await response.text(); // 重要:支持流式
res.status(200).send(data);

} catch (err) { res.status(500).json({ error: err.message }); } }

c7kybf2h26-wq avatar May 08 '26 08:05 c7kybf2h26-wq