skip to Main Content

Problem:

I am new to Next.js and trying to implement API routing using the latest version (Next.js 15) with the app-based routing system. However, I keep getting a 404 Not Found error when trying to access the GET API or when making a POST request.

File Structure:

src/
  app/
    api/
      transform/
        route.js
    page.js

Code:

API Route (src/app/api/transform/route.js)

import { NextResponse } from 'next/server';

export async function GET() {
  return NextResponse.json({ message: "Hello from transform API" });
}

export async function POST(request) {
  try {
    // Your logic here
  } catch (error) {
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}

export const runtime = 'nodejs'; // Using Node.js runtime

API Call (src/app/page.js)

const handleTransform = async () => {
  if (!apiKey || !resume || !jobDescription) {
    alert('Please provide all required information');
    return;
  }

  try {
    const formData = new FormData();
    formData.append('resume', resume);
    formData.append('jobDescription', jobDescription);
    formData.append('apiKey', apiKey);

    const response = await fetch('/api/transform', {
      method: 'POST',
      body: formData,
    });

    const data = await response.json();
    if (!response.ok) throw new Error(data.message);

    setTransformedResume(data.transformedResume);
    setAtsScore(data.score);
  } catch (error) {
    console.error(error);
    alert('Transformation failed. Please try again.');
  }
};

Environment:

  • Next.js Version: 15
  • Runtime: Node.js
  • API Location: src/app/api/transform/route.js
  • API Call: src/app/page.js

What I’ve Tried:

  1. Verified the file path is correct (src/app/api/transform/route.js).
  2. Ensured that app directory-based routing is enabled.
  3. Set the runtime to Node.js explicitly (export const runtime = 'nodejs').

Expected Behavior:

The GET endpoint should return:

{ "message": "Hello from transform API" }

And the POST request should execute without issues.


Question:

What am I doing wrong, and how can I fix the 404 error for my API routes?

2

Answers


  1. I just created two projects to reproduce your error, one locally and one in codesandbox: I couldn’t reproduce it.

    So what I suggest you to do is to create an empty project, with the same basic config of your current one, and just add the api folder first, test it and check my basic project here where I pull the message from the transform api using your same route.

    https://codesandbox.io/p/devbox/swgnk9

    Rendering title from API

    Render title from api

    API Response

    codesadbox

    Login or Signup to reply.
  2. I found the cause of the issue. For some reason, in Next.js, relative route fetching doesn’t work.

        const response = await fetch('/api/transform', {
          method: 'POST',
          body: formData,
        });
    

    Try to change the URL here to http://localhost:3000/api/transform, then test, it would probably work. I do not know why, but you can so do like join the API path with the current host (http://localhost:3000 or https://your-project-on-vercel.app) so that it always works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search