Powered by Cloudflare Pages Functions. Your API_KEY secret is securely accessed on the edge.
Notice message
Nova AI routes requests to /api/chat where Cloudflare injects context.env.API_KEY.
Cloudflare Pages Secret Integration
How API_KEY is securely grabbed without exposing it in HTML
Security Architecture: Static HTML runs in the user's browser, so it cannot directly read Cloudflare Secrets. Instead, this frontend sends prompts to /api/chat, and a Cloudflare Pages Function grabs context.env.API_KEY on the server!
1. Create functions/api/chat.js in your repo
export async function onRequestPost(context) {
try {
// Grab the secret API_KEY from Cloudflare Pages environment
const apiKey = context.env.API_KEY;
if (!apiKey) {
return new Response(JSON.stringify({ error: "API_KEY secret not configured in Cloudflare." }), {
status: 500,
headers: { "Content-Type": "application/json" }
});
}
const { messages } = await context.request.json();
const latestPrompt = messages[messages.length - 1]?.content || "";
// Example calling Gemini API (or swap for OpenAI/Anthropic)
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${apiKey}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
contents: [{ parts: [{ text: latestPrompt }] }],
systemInstruction: { parts: [{ text: "You are Nova AI, a helpful, concise, and intelligent assistant." }] }
})
}
);
const data = await response.json();
const reply = data?.candidates?.[0]?.content?.parts?.[0]?.text || "No response generated.";
return new Response(JSON.stringify({ reply }), {
headers: { "Content-Type": "application/json" }
});
} catch (err) {
return new Response(JSON.stringify({ error: err.message }), {
status: 500,
headers: { "Content-Type": "application/json" }
});
}
}
2. Add Secret in Cloudflare Dashboard:
Go to Workers & Pages → Your Project → Settings → Environment Variables, add a variable named API_KEY, click Encrypt, and redeploy.
Local Preview Fallback Mode
Simulate responses if /api/chat is not yet deployed