Cron Jobs
Schedule automated tasks like sending reminder emails with ShipAhead’s cron setup.
Tools
- cron-job.org: External service to schedule jobs by calling your API endpoints.
Setup & Run Cron Jobs
- Set CRON_SECRET
- Add an environment variable to secure your cron endpoints:
.env
CRON_SECRET="your-secret-key" - Use any strong key you like or generate one with a password generator.
- Add an environment variable to secure your cron endpoints:
- Add a Cron Job
- Create a file in
/server/jobs/, e.g.,sendReminderEmails.ts:server/jobs/sendReminderEmails.tsexport async function sendReminderEmails() { console.log('[CRON] Running job...'); // Add your logic (DB updates, notifications, etc.) } - Register it in
server/jobs/list.ts:server/jobs/list.tsimport { sendReminderEmails } from './sendReminderEmails'; const JOBS: any = { sendReminderEmails }; export function getJobModules(name: string) { return JOBS[name] ?? null; }
- Create a file in
- Schedule the Job
- Trigger your job via the API endpoint:
https://your-site.com/api/cron/sendReminderEmails - Create a cron-job.org task:
- URL: same as above
- Execution schedule: e.g.,
0 8 * * *(runs daily at 8 AM) - HTTP Header:
X-Cron-Secret: your-secret-key
- Trigger your job via the API endpoint:
- Verify
- Check server logs for
[CRON]messages. - Or test endpoint directly with curl:
curl -H "X-Cron-Secret: your-secret-key" https://your-site.com/api/cron/sendReminderEmails
- Check server logs for
Why Use cron-job.org?
- No need to install
node-cronor run background workers. - Works with serverless platforms like Vercel, Netlify, or Cloudflare.
- Secure with your custom
X-Cron-Secret. - Easy to schedule and manage without touching your app’s main code.
Example: Adding a New Job
- Just create a new file in
/server/jobs/, register it inlist.ts, and schedule it on cron-job.org like above. - You can add unlimited jobs following the same pattern.