API Template
Template for building a serverless API service
The API template provides a serverless API service built with SST. It follows a clean architecture pattern designed for scalability and maintainability.
You can find the full source code for the API template here.
Project Structure
Let’s explore the key components of the API template:
- Core Package (
/packages/core
)
The core package follows a clean architecture pattern with three main modules:
- Functions Package (
/packages/functions
)
Lambda functions that handle API requests:
- Utils Package (
/packages/utils
)
Shared utilities and tools:
Environment Configuration
Development
Create a .env.sandbox
file:
SST Configuration
The API template uses SST for deployment and infrastructure management. The configuration is defined in sst.config.ts
:
1. App Configuration
name
: Defines the app name used to prefix resourcesremoval
: Retains resources in production, removes them in other stageshome
: Uses AWS as the primary providerproviders
: Configures AWS and Cloudflare settings
2. Infrastructure Configuration
The infrastructure is modularly defined in the /infra
directory and imported into the SST config:
3. Auto-deployment Configuration
The autodeploy configuration manages deployment rules for different branches:
Defines automatic deployment rules:
main
branch → prod stage- Other branches → stage named after branch
Each deployment uses CodeBuild with:
- Large compute resources
- ARM64 architecture for better performance
- Automatic stage naming based on branch
Running Tests Locally
- Create a
.env.test
file with test credentials:
- Run the test suite:
Deployment
The API template supports multiple deployment options:
SST Deployment
Deployment to production is handled by SST Autodeploy when merging to the main branch.
This set up is a modified version of the SST Monorepo setup.
For more details on SST configuration options, refer to the SST Config Documentation.
Was this page helpful?