Next.js Discord

Discord Forum

Api route 404s with stripe

Unanswered
SpicyJungle posted this in #help-forum
Open in Discord
Hey, I can't understand why this 404s. It used to work:

/purchase/[item]/page.tsx
export default function Page({ params }: { params: { item: string } }) {
  const router = useRouter();
  const fetcher = (url: string) => fetch(url).then((res) => res.json());
  const { data, error, isLoading } = useSWR("/api/auth/session", fetcher);
    
  const session = data as Session;
  
  const [clientSecret, setClientSecret] = useState("");

  useEffect(() => {
    fetch("/api/checkout_sessions", {
      method: "POST",
      body: JSON.stringify({ quantity: 1, item: params.item }),
    })
      .then((res) => res.json())
      .then((data) => setClientSecret(data.clientSecret));
  }, [params.item]);

  ...


api/checkout_sessions/route.ts
import { NextRequest, NextResponse } from "next/server";
const stripe = require("stripe")(process.env.STRIPE_SECRET);

export const runtime = "edge"; // 'nodejs' is the default

export async function handler(req: NextRequest) {

  // This does not trigger:
  console.log("in checkout_sessions/route.ts")

  ... handling req

4 Replies