Factory
Applesauce has a two-layer factory system.
applesauce-coreprovides the low-levelEventFactoryclass and the shared operations used to build drafts, modify tags, stamp events, and sign them.applesauce-commonprovides typed factory classes for common Nostr event types like notes, comments, profiles, lists, gift wraps, and direct messages.
What It Is
Use the core layer when you need a generic builder:
ts
import { EventFactory } from "applesauce-core";
const event = await EventFactory.fromKind(1).content("Hello").sign(signer);Use the common layer when you want an event-specific API:
ts
import { NoteFactory } from "applesauce-common/factories";
const note = await NoteFactory.create("Hello #world").sign(signer);How To Use It
Pick the layer that matches your job:
Event Factorycovers the low-level builder inapplesauce-coreCommon Factoriescovers typed factories inapplesauce-common/factoriesExtending Factoriesshows how to build your own subclassesEvent Operationscovers reusable event transformsTag Operationscovers public and hidden tag transforms
Integration
Factories fit into the rest of Applesauce in a few predictable places:
- Actions create and sign events with typed factories, then publish them
- Account managers supply the signer used by
.sign()or.as(signer) - Event operations and tag operations stay reusable across both core and common factories
ts
import { CommentFactory } from "applesauce-common/factories";
const comment = await CommentFactory.create(parent, "Nice post").sign(signer);Best Practices
- Reach for
applesauce-common/factoriesfirst when a typed factory already exists - Use
EventFactorydirectly for custom kinds or shared low-level flows - Keep reusable logic in operations, not inline event mutation
- Use
.stamp()when you need an unsigned event with a pubkey
Installation
Install applesauce-core for the base builder:
sh
npm install applesauce-coresh
yarn add applesauce-coresh
pnpm add applesauce-coreInstall applesauce-common when you want the typed factory layer:
sh
npm install applesauce-commonsh
yarn add applesauce-commonsh
pnpm add applesauce-common