export default { async fetch(request) { // Autorise seulement les requêtes depuis ton site (optionnel mais recommandé) const corsHeaders = { "Access-Control-Allow-Origin": "*", // Ou remplace * par ton domaine ex: "https://tonsite.com" "Access-Control-Allow-Methods": "POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type", }; if (request.method === "OPTIONS") { return new Response(null, { headers: corsHeaders }); } if (request.method !== "POST") { return new Response("Méthode non autorisée", { status: 405 }); } const { prompt } = await request.json(); // === COLLER TON TOKEN REPLICATE ICI === const REPLICATE_TOKEN = r8_ HTMu9PoUzXdxOVvnet8tLEpLH3sbpWP1Cbb7x const response = await fetch("https://api.replicate.com/v1/predictions", { method: "POST", headers: { "Authorization": `Token ${REPLICATE_TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify({ version: "black-forest-labs/flux-1.schnell", // Tu peux changer le modèle ici input: { prompt } }) }); const data = await response.json(); // On attend que l'image soit prête (polling simple) let result = data; while (result.status !== "succeeded" && result.status !== "failed") { await new Promise(resolve => setTimeout(resolve, 1000)); const poll = await fetch(result.urls.get, { headers: { "Authorization": `Token ${REPLICATE_TOKEN}` } }); result = await poll.json(); } return new Response(JSON.stringify(result), { headers: { ...corsHeaders, "Content-Type": "application/json" } }); } };