import { Router } from "express"; const router = Router(); const ANTHROPIC_API_URL = "https://api.anthropic.com"; // Express 5 uses named parameters for wildcards router.post("/*path", async (req, res) => { const apiKey = process.env.ANTHROPIC_API_KEY; if (!apiKey) { return res.status(500).json({ error: "API key not configured" }); } // Get the path after /api/claude (e.g., /v1/messages) // Express 5 returns path segments as an array const pathParam = req.params.path; const path = "/" + (Array.isArray(pathParam) ? pathParam.join("/") : pathParam || ""); try { const response = await fetch(`${ANTHROPIC_API_URL}${path}`, { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey, "anthropic-version": "2023-06-01", }, body: JSON.stringify(req.body), }); const data = await response.json(); res.status(response.status).json(data); } catch (error) { console.error("Claude API error:", error.message); res.status(500).json({ error: "Failed to proxy request to Claude API" }); } }); export default router;