Skip to content

Templates — Functions

Every template is a complete, working project. Run aerostack init, pick a template, and you have a deployable app in seconds — with tests, dev server, and all bindings pre-configured.

Terminal window
aerostack init my-app --template <template-name>
cd my-app && npm install && aerostack dev

Or just run aerostack init and pick from the interactive menu.


TemplateWhat It BuildsDBWebSocketAICacheQueueStreaming
blankMinimal Worker starterD1YesYesYes
apiREST API (Hono)D1YesYesYes
api-neonREST API + PostgresNeonYesYesYes
ws-chatGroup chat roomYesYes
ws-chat-neonGroup chat + durable historyNeonYes
ws-voice-agentConversational AI agentYesYesYes
ws-multiplayer-gameMultiplayer state syncYesYes
ai-streamSSE AI streamingYesYes

The simplest starting point. A single file with demo routes for every platform binding.

Terminal window
aerostack init my-app --template blank

Routes included:

  • GET / — health check
  • GET /test/db — D1 database CRUD demo (create table, insert, select)
  • GET /test/cache — KV get/set demo
  • GET /test/ai — AI inference demo
  • POST /test/queue — background job enqueue demo

Bindings: DB (D1), CACHE (KV), QUEUE

Best for: Learning Aerostack, exploring bindings, building from scratch.


A production-ready REST API server with Hono framework, D1 database, and structured routing.

Terminal window
aerostack init my-app --template api

Routes included:

  • GET / — welcome message
  • GET /health — JSON health check
  • GET /notes — list notes from D1
  • POST /notes — create a note
  • GET /users/:id — parameterized endpoint
  • GET /test/cache — KV demo
  • GET /test/ai — AI proxy
  • POST /test/queue — queue producer

Bindings: DB (D1), CACHE (KV), QUEUE

Includes:

  • shared/db.ts — D1 schema + table initialization
  • shared/tracing.ts — request/error logging utilities
  • Pre-configured Vitest with Workers pool

Best for: API backends, CRUD apps, form handlers, any project that needs a database.


api-neon — REST API with Neon PostgreSQL

Section titled “api-neon — REST API with Neon PostgreSQL”

Same production-ready API server, but backed by Neon (serverless Postgres) instead of D1. Use this when you need full PostgreSQL features.

Terminal window
aerostack init my-app --template api-neon

Routes included:

  • GET / — welcome
  • GET /health — health check
  • GET /users — list users from Postgres
  • POST /users — create a user
  • GET /test/cache — KV demo
  • GET /test/ai — AI proxy
  • POST /test/queue — queue producer

Bindings: PG (PostgreSQL via Neon HTTP), CACHE (KV), QUEUE

Best for: Projects needing Postgres features (JSON columns, full-text search, stored procedures), migrating existing Postgres apps.


Real-time group chat rooms. Messages broadcast to all connected clients. Last 20 messages stored in KV and replayed to new joiners.

Terminal window
aerostack init my-app --template ws-chat

Endpoints:

  • GET / — health check
  • WS /ws/<roomId> — WebSocket group chat

Message protocol:

// Client → Server
{ "type": "join", "username": "Alice" }
{ "type": "chat", "text": "Hello!" }
{ "type": "ping" }
// Server → Client
{ "type": "joined", "userId": "...", "username": "Alice", "roomId": "general" }
{ "type": "chat", "from": "userId", "username": "Alice", "text": "Hello!", "timestamp": "..." }
{ "type": "system", "message": "Alice joined" }
{ "type": "pong" }

Bindings: CACHE (KV) — last 20 messages per room

Best for: Chat apps, notification feeds, live commenting, any real-time messaging.


ws-chat-neon — Group Chat with Durable History

Section titled “ws-chat-neon — Group Chat with Durable History”

Same group chat, but with full message history stored in Neon PostgreSQL. Messages survive restarts and are SQL-queryable.

Terminal window
aerostack init my-app --template ws-chat-neon

Endpoints:

  • GET / — health check
  • WS /ws/<roomId> — WebSocket group chat
  • GET /rooms/<roomId>/history — HTTP endpoint for last 50 messages

Bindings: PG (PostgreSQL via Neon)

Key differences from ws-chat:

  • Auto-creates messages table on first use
  • New joiners replay last 50 messages from DB (not 20 from KV)
  • HTTP endpoint lets clients fetch history without WebSocket

Best for: Chat apps that need message persistence, audit logs, search across history.


ws-voice-agent — Conversational AI Agent

Section titled “ws-voice-agent — Conversational AI Agent”

A real-time conversational AI over WebSocket. Conversation history persisted in KV with 1-hour TTL. Reconnecting with the same sessionId restores full context.

Terminal window
aerostack init my-app --template ws-voice-agent

Endpoints:

  • GET / — health check + usage info
  • WS /ws?sessionId=<id> — WebSocket AI conversation

Message protocol:

// Client → Server
{ "type": "user_text", "text": "What is the capital of France?" }
{ "type": "clear" } // Reset conversation
{ "type": "ping" }
// Server → Client
{ "type": "status", "historyLength": 5, "sessionId": "abc123" }
{ "type": "assistant_text", "text": "Paris is the capital of France." }
{ "type": "pong" }

Bindings: CACHE (KV) — session history with 1-hour TTL

Key features:

  • Omit sessionId for a fresh session (server generates one)
  • Reuse sessionId to resume a conversation with full context
  • History writes never block AI response (ctx.waitUntil())

Best for: AI chatbots, voice agents, copilot interfaces, any conversational AI product.


ws-multiplayer-game — Multiplayer State Sync

Section titled “ws-multiplayer-game — Multiplayer State Sync”

Real-time multiplayer state synchronization. Player positions broadcast to all connected clients. Room state snapshotted to KV after every event.

Terminal window
aerostack init my-app --template ws-multiplayer-game

Endpoints:

  • GET / — health check
  • GET /rooms/<roomId> — HTTP snapshot of current players (pre-load before connecting)
  • WS /ws/<roomId> — WebSocket state sync

Message protocol:

// Client → Server
{ "type": "join", "name": "Alice" }
{ "type": "move", "x": 10, "y": 20 }
{ "type": "chat", "text": "hi" }
{ "type": "ping" }
// Server → Client
{ "type": "joined", "playerId": "uuid", "name": "Alice", "roomId": "demo-room" }
{ "type": "state", "players": [{ "id": "...", "name": "Alice", "x": 10, "y": 20 }] }
{ "type": "chat", "from": "playerId", "text": "hi" }
{ "type": "system", "message": "Bob left" }

Bindings: CACHE (KV) — room state snapshots

Key features:

  • Every join/move/leave updates KV snapshot
  • HTTP GET /rooms/<roomId> pre-loads full state for new clients
  • Snapshots never block broadcasts (ctx.waitUntil())

Best for: Multiplayer games, collaborative editors, live cursors, shared whiteboards.


Token-by-token AI response streaming over Server-Sent Events (SSE). The standard pattern for chat UIs, copilots, and progressive output.

Terminal window
aerostack init my-app --template ai-stream

Endpoints:

  • GET /generate?prompt=... — non-streaming AI response (for testing)
  • POST /stream — SSE streaming endpoint

SSE event format:

event: start
data: {"message":"Generating..."}
event: token
data: {"token":"Paris","text":"Paris"}
event: token
data: {"token":" is","text":"Paris is"}
event: done
data: {"text":"Paris is the capital of France."}
EventWhenData
startBefore AI call{ message }
tokenEach chunk{ token, text }text is accumulated so far
doneAfter last token{ text } — full response
errorOn failure{ message }

Bindings: AI (via @aerostack/sdk)

Key features:

  • Uses TransformStream for reliable Workers streaming
  • Standard SSE format — works with EventSource and fetch
  • Extensible to multi-turn conversations via sdk.ai.chat()

Best for: Chat UIs, AI copilots, progressive text generation, any frontend that needs streaming responses.


Every template follows the same workflow:

Terminal window
aerostack init my-app --template api
cd my-app

Or use the interactive menu:

Terminal window
aerostack init my-app
# → Select template from list
# → Select database (D1 or Neon)
Terminal window
npm install

Copy the example environment file:

Terminal window
cp .dev.vars.example .dev.vars

Edit .dev.vars with your credentials:

Terminal window
# .dev.vars — local secrets (never commit this file)
AEROSTACK_PROJECT_ID=your-project-id
AEROSTACK_API_KEY=your-api-key
# Only for Neon templates:
DATABASE_URL=postgres://user:pass@ep-xxx.us-east-2.aws.neon.tech/mydb
Terminal window
aerostack dev
# or
npm run dev

Your app is running at http://localhost:8787.

Terminal window
npm test
# or
npm run test:watch

Every template includes sample tests using Vitest with the Cloudflare Workers pool.

Terminal window
aerostack link --write-toml

This writes your project ID into aerostack.toml so deploys go to the right place.

Terminal window
aerostack deploy

Live on Cloudflare’s edge in 300+ data centers.

Terminal window
aerostack secrets set AEROSTACK_API_KEY
# For Neon templates:
aerostack secrets set DATABASE_URL

When running aerostack init, you choose a database:

  • Zero config, zero latency (in-process binding)
  • SQLite syntax
  • Best for most projects
  • Templates: blank, api, ws-chat
  • Full Postgres features (JSON, full-text search, extensions)
  • HTTP connection — no TCP required on Workers
  • Best for Postgres-dependent apps or migrating existing data
  • Templates: api-neon, ws-chat-neon