Next.js Discord

Discord Forum

Interacting with server actions externally

Answered
Briquet Griffon Vendéen posted this in #help-forum
Open in Discord
Briquet Griffon VendéenOP
Hey guys, I'm currently building an application that will need to interact with Nextjs API routes on behalf of developers. It calls the endpoints of Nextjs apps and does stuff with the data. The problem is that we often use server actions as api routes since they are more convenient.

Is there a way to interact with these server actions externally? (outside of the nextjs app itself)

Thanks!
Answered by Briquet Griffon Vendéen
After a good amount of research, seems like there isn't a sustainable way to predict the id's as the ids are generated at build time, and are specifically hashed to be "unguessable". Seems like this hash is being generated using the filename and function name: https://stackoverflow.com/questions/78835327/how-to-read-nextjs-server-action-function-name-and-parameters-in-middleware-app


On top of that it seems like theres is an encryption key that is rotated every build, that is added to the hash as a salt. Though, this encryption key can be defined by adding a NEXT_SERVER_ACTIONS_ENCRYPTION_KEY env variable in your .env.local. This will keep the same next action id for all your actions throughout different builds as long as you don't change the action function name or location.
View full answer

6 Replies

server actions are just normal api routes in disguise, you can copy the request from the network tab and replay it with your respective params and it would just work
@Yi Lon Ma server actions are just normal api routes in disguise, you can copy the request from the network tab and replay it with your respective params and it would just work
Briquet Griffon VendéenOP
Yea I'm doing that right now haha. Theres a bunch of identifiers in the request. Is there any way we can get these ids and data so that we can programatically send a request to the route?
identifiers like?
Briquet Griffon VendéenOP
The 'next-action' identifier
Briquet Griffon VendéenOP
After a good amount of research, seems like there isn't a sustainable way to predict the id's as the ids are generated at build time, and are specifically hashed to be "unguessable". Seems like this hash is being generated using the filename and function name: https://stackoverflow.com/questions/78835327/how-to-read-nextjs-server-action-function-name-and-parameters-in-middleware-app


On top of that it seems like theres is an encryption key that is rotated every build, that is added to the hash as a salt. Though, this encryption key can be defined by adding a NEXT_SERVER_ACTIONS_ENCRYPTION_KEY env variable in your .env.local. This will keep the same next action id for all your actions throughout different builds as long as you don't change the action function name or location.
Answer