Skip to content

api-proxy #1

Description

@c7kybf2h26-wq

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 });
}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions