{"id":13455777,"url":"https://github.com/paulphys/nextjs-cron","last_synced_at":"2025-03-24T08:34:42.447Z","repository":{"id":44682759,"uuid":"344195576","full_name":"paulphys/nextjs-cron","owner":"paulphys","description":"Cron jobs with Github Actions for Next.js apps on Vercel▲","archived":false,"fork":false,"pushed_at":"2023-02-23T13:02:15.000Z","size":30,"stargazers_count":191,"open_issues_count":0,"forks_count":16,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-28T23:33:19.500Z","etag":null,"topics":["cron","github-actions","nextjs","vercel"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paulphys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-03-03T16:44:52.000Z","updated_at":"2024-09-27T00:29:35.000Z","dependencies_parsed_at":"2024-01-13T17:48:05.918Z","dependency_job_id":"0f31df3d-9885-40f5-8bad-2ee47dabd313","html_url":"https://github.com/paulphys/nextjs-cron","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulphys%2Fnextjs-cron","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulphys%2Fnextjs-cron/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulphys%2Fnextjs-cron/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulphys%2Fnextjs-cron/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paulphys","download_url":"https://codeload.github.com/paulphys/nextjs-cron/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245236289,"owners_count":20582398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cron","github-actions","nextjs","vercel"],"created_at":"2024-07-31T08:01:10.944Z","updated_at":"2025-03-24T08:34:42.168Z","avatar_url":"https://github.com/paulphys.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003e **Note**\n\nAs of [February 22nd 2023](https://vercel.com/blog/cron-jobs) Vercel is officially offering built-in cron jobs to trigger your serverless and edge functions. Read the [documentation](https://vercel.com/docs/cron-jobs) to learn more. Keep in mind the feature is only free during its beta phase, it'll be a paid feature for general availability, which means that the GitHub Actions route will remain relevant for a completly free option.\n\n# Next.js Cron\nCron jobs with [Github Actions](https://github.com/features/actions) for Next.js applications on Vercel▲\n\n## Motivation\nSince the Vercel platform is event-driven, therefore not maintaining a running server, you can't really schedule calls on your [API routes](https://nextjs.org/docs/api-routes/introduction) or [Serverless functions](https://vercel.com/docs/serverless-functions/introduction) in your Next.js application.\nAlthough there are many pre-existing services that provide scheduled cron jobs, I ultimately decided that [Github Actions](https://github.com/features/actions) suits my needs the best, since it integrates nicely with any project that already lives on Github, plus it's completely free.\n\n## Get started\nAll Github Actions reside in the directory `.github/workflows/` of your repository and are written in [YAML](https://yaml.org/).\n\n [`.github/workflows/starter.yaml`](https://github.com/baulml/nextjs-cron/blob/master/.github/workflows/starter.yaml) is the most basic workflow to help you get started with Actions.\n \n## Scheduled tasks\nWith [Scheduled events](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#scheduled-events) you can execute tasks at specified intervals. For instance, the provided workflow `.github/workflows/scheduled.yaml` executes a HTTP request with curl every 60 minutes.\n\n```yaml\nname: Hourly cron job\non:\n  schedule:\n    - cron: '*/60 * * * *'\njobs:\n  cron:\n    runs-on: ubuntu-latest\n    steps:\n      - name: Hourly cron job\n        run: |\n          curl --request POST \\\n          --url 'https://example.com/api/task' \\\n          --header 'Authorization: Bearer ${{ secrets.ACTION_KEY }}'\n```\nIf you are having trouble writing cron schedule expressions, take a look at [crontab guru](https://crontab.guru/).\n\n## Next.js API routes\n\n[API routes](https://nextjs.org/docs/api-routes/introduction) and [Serverless functions](https://vercel.com/docs/serverless-functions/introduction) provide a straightforward solution to building your API with Next.js on Vercel.\nAny file inside the folder `pages/api` is mapped to `/api/*` and will be treated as an API endpoint instead of a `page`.\n\nIf you are using serverless functions, regardless of the [Runtime](https://vercel.com/docs/runtimes), you would need to put the files into the `/api/` directory at your project's root.\n\n### Authorization flow\nTo securely trigger API routes and serverless functions with Github Actions, you need to provide an authorization key in the header of your API call, which, when executed, gets compared to a corresponding key in your Next.js application.\n\nYou can achieve this by adding [Encrypted Secrets](https://docs.github.com/en/actions/reference/encrypted-secrets) to your Github repository and passing them with the header of your HTTP request, like shown in the previous code snippet.\nAlong with adding the key to your Github repository, you also need to access it within your Next.js application, preferably through [Environment Variables](https://nextjs.org/docs/basic-features/environment-variables).\n\nThe example `pages/api/example.js` implements this authorization flow.\n\n```js\nexport default function handler(req, res) {\n\n  const { APP_KEY } = process.env;\n  const { ACTION_KEY } = req.headers.authorization.split(\" \")[1];\n\n  try {\n    if (ACTION_KEY === APP_KEY) {\n      // Process the POST request\n      res.status(200).json({ success: 'true' })\n    } else {\n      res.status(401)\n    }\n  } catch(err) {\n    res.status(500)\n  }\n}\n```\n\nUse `pages/api/example.ts` for Typescript.\n\n```ts\nimport type { NextApiRequest, NextApiResponse } from 'next'\n\nexport default function handler(req:NextApiRequest, res:NextApiResponse) {\n\n  const { APP_KEY } = process.env;\n  const { ACTION_KEY } = req.headers.authorization.split(\" \")[1];\n\n  try {\n    if (ACTION_KEY === APP_KEY) {\n      // Process the POST request\n      res.status(200).json({ success: 'true' })\n    } else {\n      res.status(401)\n    }\n  } catch(err) {\n    res.status(500)\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulphys%2Fnextjs-cron","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaulphys%2Fnextjs-cron","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulphys%2Fnextjs-cron/lists"}