Been trying to make a successful request but everything I try returns a 404 error.
Any advice is appreciated.
router.get("/test-huggingface-debug", async (req, res) => {
try {
// Get API key
const hugKey = [apiKeyHere];
if (!hugKey) {
return res.status(500).json({
success: false,
error: "Hugging Face API key not configured",
});
}
console.log(`API Key loaded, starting with: ${hugKey.substring(0, 3)}..., length: ${hugKey.length}`);
// Use the simplest, most reliable API format with a guaranteed available model
console.log("Attempting direct API call with fetch...");
// Build the URL and options manually
const url = "https://api-inference.huggingface.co/models/gpt2";
const options = {
method: "POST",
headers: {
Authorization: `Bearer ${hugKey}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ inputs: "Hello, I am testing the Hugging Face API." })
};
// Use native fetch API instead of axios
console.log("Sending request to:", url);
const response = await fetch(url, options);
console.log("Response status:", response.status);
if (!response.ok) {
const errorText = await response.text();
console.error("Error response body:", errorText);
throw new Error(`API responded with status ${response.status}: ${errorText}`);
}
const data = await response.json();
console.log("Success! HF Response:", JSON.stringify(data).substring(0, 200));
return res.status(200).json({
success: true,
message: "Successfully connected to Hugging Face API",
data: data
});
} catch (error) {
console.error("Test HF error:", error.message);
return res.status(500).json({
success: false,
error: `Failed to connect to Hugging Face: ${error.message}`
});
}
});