Astro Quick Start

In this tutorial you will add Inngest to an Astro app to easily run background tasks and build complex workflows.

Inngest makes it easy to build, manage, and execute durable functions. Some use cases include scheduling drip marketing campaigns, building payment flows, or chaining LLM interactions.

By the end of this ten-minute tutorial you will:

  • Set up and run Inngest on your machine.
  • Write your first Inngest function.
  • Trigger your function from your app and through Inngest Dev Server.

Let's get started!

Before you start: choose a project

In this tutorial you can use any existing Astro project with server-side rendering (SSR) enabled, or you can create a new one.

Instructions for creating a new Astro project

Use the Astro CLI to create a new project:

npm create astro@latest my-astro-app
cd my-astro-app

Astro requires an adapter for server-side API routes. Install the Node.js adapter:

npx astro add node

Ensure your astro.config.mjs includes the adapter and uses server output:

astro.config.mjs

import { defineConfig } from "astro/config";
import node from "@astrojs/node";

export default defineConfig({
  adapter: node({ mode: "standalone" }),
  output: "server",
});

Once you've chosen a project, open it in a code editor.

Starting your project

Start your Astro app in development mode:

npm run dev

By default, Astro runs on port 4321.

Now let's add Inngest to your project.

1. Install the Inngest SDK

In your project directory's root, run the following command to install Inngest SDK:

npm install inngest

2. Run the Inngest Dev Server

Next, start the Inngest Dev Server, which is a fast, in-memory version of Inngest where you can quickly send and view events and function runs. This tutorial assumes that your Astro server will be running on port 4321; change this to match your port if you use another.

npx inngest-cli@latest dev -u http://localhost:4321/api/inngest
You should see a similar output to the following:
$ npx inngest-cli@latest dev -u http://localhost:4321/api/inngest

12:33PM INF executor > service starting
12:33PM INF runner > starting event stream backend=redis
12:33PM INF executor > subscribing to function queue
12:33PM INF runner > service starting
12:33PM INF runner > subscribing to events topic=events
12:33PM INF no shard finder;  skipping shard claiming
12:33PM INF devserver > service starting
12:33PM INF devserver > autodiscovering locally hosted SDKs
12:33PM INF api > starting server addr=0.0.0.0:8288

        Inngest dev server online at 0.0.0.0:8288, visible at the following URLs:

         - http://127.0.0.1:8288 (http://localhost:8288)

        Scanning for available serve handlers.
        To disable scanning run `inngest dev` with flags: --no-discovery -u <your-serve-url>

In your browser open http://localhost:8288 to see the development UI where later you will test the functions you write:

Inngest Dev Server's 'Runs' tab with no data

3. Create an Inngest client

Inngest invokes your functions securely via an API endpoint at /api/inngest. To enable that, you will create an Inngest client in your project, which you will use to send events and create functions.

Create a file in the directory of your preference. We recommend creating an inngest directory inside src for your client and all functions.

src/inngest/index.ts

import { Inngest } from "inngest";

// Create a client to send and receive events
export const inngest = new Inngest({ id: "my-app" });

// Create an empty array where we'll export future Inngest functions
export const functions = [];

4. Set up the Inngest HTTP endpoint

Astro uses file-based routing for API endpoints. Create an API route at src/pages/api/inngest.ts that uses the inngest/astro serve handler:

src/pages/api/inngest.ts

import { serve } from "inngest/astro";
import { inngest, functions } from "../../inngest";

export const { GET, POST, PUT } = serve({
  client: inngest,
  functions,
});

👉 Astro's file-based routing automatically maps src/pages/api/inngest.ts to the /api/inngest endpoint. The inngest/astro serve handler exports GET, POST, and PUT handlers that Astro expects.

5. Write your first Inngest function

In this step, you will write your first durable function. This function will be triggered whenever a specific event occurs (in our case, it will be test/hello.world). Then, it will sleep for a second and return a "Hello, World!".

To define the function, use the createFunction method on the Inngest client.

Learn more: What is createFunction method?

The createFunction method takes three objects as arguments:

  • Configuration: A unique id is required and it is the default name that will be displayed on the Inngest dashboard to refer to your function. You can also specify additional options such as concurrency, rateLimit, retries, or batchEvents, and others.
  • Trigger: event is the name of the event that triggers your function. Alternatively, you can use cron to specify a schedule to trigger this function. Learn more about triggers here.
  • Handler: The function that is called when the event is received. The event payload is passed as an argument. Arguments include step to define durable steps within your handler and additional arguments include logging helpers and other data.

Define a function in the same file where we defined our Inngest client:

src/inngest/index.ts

import { Inngest } from "inngest";

export const inngest = new Inngest({ id: "my-app" });

// Your new function:
const helloWorld = inngest.createFunction(
  { id: "hello-world" },
  { event: "test/hello.world" },
  async ({ event, step }) => {
    await step.sleep("wait-a-moment", "1s");
    return { message: `Hello ${event.data.email}!` };
  },
);

// Add the function to the exported array:
export const functions = [
  helloWorld
];

In the previous step, we configured the exported functions array to be passed to our Inngest HTTP endpoint. Each new function must be added to this array in order for Inngest to read its configuration and invoke it.

Now, it's time to run your function!

6. Trigger your function from the Inngest Dev Server UI

You will trigger your function in two ways: first, by invoking it directly from the Inngest Dev Server UI, and then by sending events from code.

With your Astro app and Inngest Dev Server running, open the Inngest Dev Server UI and select the "Functions" tab http://localhost:8288/functions. You should see your function. (Note: if you don't see any function, select the "Apps" tab to troubleshoot)

Inngest Dev Server web interface's functions tab with functions listed

To trigger your function, use the "Invoke" button for the associated function:

Inngest Dev Server web interface's functions tab with the invoke button highlighted

In the pop up editor, add your event payload data like the example below. This can be any JSON and you can use this data within your function's handler. Next, press the "Invoke Function" button:

{
  "data": {
    "email": "test@example.com"
  }
}
Inngest Dev Server web interface's invoke modal with payload editor and invoke submit button highlighted

The payload is sent to Inngest (which is running locally) which automatically executes your function in the background! You can see the new function run logged in the "Runs" tab:

Inngest Dev Server web interface's runs tab with a single completed run displayed

When you click on the run, you will see more information about the event, such as which function was triggered, its payload, output, and timeline:

Inngest Dev Server web interface's runs tab with a single completed run expanded

In this case, the payload triggered the hello-world function, which did sleep for a second and then returned "Hello, World!". No surprises here, that's what we expected!

Inngest Dev Server web interface's runs tab with a single completed run expanded indicating that hello-world function ran, that it slept for 1s, and that the correct body was returned

To aid in debugging your functions, you can quickly "Rerun" or "Cancel" a function. Try clicking "Rerun" at the top of the "Run details" table:

Run details expanded with rerun and cancel buttons highlighted

After the function was replayed, you will see two runs in the UI:

Inngest Dev Server web interface's runs tab with two runs listed

Now you will trigger an event from inside your app.

7. Trigger from code

Inngest is powered by events.

Learn more: events in Inngest.

It is worth mentioning here that an event-driven approach allows you to:

  • Trigger one or multiple functions from one event, aka fan-out.
  • Store received events for a historical record of what happened in your application.
  • Use stored events to replay functions when there are issues in production.
  • Interact with long-running functions by sending new events including waiting for input and cancelling.

To trigger Inngest functions to run in the background, you will need to send events from your application to Inngest. Once the event is received, it will automatically invoke all functions that are configured to be triggered by it.

To send an event from your code, you can use the Inngest client's send() method.

Learn more: send() method.

Note that with the send method used below you now can:

  • Send one or more events within any API route.
  • Include any data you need in your function within the data object.

In a real-world app, you might send events from API routes that perform an action, like registering users (for example, app/user.signup) or creating something (for example, app/report.created).

You will now send an event from within your Astro app from a /api/hello GET endpoint. Create a new API route:

src/pages/api/hello.ts

import type { APIRoute } from "astro";
import { inngest } from "../../inngest";

export const GET: APIRoute = async () => {
  await inngest.send({
    name: "test/hello.world",
    data: {
      email: "testUser@example.com",
    },
  });

  return new Response(JSON.stringify({ message: "Event sent!" }), {
    headers: { "Content-Type": "application/json" },
  });
};

Every time this API route is requested, an event is sent to Inngest. To test it, open http://localhost:4321/api/hello (change your port if your Astro app is running elsewhere). You should see the following output: {"message":"Event sent!"}

Web browser showing the JSON response of the /api/hello endpoint

If you go back to the Inngest Dev Server, you will see a new run is triggered by this event:

Inngest Dev Server web interface's runs tab with a third run triggered by the 'test/hello.world' event

And - that's it! You now have learned how to create Inngest functions and you have sent events to trigger those functions. Congratulations 🥳

Next Steps

To continue your exploration, feel free to check out:

  • Examples of what other people built with Inngest.
  • Case studies showcasing a variety of use cases.
  • Our blog where we explain how Inngest works, publish guest blog posts, and share our learnings.

You can also read more: