api-proxy
api-proxy copied to clipboard
api-proxy
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 }); } }