Announcing our new Vercel integration
Join our Discord
Sign up for free

Next.js

Inngest works with NextJS out of the box, and allows you to write background jobs, scheduled functions, and event driven systems using Next's API routes.

Follow along to learn how to:

  1. Integrate your event types within functions
  2. Write functions in NextJS
  3. Serve your functions with a new API route
  4. Register your functions with Inngest after deploying to your hosting service.

Integrating event types into your project

Inngest fully types every event you send, ensuring that your functions and data are always valid. To generate your type library, run:

npx inngest-cli types ts

This will prompt you to log in to your Inngest account, then generate types for every event in your account (plus public events) inside the __generated__ folder.

Writing functions in NextJS

Firstly, create a new directory within the root of your NextJS app called inngest. This will contain all of your functions:

bash
mkdir ./inngest

Within this directory you can write your Inngest functions, eg ./inngest/new_pr.ts:

./inngest/new_pr.ts
typescript
import { createFunction } from "inngest";
// Import the type for the event you want to listen to. This fully types
// the arguments to your function. The types are generated by running
// `npx inngest-cli types ts`.
import { GithubPullRequest } from '../__generated__/inngest';
 
export const newPR = createFunction<GithubPullRequest>("New PR", "github/pull_request", ({ event }) => {
// This function is triggered when the `github/pull_request` event is received via
// a GH webhook.
if (event.data.action === 'opened') {
// New PR opened.
}
});

Serving functions in NextJS

Create a new ./pages/api/inngest.ts file to serve the Inngest function API. Import serve from the inngest/next package to create your endpoint, with all of the functions you'd like to enable:

./pages/api/inngest.ts
typescript
import { serve } from "inngest/next";
import { newPR } from "../../inngest/new_pr";
 
// You must export the serve handler, which hosts all of the provided functions
// under one API endpoint.
export default serve("My app/service name", [newPR]);

This hosts an API endpoint at ${your-url}/api/inngest, which we'll use to call your functions.

serve syntax

serve(appName, [functions]);
serve(appName, [functions], options);

serve arguments

  • appName: The name of your app or microservice, used to group all functions together
  • [functions]: An array of all functions to enable
  • options: An optional object of options, with the type:
ts
type Options = {
/**
* A key used to sign requests to and from Inngest in order to prove that the
* source is legitimate.
*
* You must provide a signing key to communicate securely with Inngest. If
* your key is not provided here, we'll try to retrieve it from the
* `INNGEST_SIGNING_KEY` environment variable.
*
* You can retrieve your signing key from the Inngest UI inside the "Secrets"
* section at {@link https://app.inngest.com/secrets}. We highly recommend
* that you add this to your platform's available environment variables as
* `INNGEST_SIGNING_KEY`.
*
* If no key can be found, you will not be able to register your functions or
* receive events from Inngest.
*/
signingKey?: string;
/**
* Controls whether a landing page with introspection capabilities is shown
* when a `GET` request is performed to this handler.
*
* Defaults to true if NODE_ENV !== production.
*
* This page is highly recommended when getting started in development,
* testing, or staging environments.
*/
landingPage?: boolean;
/**
* The URL used to register functions with Inngest.
* Defaults to https://api.inngest.com/fn/register
*/
inngestRegisterUrl?: string;
/**
* If provided, will override the used `fetch` implementation. Useful for
* giving the library a particular implementation if accessing it is not done
* via globals.
*
* By default the library will try to use the native Web API fetch, falling
* back to a Node implementation if no global fetch can be found.
*/
fetch?: typeof fetch;
}

Checking function configuration

To make sure everything is set up as you expect it to be, Inngest provides a local dashboard to introspect your served handler, show any found functions, and raise any errors before you get to production.

Visit ${your-url}/api/inngest (usually http://localhost:3000/api/inngest) to see the dashboard. If there are any problems detected, they'll show up here.

The SDK's local dashboard showing success

Deploying your Next.js application

When you deploy your application, Inngest needs to know where your applicaiton is running so it can call your functions. You can "register" your app in a couple of ways:

Registering automatically

Use one of our officially supported integrations which will automatically notify Inngest whenever you've deployed updated functions:

Registering manually

If you're deploying your appliction to another platform, you can register your app via the Inngest UI or via our API with a curl request.