Can I put the script for a CRON function on the root of my server and allow it to run as a CRON job?
Unanswered
Chinese Egret posted this in #help-forum
Chinese EgretOP
Basically, I am developing an app that uses a CRON function that I only want to run at the given time for the function. By default, it seems that CRON functions go into '/pages/api/', my problem with this is that anyone can send an HTTP request to that URL and it will execute. Is it possible I can just move it to the root of my server ('/') and have it function as I need it to?
6 Replies
@Chinese Egret Basically, I am developing an app that uses a CRON function that I only want to run at the given time for the function. By default, it seems that CRON functions go into '/pages/api/', my problem with this is that anyone can send an HTTP request to that URL and it will execute. Is it possible I can just move it to the root of my server ('/') and have it function as I need it to?
I can just move it to the root of my server ('/') and have it function as I need it to?can you explain this method in more details?
Chinese EgretOP
So pretty much the CRON job I have, I only want the server to be able to execute it at the given schedule time and not have anyone be able to execute manually by just sending an HTTP GET request to the url. The documentation on Vercel's page suggests to place it in the '/pages/api' folder when using Next.JS. However doing so would allow people to do exactly what I don't want, which is unauthorized GET requests. What I am attempting to do is move the CRON function to another location on my server to where the server can execute it, but it cannot be executed by a client through a simple HTTP request.
@Chinese Egret So pretty much the CRON job I have, I only want the server to be able to execute it at the given schedule time and not have anyone be able to execute manually by just sending an HTTP GET request to the url. The documentation on Vercel's page suggests to place it in the '/pages/api' folder when using Next.JS. However doing so would allow people to do exactly what I don't want, which is unauthorized GET requests. What I am attempting to do is move the CRON function to another location on my server to where the server can execute it, but it cannot be executed by a client through a simple HTTP request.
Hmm. And I am asking how do you plan to make the server execute it, because vercel cron is based on http requests
Chinese EgretOP
That's the part I wasn't sure about
You have to make an api route. Protect it with a secret string (/api/cron?secret=hello) so random people cannot trigger the job without knowing the secret
That’s the only known way to protect vercel cron job at the moment that I know of