API Question
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
Is handling requests to an api like this okay to query by a parameter?
or what other approaches would be better?
/api/projects?project_id=${project_id}or what other approaches would be better?
49 Replies
i feel like dynamic param is better and more clear (esp when getting more nested)
If it is a post/put/delete param. I would either pass the ID in the body or use searchparams.
If it's a get method, use search params.
I kinda like to keep all methods in one file "put/post/delete/get/patch" etc.
If it's a get method, use search params.
I kinda like to keep all methods in one file "put/post/delete/get/patch" etc.
that kinda works for one level, but if you have things relating back to project (ie add/change post for project), it seems way more confusing then a simple
PATCH /api/projects/<id>/post/<id> (ie discord api uses similar thing and its so much simpler to undertand and how things relate to eachother then just params)@averydelusionalperson If it is a post/put/delete param. I would either pass the ID in the body or use searchparams.
If it's a get method, use search params.
I kinda like to keep all methods in one file "put/post/delete/get/patch" etc.
Masai LionOP
It’s a GET param, I handle with one single API a get all or by querying by ID
@riský i feel like dynamic param is better and more clear (esp when getting more nested)
Masai LionOP
Dynamic param you mean like I do it?
mb i mean dynmic routes
@riský mb i mean dynmic routes
Masai LionOP
ye I do dynamic routes, but I don’t know if doing the same for an API would be good
Cuz I use dynamic route params and send them to the API with the method on the first message
tho not sure if it’s best practice
in confused whats wrong with dynamic routes?
unless you want to have a really complex api, dynamic route is kinda the way thats been there for ever (ie post for form action)
@riský in confused whats wrong with dynamic routes?
Masai LionOP
like for the API? nothing, tho not sure if it’s good practice
it isn't good practice nor bad practice
it's just one of the ways to use it
@Masai Lion like for the API? nothing, tho not sure if it’s good practice
hmm ok, i havent made my own api much, but i do like the discord one (i cant imagine that being queried)... and any major company that has official api is like that
@averydelusionalperson it isn't good practice nor bad practice
i mean it is up to you and if its for yourself of 3rd parties, but with at least dynamic route you have to privide that param, but with query you can forget or so and more things possible to annoy you
@averydelusionalperson it's just one of the ways to use it
Masai LionOP
Let’s say it’s the basics
I would use dynamic routes if it was an express api, but I prefer to keep all in one file so... I'll use searchparams in Next.js
but dynamic routes is better option IMO
@averydelusionalperson I would use dynamic routes if it was an express api, but I prefer to keep all in one file so... I'll use searchparams in Next.js
yeah i was going to say for a simple express api it is kinda easier, but with things like nextjs they do the hard work and let you do things in more abstracted way
(also me when server action for most anyway and now like uhh maybe people want api so...)
I only use api routes when I need file upload
Masai LionOP
Mmmmm so then I will keep my API as it is
/api/projects?project_id=${project_id}you could
@averydelusionalperson I only use api routes when I need file upload
you do know server actions can do file upload
@Masai Lion Mmmmm so then I will keep my API as it is
/api/projects?project_id=${project_id}
what are your other endpoints
@riský *you do know server actions can do file upload*
I can't when I am using React hook form
(ie any making use of peoject id?)
@averydelusionalperson I can't when I am using React hook form
ah yeah that is why i stopped using as its kinda pain for me - but i wonder why they abstract it so much but dont let you give your own function)
@riský what are your other endpoints
Masai LionOP
None

then just use dynamic routes
i mean have a look at the url length and removing repetition
GET /api/projects?project_id=${project_id}GET /api/projects/${project_id}the only reason id say use query params is if you want to do bulk like
GET /api/projects?project_id=${project_id_a}&project_id=${project_id_b}I would send project id's comma seperated
@riský the only reason id say use query params is if you want to do bulk like
`GET /api/projects?project_id=${project_id_a}&project_id=${project_id_b}`
Masai LionOP
Makes sense, I think I will change it to dynamic routes, and use optional route [[…project_id]] so it ISNT obligatory to pass it, just like I need it to be
if you have 2 params with same name it becoms list, use this info wisely as you can break lots of sites (like my own)
@riský the only reason id say use query params is if you want to do bulk like
`GET /api/projects?project_id=${project_id_a}&project_id=${project_id_b}`
Masai LionOP
I don’t need to pass many tbh
@Masai Lion Makes sense, I think I will change it to dynamic routes, and use optional route [[…project_id]] so it ISNT obligatory to pass it, just like I need it to be
ohh if that is the case, then you could redirect on base :)
@riský ohh if that is the case, then *you could redirect on base :)*
Masai LionOP
What’s that
Wdym
like on
/api/projects you can redirect to whatever the default project_id isEnd of the day, everything depends on you
you can use however you want
depending on your needs
Masai LionOP
Nice
Still doesn’t mean any risk to like security or sum
@Masai Lion Still doesn’t mean any risk to like security or sum
only a risk to security if you make it :) (ie if you fetch user info, ensure they can access)
tho tbf its prob better to just use query param if its this basic and you can have it omitted (not double fetching for some things)