Events

Firstly, make sure you have the inngest package installed via NPM or yarn:

npm install inngest  # or yarn add inngest

Start with creating an Inngest client. An "Event Key" is required to send events (How to create an Event Key):

import { Inngest } from 'inngest';

// Recommended: Set an INNGEST_EVENT_KEY environment variable for automatic configuration:
const inngest = new Inngest({ name: "Your app name" });

// Or you can pass the eventKey explicitly to the constructor:
const inngest = new Inngest({ name: "Your app name", eventKey: "xyz..." });

Then, sending an event is simple, just call the the send() method with your event payload (Read about the payload format):

// This sends an event to Inngest.
await inngest.send({
  // The event name
  name: "storefront/cart.checkout.completed",
  // The event's data
  data: {
    cartId: "ed12c8bde",
    itemIds: ["9f08sdh84", "sdf098487", "0fnun498n"],
    account: {
      id: 123,
      email: "test@example.com",
    },
  }
});

👉 You should always use await or .then() with send() to ensure that the event has been sent to Inngest. Serverless platforms may terminate early and prevent an event from being successfully sent.

Sending this event, named storefront/cart.checkout.completed, to Inngest will do two things:

  1. Automatically run any functions that are triggered by this specific event, passing the event payload to the function's arguments.
  2. Store the event payload in Inngest cloud, creating a new event type and event version if it doesn't already exist.

💡 One event can trigger multiple functions, enabling you to consume a single event in multiple ways. This is different than traditional message queues where only one worker can consume a single message.

Sending multiple events at once

You can also send multiple events in a single send call. This enables you to send a batch of events very easily. You can send up to 512kb in a single call which means you can send anywhere between 10 and 1000 typically sized payloads at once.

await inngest.send([
  { name: "storefront/cart.checkout.completed", data: { ... } },
  { name: "storefront/coupon.used", data: { ... } },
  { name: "storefront/loyalty.program.joined", data: { ... } },
])

This is especially useful if you have an array of data in your app and you want to send an event for each item in the array:

// This function call might return 10s or 100s of items, so we can use map
// to transform the items into event payloads then pass that array to send:
const importedItems = await api.fetchAllItems();
const events = importedItems.map((item) => ({
  name: "storefront/item.imported",
  data: {
    ...item,
  }
}));
await inngest.send(events);

Deduplication

Often, you may need to prevent duplicate events from being sent and therefore processed by Inngest. If your system could possibly send the same event more than once, you will want to ensure that it is not ingested more than once.

To prevent duplicate events, you can add an id parameter to the event payload. Once Inngest receives an event with an id, any events send with the exact same id will be ignored regardless of the event's payload.

await inngest.send({
  // Your deduplication id must be specific to this event payload.
  // Use something that will not be used across event types, not a generic value like cartId
  id: "cart-checkout-completed-ed12c8bde",
  name: "storefront/cart.checkout.completed",
  data: {
    cartId: "ed12c8bde",
    // ...the rest of the payload's data...
  }
});

The id is global across all event types, so make sure your id isn't a value that will be shared across different event types.

For example, for two events like storefront/item.imported and storefront/item.deleted, do not use the item's id (9f08sdh84) as the event deduplication id. Instead, combine the item's id with the event type to ensure it's specific to that event (e.g. item-imported-9f08sdh84).

Advanced usage

There's more you can do with events, such as user identification, event versioning, and filtering for each key.