Script does not work with environmental variables

Hi, I am using this function to get contact form submit data and send it via sendgrid. However, once I change the API keys to environment variables it does not work anymore since it cannot get those environment variables from my pages directory. Do you have any ideas how to solve it?
export async function onRequestPost(context) {
try {
return await handleRequest(context);
} catch (e) {
console.error(e);
return new Response("Error sending message", { status: 500 });
}
}
async function handleRequest({ request }) {
try {
const ip = request.headers.get("CF-Connecting-IP");
const formData = await request.formData();
...
const token = formData.get("cf-turnstile-response");
const tokenValidated = await validateToken(ip, token);
if (!tokenValidated) {
return new Response("Token validation failed", { status: 403 });
}

await forwardMessage(
...
);

return new Response("OK", { status: 200 });
} catch (error) {
console.error("Error handling request:", error);
return new Response("Error handling request", { status: 500 });
}
}

async function validateToken(ip, token) {
const formData = new FormData();
formData.append("secret", env.TURNSTILE_SECRET);
formData.append("response", token);
formData.append("remoteip", ip);

const url = "https://challenges.cloudflare.com/turnstile/v0/siteverify";

const result = await fetch(url, {
body: formData,
method: "POST",
});

const outcome = await result.json();
console.log(outcome);
return outcome.success;
}

async function forwardMessage(
...
) {
console.log("Form Data:", {
...
});
try {
const sendGridApiKey = env.SENDGRID_CONTACT_API;
const sendGridEndpoint = "https://api.sendgrid.com/v3/mail/send";

....
export async function onRequestPost(context) {
try {
return await handleRequest(context);
} catch (e) {
console.error(e);
return new Response("Error sending message", { status: 500 });
}
}
async function handleRequest({ request }) {
try {
const ip = request.headers.get("CF-Connecting-IP");
const formData = await request.formData();
...
const token = formData.get("cf-turnstile-response");
const tokenValidated = await validateToken(ip, token);
if (!tokenValidated) {
return new Response("Token validation failed", { status: 403 });
}

await forwardMessage(
...
);

return new Response("OK", { status: 200 });
} catch (error) {
console.error("Error handling request:", error);
return new Response("Error handling request", { status: 500 });
}
}

async function validateToken(ip, token) {
const formData = new FormData();
formData.append("secret", env.TURNSTILE_SECRET);
formData.append("response", token);
formData.append("remoteip", ip);

const url = "https://challenges.cloudflare.com/turnstile/v0/siteverify";

const result = await fetch(url, {
body: formData,
method: "POST",
});

const outcome = await result.json();
console.log(outcome);
return outcome.success;
}

async function forwardMessage(
...
) {
console.log("Form Data:", {
...
});
try {
const sendGridApiKey = env.SENDGRID_CONTACT_API;
const sendGridEndpoint = "https://api.sendgrid.com/v3/mail/send";

....
3 Replies
Chris | Warawul Coffee GmbH
Any help? What am I missing with regards to environmental variables in functions
kian
kian13mo ago
env isn't a global, it refers to an parameter that is passed to the handlers (like onRequestPost). async function handleRequest({ request }) should become async function handleRequest({ request, env }), async function validateToken(ip, token) should become async function validateToken(ip, token, env) and then it will work. https://developers.cloudflare.com/pages/platform/functions/api-reference/#eventcontext
Chris | Warawul Coffee GmbH
Thanks I managed to get turnstile API to work with env, but not the sendgrid API
Want results from more Discord servers?
Add your server