Skip to content

API Starter Template

Simple Express API ready to extend. Minimal boilerplate for building serverless APIs.

Terminal window
# Create project
stacksolo init --template api-starter
# Install dependencies
cd my-api
npm install
# Start development
npm run dev
  • Express server on Cloud Functions
  • Health check endpoint
  • Echo endpoint for testing
  • TypeScript + tsup build
├── functions/api/
│ └── src/
│ └── index.ts # API routes
└── stacksolo.config.json
MethodPathDescription
GET/api/healthHealth check
POST/api/echoEcho back request body
// Add to functions/api/src/index.ts
app.get('/api/users', async (req, res) => {
// Your logic here
res.json({ users: [] });
});
app.post('/api/users', async (req, res) => {
const { name, email } = req.body;
// Create user
res.json({ id: '123', name, email });
});

To add Firebase Auth protection:

import { kernel } from '@stacksolo/runtime';
// Protect all routes under /api/protected
app.use('/api/protected', kernel.authMiddleware());
app.get('/api/protected/profile', async (req, res) => {
const { uid, email } = req.user!;
res.json({ uid, email });
});

To add PostgreSQL:

  1. Update stacksolo.config.json:
{
"networks": [{
"databases": [{
"name": "main",
"databaseVersion": "POSTGRES_15"
}],
"functions": [{
"name": "api",
"env": {
"DATABASE_URL": "@database/main.connectionString"
}
}]
}]
}
  1. Install Drizzle:
Terminal window
npm install drizzle-orm postgres
npm install -D drizzle-kit
  1. Create your schema and queries
Terminal window
stacksolo deploy

This creates:

  • Cloud Functions API
  • Load balancer with SSL