Skip to content

Hooks & Custom Logic

Write hooks that intercept lifecycle events (before/after auth, etc.) or create entirely new API endpoints — all with full SDK access and zero server management.

Hooks run at specific moments in the request/response lifecycle:

  • auth.register.before — validate email domain before account creation
  • auth.login.after — log sign-ins, update last-seen timestamps
  • order.complete.after — calculate loyalty points, send confirmation

Create new endpoints at:

https://api.aerostack.dev/api/v1/custom/<your-endpoint>

These endpoints get the full sdk object pre-wired — no boilerplate required.

Every hook receives the full sdk object:

PrimitiveSDK callWhy use it in hooks?
Databasesdk.db.query()Cross-table joins, custom validation
Cachesdk.cache.set()Store computed results at the edge
Queuesdk.queue.send()Offload heavy tasks to background workers
AIsdk.ai.complete()Categorize input, summarize data in real-time
Realtimesdk.socket.emit()Notify connected clients on lifecycle events
// Hook: auth.register.before
export default async function(sdk, event) {
const { email } = event.data
// Block disposable email providers
const blocklist = ['mailinator.com', 'tempmail.com', '10minutemail.com']
const domain = email.split('@')[1]
if (blocklist.includes(domain)) {
throw new Error('Disposable email addresses are not allowed')
}
// Allow everything else
return { proceed: true }
}
// Custom endpoint: POST /api/v1/custom/analytics/summary
export default async function(sdk, event) {
const { projectId, startDate, endDate } = event.data
const cacheKey = `analytics:${projectId}:${startDate}:${endDate}`
const cached = await sdk.cache.get(cacheKey)
if (cached) return cached
const [signups, orders, revenue] = await sdk.db.batch([
{
sql: 'SELECT COUNT(*) as count FROM users WHERE created_at BETWEEN ? AND ?',
params: [startDate, endDate]
},
{
sql: 'SELECT COUNT(*) as count FROM orders WHERE created_at BETWEEN ? AND ?',
params: [startDate, endDate]
},
{
sql: 'SELECT SUM(total) as total FROM orders WHERE status = ? AND created_at BETWEEN ? AND ?',
params: ['paid', startDate, endDate]
}
])
const result = {
signups: signups.results[0]?.count ?? 0,
orders: orders.results[0]?.count ?? 0,
revenue: revenue.results[0]?.total ?? 0,
generatedAt: Date.now()
}
await sdk.cache.set(cacheKey, result, { ttl: 300 })
return result
}
  1. Develop — Monaco editor with TypeScript and AI assistance in the dashboard.
  2. Test — Dry-run with mock payloads in the browser or via CLI.
  3. Deploy — Push to the global network in under 1 second. No build pipelines.
  4. Monitor — Trace every execution in the Platform observability dashboard.
Terminal window
# Deploy a hook from local file
aerostack deploy hooks/auth-before-register.ts --hook auth.register.before
# Deploy a custom endpoint
aerostack deploy api/analytics-summary.ts --endpoint analytics/summary