Getting Started with CalmAPI
Learn how to install CalmAPI and create your first project in minutes.
Prerequisites#
Before you begin, make sure you have:
- Node.js (v14 or higher) - Download here
- MongoDB - Download here or use MongoDB Atlas (free cloud database)
- Basic JavaScript knowledge
- A code editor (VS Code recommended)
Installation#
Step 1: Install CalmAPI CLI Globally#
Open your terminal and run:
npm install -g calmapiThis installs the CalmAPI command-line tool globally on your system.
Step 2: Verify Installation#
Check if it's installed correctly:
calmapi --versionYou should see the version number (e.g., 0.2.5).
Creating Your First Project#
Step 1: Run CalmAPI Generator#
Navigate to your workspace directory and run:
cd ~/projects
calmapiStep 2: Follow the Setup Wizard#
The interactive wizard will ask you:
? Project name: my-awesome-api
? Description: My first REST API with CalmAPI
? Author: Your Name
? Database name: my_awesome_api
? Port (default 3000): 3000Pro Tip: Use kebab-case for project names (e.g., my-awesome-api)
Step 3: Navigate to Your Project#
cd my-awesome-apiStep 4: Install Dependencies#
npm installConfiguration#
Environment Variables#
CalmAPI uses a .env file for configuration. Open it and configure:
# Server Configuration
PORT=3000
NODE_ENV=development
# Database Configuration
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/my_awesome_api
# JWT Secret (Change this!)
JWT_SECRET=your-super-secret-key-change-this-in-production
# AWS S3 (Optional - for file uploads)
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
AWS_S3_BUCKET=your-bucket-name
AWS_REGION=us-east-1š Security Note: Never commit .env to version control! It's already in .gitignore.
Project Structure#
After setup, your project looks like this:
my-awesome-api/
āāā node_modules/ # Dependencies
āāā src/ # Your application code
ā āāā configs/ # App configurations
ā āāā helpers/ # Helper functions
ā āāā modules/ # API modules (this is where you work!)
ā ā āāā auth/ # Authentication module
ā ā āāā user/ # User module
ā ā āāā media/ # File upload module
ā ā āāā post/ # Sample CRUD module
ā āāā plugins/ # Custom plugins
ā āāā utils/ # Utility functions
āāā system/ # CalmAPI core (don't edit)
ā āāā configs/ # Server, database, routing
ā āāā core/ # Base classes
ā āāā helpers/ # System helpers
āāā .env # Environment variables
āāā .gitignore # Git ignore rules
āāā calmapi.json # CalmAPI metadata
āāā index.js # Application entry point
āāā package.json # Project dependencies
āāā eslintrc.json # ESLint configurationKey Directories#
src/modules/- This is where YOU create APIs. Each folder is a module.system/- CalmAPI's core code. You can read it, admire it, maybe send it a compliment. But modifying it? That's "I know what I'm doing" territory, and if you're reading this documentation, statistically speaking....env- Your configuration. Keep it secret!
Running Your Application#
Start the Server#
npm startYou should see:
⬤ CALMAPI - Keep Calm and REST
ā Project: my-awesome-api v1.0.0
ā CalmAPI Version: 0.2.5
ā Application Started
ā Connected to Database: my_awesome_api
ā Listening on port 3000
ā Endpoint Base URL: http://127.0.0.1:3000
āāāāāāāāāāā¬āāāāāāāāāāāāāāāāāā¬āāāāāāāāā¬āāāāāāāāāāāāāāā
ā Module ā Route ā Mapped ā API Exposed ā
āāāāāāāāāāā¼āāāāāāāāāāāāāāāāāā¼āāāāāāāāā¼āāāāāāāāāāāāāāā¤
ā auth ā /auth ā ā ā ā ā
ā user ā /users ā - ā ā ā
ā media ā /media ā ā ā ā ā
ā post ā /posts ā ā ā ā ā
āāāāāāāāāāā“āāāāāāāāāāāāāāāāāā“āāāāāāāāā“āāāāāāāāāāāāāāā
ā API Modules Loadedš Congratulations! Your API is running!
Testing Your API#
Method 1: Browser#
Open your browser and visit:
http://localhost:3000You should see:
{
"status": true,
"message": "Welcome to the my-awesome-api"
}Method 2: Using curl#
In your terminal:
curl http://localhost:3000/apiResponse:
{
"status": true,
"data": {
"message": "API Running"
}
}Method 3: Using Postman or Insomnia#
- Download Postman or Insomnia
- Create a new request
- Set URL to
http://localhost:3000/api/posts - Click Send
Understanding Built-in Modules#
CalmAPI comes with 4 pre-built modules:
1. Auth Module (/api/auth)#
Handles user authentication:
- Register new users
- Login/logout
- Password reset
- JWT token management
2. User Module (/api/users)#
User data model (not exposed by default)
3. Media Module (/api/media)#
File uploads to AWS S3
4. Post Module (/api/posts)#
A complete example CRUD module showing best practices
Your First API Test#
Let's test the Post module that comes with CalmAPI:
1. Register a User#
curl -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"password": "securepass123"
}'Response:
{
"status": true,
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"_id": "60d5ec49f1b2c72b8c8e4f3a",
"name": "John Doe",
"email": "john@example.com"
}
}
}Save the token! You'll need it for authenticated requests.
2. Create a Post (Authenticated)#
curl -X POST http://localhost:3000/api/posts \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-d '{
"title": "My First Post",
"content": "Hello CalmAPI!"
}'3. Get All Posts#
curl http://localhost:3000/api/postsCommon Issues & Solutions#
Issue: "Cannot connect to database"#
Solution: Make sure MongoDB is running:
# Check if MongoDB is running
ps aux | grep mongod
# Start MongoDB (if installed locally)
brew services start mongodb-community # macOS
sudo systemctl start mongod # LinuxOr use MongoDB Atlas cloud database.
Issue: "Port 3000 is already in use"#
Solution: Either:
- Stop the process using port 3000
- Change
PORTin.envto another port (e.g., 3001)
Issue: "Module not found"#
Solution: Make sure you ran npm install in your project directory.
Development Tips#
1. Hot Reload (Optional ā Use with Caution)#
You can install nodemon for automatic restarts during development:
npm install -g nodemonThen run:
nodemon index.jsā ļø Why we don't recommend nodemon for database-heavy projects:
Automatic restarts on file save can cause subtle, hard-to-debug issues, especially when working with database schemas. For example, if you modify a Mongoose model's index configuration, nodemon may restart the server mid-change and apply a partial or unintended index to your database. When you save again with the correct configuration, the damage is already done, but there's no obvious error, leaving you chasing a phantom bug.
Safer alternatives:
- Run your server manually with
npm startornode index.jsand restart only when needed- Use nodemon selectively by ignoring database model files:
nodemon --ignore 'models/'
2. ESLint#
Check your code quality:
npm run lint3. Keep Your Token Safe#
When testing, save your auth token in a variable:
# In your terminal
export TOKEN="your-jwt-token-here"
# Then use it
curl -H "Authorization: Bearer $TOKEN" http://localhost:3000/api/postsWhat's Next?#
Now that your project is running, let's understand how it works!
Next: Understanding the Architecture ā
Quick Reference#
# Install CalmAPI
npm install -g calmapi
# Create new project
calmapi
# Generate a module
calmapi generate module product
# Start server
npm start
# Run linter
npm run lint