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:
- Verified the file path is correct (
src/app/api/transform/route.js
). - Ensured that app directory-based routing is enabled.
- 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
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
API Response
I found the cause of the issue. For some reason, in Next.js, relative route fetching doesn’t work.
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.