I have a POST API endpoint in the src/pages/api/turnstile.ts, when I deploy to vercel the endpoint says 404
src/pages/api/turnstile.ts
import { TURNSTILE_SECRET_KEY } from 'astro:env/server';
import type { APIRoute } from 'astro';
export const POST: APIRoute = async ({ request }) => {
const headers = { 'Content-Type': 'application/json' };
try {
const body = await request.json();
const secret = TURNSTILE_SECRET_KEY;
const { token } = body;
if (!token) {
return new Response(
JSON.stringify({ error: 'Turnstile token missing' }),
{ status: 400, headers }
);
}
if (!secret) {
return new Response(
JSON.stringify({ error: 'Server configuration error' }),
{ status: 500, headers }
);
}
// Validate token with Cloudflare
const verifyRes = await fetch(
'https://challenges.cloudflare.com/api/turnstile/v0/siteverify',
{
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
secret,
response: token,
}),
}
);
const verifyJson = await verifyRes.json();
if (!verifyJson.success) {
return new Response(
JSON.stringify({
error: 'Invalid Turnstile token',
details: verifyJson['error-codes'] || [],
}),
{ status: 400, headers }
);
}
return new Response(
JSON.stringify({ message: 'verified', success: true }),
{ status: 200, headers }
);
} catch (error) {
return new Response(
JSON.stringify({
error: 'Server error during verification',
message: error instanceof Error ? error.message : 'Unknown error',
}),
{ status: 500, headers }
);
}
};