Introducing the Inngest TypeScript / JavaScript SDK
Join our Discord
Sign up for free

Writing functions

Follow along to learn how to:

  • Write a background function triggered by an event
  • Write a scheduled function

Writing functions is the same no matter what platform you deploy to — so this example can be used for any TypeScript or JavaScript project (such as NextJS, Netlify, or Lambda).

Install the SDK

Firstly, you need to install the SDK via npm or yarn:

bash
npm install inngest # or yarn add inngest

Writing a background function

Copy the following code into a new file in your project, for example within ./inngest/function.ts

typescript
import { createFunction } from "inngest";
// This function runs the code and will be called in the background.
const job = async ({ event }) => {
return "hello!"; // You can write whatever you want here.
}
export const demoFunction = createFunction<DemoEvent>(
"Demo function", // The name of your function, used for observability.
"demo/demo.event", // The event that triggers this function.
job, // The function code, defined above.
)

This defines a new background job which runs any code that you pass it. This function is automatically triggered in the background whenever the demo/test.event event is received. You can test this function using standard tooling such as Jest or Mocha by exporting the job code and running standard unit tests.

Writing a scheduled function

The following code defines a scheduled function, which runs automatically using the specified cron schedule:

typescript
import { createScheduledFunction } from "inngest";
// This code will be called by the scheduled function.
const job = async () => {
return "hello!"; // You can write whatever you want here.
}
export createScheduledFunction(
"Demo function", // The name of your function, used for observability.
"0 * * * *", // The cron syntax for the function
job, // The function code, defined above.
)

This is very similar to defining an event-driven function, except it uses the createScheduledFunction call and a cron schedule to run your function at the specified interval. This code can be deployed to any platform and will run automatically, without any HTTP calls.

TypeScript

The Inngest SDK lets you strictly type all of your code against your real production data. This powerful feature helps to:

  • Protect you from making breaking changes
  • Encourage discoverability of your events via autocomplete
  • Give you instant feedback as to whether your code will run as intended

To use generated event types, just run the types command on the Inngest CLI. You can do this via npx:

sh
npx inngest-cli types ts [--output my/file.ts]

This will - by default - create a file at ./__generated__/inngest.ts with all of your event types. You can then pass these in to a new Inngest() client.

typescript
import { Inngest } from "inngest";
import { Events } from "./__generated__/inngest";
const inngest = new Inngest<Events>({ name: "My App" });

Or use individual events if using the createFunction helper directly.

typescript
import { createFunction } from "inngest";
import { DemoEvent } from "./__generated__/inngest";
const fn = createFunction<DemoEvent>("Demo function", "demo/demo.event", job);

Finally, sometimes you want to provide type safety before an event has reached your Inngest Cloud account. For this, we can pass extra custom event types to the generated Events type which will be silently overridden when these events appear in Inngest.

typescript
const inngest = new Inngest<
Events<{
"app/user.created": { data: { id: string } };
"app/user.updated": AppUserUpdated;
}>
>({ name: "My App" });

You can learn more about the Inngest SDK's TypeScript support on our Using with TypeScript page.

Next up

Triggering functions: next, you’ll run through how you can trigger these functions by sending events.

Deploying: then, you’ll learn how to deploy to your platform of choice so that they’re live.