News & Announcements

Announcing Background and Scheduled API Routes for Next.js

Web apps are now heavily API-driven, bringing instant data access and interactivity into the user experience. However, for some API requests like batch processing, which must verify and publish large volumes of data resulting in longer request times, additional development work is needed to write and maintain the appropriate serverless logic. This remains true even for popular frameworks like Next.js, and a simpler solution to run API routes asynchronously in the background or on a predefined schedule like a cron job is more ideal.

Today, we’re excited to announce Background and Scheduled API Routes for the Next.js framework. This means developers can build, deploy, preview, and instantly rollback API routes as background and scheduled functions right alongside other serverless functions—all maintained in the same Git repo as frontend code.

Because this capability is implemented through Netlify’s unique Next.js Runtime, developers can use scheduled and background API routes without having to rely on third-party services or write scaffolding code. And most importantly, this new functionality requires minimal configurations. Read more about the new feature in Netlify’s Next.js documentation!

Use Cases for Background and Scheduled API Routes for Next.js

Next.js API Routes can be deployed with a single folder in a Next.js project. This is a great starting point for a lot of scenarios like handling form input or masking the URL of external services, but longer-running or scheduled tasks with API routes still require overhead work. To overcome these shortcomings, developers typically rely on external services such as GitHub Actions, Upstash’s QStash, or Pipedream, or implement other workarounds that increase both complexity and cost.

Background API Routes allow developers to leverage a native functionality within Next.js to deliver a smooth end-user experience that eliminates the need to wait for longer-running tasks to complete. For instance, the following tasks can be pushed to the background:

  • Batch processing
  • Web scraping
  • Media processing/optimization
  • Intensive computation

Similarly, Scheduled API Routes enable Next.js developers to more easily execute tasks such as:

  • Data backups to Git repos
  • Invoice processing
  • Data fetching for self-updating dashboards
  • Abandoned cart recovery

All of the above are natively enabled with zero configuration through Next.js Runtime on Netlify.

Getting Started

You don’t need to install anything to use the new API route types on Netlify. Start by setting the “type” value in the configuration file, as shown in the examples below.

Background API Routes

Implemented using Netlify Background Functions, these are triggered the same way as regular API routes. However, instead of needing to return within 10 seconds, they immediately return a “202 Accepted” response and can then run in the background for up to 15 minutes. To enable them, export the following config object:

export default async (req, res) => {
   // Do something slow
}

export const config = {
  type: 'experimental-background',
}

Note that Netlify Background Functions require Netlify’s Pro plan or above.

Scheduled API Routes

Also known as cron functions, these are functions which are triggered on a schedule rather than via a web request. They will return a 404 if you try to request them normally. To enable them, export the following config:

export default (req, res) => {
  // Do something scheduled
  res.send()
}

export const config = {
  type: 'experimental-scheduled',
  schedule: '@hourly',
}

The schedule field uses cron expression format. Scheduled functions only run once deployed to production. To test them locally, request them like a normal API route. Scheduled functions are available on every Netlify plan.

Achieve more with Next.js and Netlify today

Next.js is one of the most popular frameworks for developers, and we remain committed to providing the most rewarding and seamless developer experience when pushing Next.js apps to production. To learn more about deploying Next.js projects on Netlify and the added value delivered to developers, visit Next.js on Netlify docs.

Keep reading

Recent posts

How do the best dev and marketing teams work together?