Help with dynamic API calls in NEXT.js
Answered
West African Lion posted this in #help-forum
West African LionOP
Hi everyone, I have followed this guide here https://github.com/BastidaNicolas/nextauth-prisma-stripe to create a stripe/nextauth app. This has all worked successfully. I wanted to add an API call in the 'dashboard' page. I have got this same dynamic API call to work in a previous project but can't seem to get it to work in this project. I've tried the following: adding a folder in /api called replicate/predictions which contains [id].js and index.js. I then call these in page.js (dashboard page). I have attached the code and folder structure. The API call just doesn't work at all! do I need to use routes.ts?
Answered by Ray
you need to change the code and the filename
https://nextjs.org/docs/app/building-your-application/routing/route-handlers
https://nextjs.org/docs/app/building-your-application/routing/route-handlers
18 Replies
@West African Lion Hi everyone, I have followed this guide here https://github.com/BastidaNicolas/nextauth-prisma-stripe to create a stripe/nextauth app. This has all worked successfully. I wanted to add an API call in the 'dashboard' page. I have got this same dynamic API call to work in a previous project but can't seem to get it to work in this project. I've tried the following: adding a folder in /api called replicate/predictions which contains [id].js and index.js. I then call these in page.js (dashboard page). I have attached the code and folder structure. The API call just doesn't work at all! do I need to use routes.ts?
you need to change the code and the filename
https://nextjs.org/docs/app/building-your-application/routing/route-handlers
https://nextjs.org/docs/app/building-your-application/routing/route-handlers
Answer
app/api/predictions/route.jsapp/api/predictions/[id]/route.jsWest African LionOP
Hi, thank you! I did have a look at route handling and assumed that was the issue. what do you mean by change the code? is it my route.js files I need to change?
@West African Lion Hi, thank you! I did have a look at route handling and assumed that was the issue. what do you mean by change the code? is it my route.js files I need to change?
yes, your code only work with api route in pages router
but you are using app router
West African LionOP
Will I be able to get this to work in app router? I don;t really know where to start with this
@West African Lion Will I be able to get this to work in app router? I don;t really know where to start with this
sure it will work, read the link and change the code
West African LionOP
is it the route.js code I need to fix or in my page.js?
or both
@West African Lion is it the route.js code I need to fix or in my page.js?
app/api/predictions/page.js to app/api/predictions/route.js
the name of the api route have to be
route.jsWest African LionOP
Thank you, I've done that. confusingly, I have another file called page.js under dashboard. does this need to be changed too? are there changes to how I call the API?
this is where I'm at
West African LionOP
the file in the same directory as [id] is a route, both files are needed for the API request
These are the two files ([id] and page.js) I have moved them both
@West African Lion the file in the same directory as [id] is a route, both files are needed for the API request
if its a page, name it page.js.
if its an api route, name it route.js
if its an api route, name it route.js