Stacks Actions
Server actions are reusable business logic units invoked from routes, events, or CLI commands.
Key Paths
- Core package:
storage/framework/core/actions/src/
- Application actions:
app/Actions/
- Default framework actions:
storage/framework/defaults/app/Actions/
- Framework actions (generated):
storage/framework/actions/
Creating an Action
// app/Actions/NotifyUser.ts
export default {
name: 'NotifyUser',
description: 'Notify user after creation',
async handle(request: any) {
const id = request.get('id')
const name = request.get('name')
console.log(`User ${name} (${id}) created`)
return { success: true }
}
}
Auto-Generated API Actions (useApi Trait)
When a model defines useApi, the framework auto-generates REST actions:
defineModel({
name: 'Product',
traits: {
useApi: {
uri: 'products',
routes: ['index', 'store', 'show', 'update', 'destroy']
}
}
})
This generates:
GET /api/products → Index action (list all)
POST /api/products → Store action (create)
GET /api/products/{id} → Show action (get one)
PUT /api/products/{id} → Update action
DELETE /api/products/{id} → Destroy action
Default Framework Actions (80+)
Authentication Actions
LoginAction — POST /login (validates email + password, returns token + user)
RegisterAction — POST /register
LogoutAction — POST /logout (auth required)
RefreshTokenAction — POST /auth/refresh
CreateTokenAction — POST /auth/token
ListTokensAction — GET /auth/tokens (auth required)
RevokeTokenAction — DELETE /auth/tokens/{id}
GetMeAction — GET /me (auth required)
PasskeyRegistrationAction — passkey authentication options
Dashboard Settings Actions (40+)
For each settings category (AI, Analytics, App, Cache, Cloud, Database, DNS, Email, Environment, FileSystems, Hashing, Library, Logging, Notifications, Payment, Ports, Queue, SearchEngine, Security, Services, Storage, Team, UI):
Get{Category}SettingsAction — read current settings
Update{Category}SettingsAction — update settings
Commerce Actions
- CRUD actions for: Products, Orders, Customers, Payments, Coupons, GiftCards, Reviews, Shipping, DeliveryRoutes, TaxRates, LicenseKeys, etc.
Content Actions
- CRUD actions for: Posts, Pages, Authors, Categories, Tags, Comments
System Actions
HealthAction — GET /health (returns status, uptime, memory, PID, Bun version)
GetUserCountAction — user count for dashboard
GetSubscriberCountAction — subscriber count
- Deployment CRUD actions
- Job monitoring actions
- Notification actions
- Request analytics actions
Action Handler Pattern
Actions receive the enhanced request object:
async handle(request: EnhancedRequest) {
const name = request.get('name') // input value
const email = request.input('email') // alias
const all = request.all() // all input
const user = await request.user() // authenticated user
return { success: true, data: { ... } }
}
Using Actions in Routes
// String-based (auto-loaded)
route.post('/users', 'Actions/CreateUser')
// In events (app/Events.ts)
{ 'user:registered': ['SendWelcomeEmail'] } // action name as listener
CLI Commands
buddy make:action [name] — scaffold a new action
Gotchas
- Application actions go in
app/Actions/
- Framework default actions are in
storage/framework/defaults/app/Actions/
- The
handle() method is required — it receives the request object
- Actions used as event listeners also have a
handle(event) method
- The
useApi model trait auto-generates CRUD actions + routes
- Actions are resolved dynamically at runtime via string names
- The HealthAction at
/health is useful for container health checks
- Login action returns
{ token: string, user: { id, email, name } }
- All dashboard settings actions read/write from the corresponding config files
1---2name: stacks-actions-33description: Use when working with Stacks server actions — creating actions in app/Actions/, auto-generated API actions from the useApi model trait, the 80+ default framework actions (auth, dashboard, commerce, content, deployment, jobs), action request/response handling, or action registration. Covers @stacksjs/actions and storage/framework/defaults/app/Actions/.4license: MIT5---67# Stacks Actions89Server actions are reusable business logic units invoked from routes, events, or CLI commands.1011## Key Paths12- Core package: `storage/framework/core/actions/src/`13- Application actions: `app/Actions/`14- Default framework actions: `storage/framework/defaults/app/Actions/`15- Framework actions (generated): `storage/framework/actions/`1617## Creating an Action1819```typescript20// app/Actions/NotifyUser.ts21export default {22 name: 'NotifyUser',23 description: 'Notify user after creation',2425 async handle(request: any) {26 const id = request.get('id')27 const name = request.get('name')28 console.log(`User ${name} (${id}) created`)29 return { success: true }30 }31}32```3334## Auto-Generated API Actions (useApi Trait)3536When a model defines `useApi`, the framework auto-generates REST actions:3738```typescript39defineModel({40 name: 'Product',41 traits: {42 useApi: {43 uri: 'products',44 routes: ['index', 'store', 'show', 'update', 'destroy']45 }46 }47})48```4950This generates:51- `GET /api/products` → Index action (list all)52- `POST /api/products` → Store action (create)53- `GET /api/products/{id}` → Show action (get one)54- `PUT /api/products/{id}` → Update action55- `DELETE /api/products/{id}` → Destroy action5657## Default Framework Actions (80+)5859### Authentication Actions60- `LoginAction` — POST /login (validates email + password, returns token + user)61- `RegisterAction` — POST /register62- `LogoutAction` — POST /logout (auth required)63- `RefreshTokenAction` — POST /auth/refresh64- `CreateTokenAction` — POST /auth/token65- `ListTokensAction` — GET /auth/tokens (auth required)66- `RevokeTokenAction` — DELETE /auth/tokens/{id}67- `GetMeAction` — GET /me (auth required)68- `PasskeyRegistrationAction` — passkey authentication options6970### Dashboard Settings Actions (40+)71For each settings category (AI, Analytics, App, Cache, Cloud, Database, DNS, Email, Environment, FileSystems, Hashing, Library, Logging, Notifications, Payment, Ports, Queue, SearchEngine, Security, Services, Storage, Team, UI):72- `Get{Category}SettingsAction` — read current settings73- `Update{Category}SettingsAction` — update settings7475### Commerce Actions76- CRUD actions for: Products, Orders, Customers, Payments, Coupons, GiftCards, Reviews, Shipping, DeliveryRoutes, TaxRates, LicenseKeys, etc.7778### Content Actions79- CRUD actions for: Posts, Pages, Authors, Categories, Tags, Comments8081### System Actions82- `HealthAction` — GET /health (returns status, uptime, memory, PID, Bun version)83- `GetUserCountAction` — user count for dashboard84- `GetSubscriberCountAction` — subscriber count85- Deployment CRUD actions86- Job monitoring actions87- Notification actions88- Request analytics actions8990## Action Handler Pattern9192Actions receive the enhanced request object:93```typescript94async handle(request: EnhancedRequest) {95 const name = request.get('name') // input value96 const email = request.input('email') // alias97 const all = request.all() // all input98 const user = await request.user() // authenticated user99100 return { success: true, data: { ... } }101}102```103104## Using Actions in Routes105106```typescript107// String-based (auto-loaded)108route.post('/users', 'Actions/CreateUser')109110// In events (app/Events.ts)111{ 'user:registered': ['SendWelcomeEmail'] } // action name as listener112```113114## CLI Commands115- `buddy make:action [name]` — scaffold a new action116117## Gotchas118- Application actions go in `app/Actions/`119- Framework default actions are in `storage/framework/defaults/app/Actions/`120- The `handle()` method is required — it receives the request object121- Actions used as event listeners also have a `handle(event)` method122- The `useApi` model trait auto-generates CRUD actions + routes123- Actions are resolved dynamically at runtime via string names124- The HealthAction at `/health` is useful for container health checks125- Login action returns `{ token: string, user: { id, email, name } }`126- All dashboard settings actions read/write from the corresponding config files