#API Endpoint 404 on Vercel

5 messages · Page 1 of 1 (latest)

misty dagger
#

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 }
    );
  }
};
#

astro.config.mjs

import { defineConfig, envField } from 'astro/config';
import tailwindcss from '@tailwindcss/vite';
import react from '@astrojs/react';
import lottie from 'astro-integration-lottie';
import vercel from '@astrojs/vercel';
import redirectConfig from './src/redirects.json';

// https://astro.build/config
export default defineConfig({
  output: 'server',
  prefetch: {
    prefetchAll: true,
  },
  // @ts-ignore
  redirects: redirectConfig,
  integrations: [react(), lottie()],
  vite: {
    plugins: [tailwindcss()],
  },
  adapter: vercel({
    webAnalytics: {
      enabled: true,
    },
  }),
  env: {
    schema: {
      // PUBLIC_TURNSTILE_SITE_KEY:envField.string({ context: "client", access: "public"}),
      TURNSTILE_SECRET_KEY: envField.string({
        context: 'server',
        access: 'secret',
      }),
      PUBLIC_CMS_API: envField.string({
        context: 'client',
        access: 'public',
      }),
    },
  },
  markdown: {
    shikiConfig: {
      theme: 'github-dark',
    },
  },
});
river plaza
#

You wrote /pages/api/captcha.ts once and /pages/api/turnstile.ts once in your message. Was this a mistake? Maybe you misnamed the file or referenced/requested the wrong name somewhere?

misty dagger
#

Yes my bad
It is src/pages/api/turnstile.ts

river plaza
#

so was this the problem?