Getting Started
This guide will help you set up and run the Sistem Monev API locally for development, and understand how to interact with the API endpoints.
Prerequisites
Before you begin, ensure you have the following installed on your system:
- Node.js (version 22 or higher)
- npm (comes with Node.js)
- PostgreSQL (version 16 or higher, you can also use Supabase)
- Git (for cloning the repository)
Installation
1. Clone the Repository
git clone https://github.com/Danne56/sistem-monev-api.git
cd sistem-monev-api2. Install Dependencies
npm install3. Environment Configuration
Create a .env file in the API root directory based on the provided example:
cp .env.example .envConfigure the following environment variables in your .env file:
# Database Configuration
DB_USER=your_database_user
DB_HOST=your_database_host
DB_NAME=your_database_name
DB_PASSWORD=your_database_password
DB_PORT=your_database_port
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key
JWT_EXPIRES_IN=24h
# Email Configuration (for sending verification emails)
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=your-app-password
# Server Configuration
PORT=5000
NODE_ENV=development
# Frontend URL (for CORS)
FRONTEND_URL=http://localhost:51734. Database Setup
- Create a PostgreSQL database or you can use Supabase
- Change database configuration in .env file
5. Google Cloud Storage Setup
To enable file uploads and storage, you need to configure Google Cloud Storage. This involves creating a service account and providing its credentials to the application.
Go to Google Cloud Console: Navigate to the Google Cloud Console.
Select or Create a Project:
- If you have an existing project, select it from the project dropdown at the top of the page.
- If not, click Create Project, give it a name, and follow the setup instructions.
Enable the Cloud Storage API:
- In the navigation menu (
☰), go to APIs & Services > Library. - Search for "Cloud Storage" and select it.
- Click Enable to activate the API for your project.
- In the navigation menu (
Create a Service Account:
- In the navigation menu, go to IAM & Admin > Service Accounts.
- Click + CREATE SERVICE ACCOUNT.
- Service account details:
- Name: Give your service account a descriptive name (e.g.,
sistem-monev-storage). - Service account ID: An ID will be automatically generated.
- Description: Add a brief description of its purpose.
- Name: Give your service account a descriptive name (e.g.,
- Click CREATE AND CONTINUE.
Grant Permissions:
- In the Grant this service account access to project step, you need to add a role.
- Click Select a role and search for Storage Admin.
- Select the
Storage Adminrole. This grants the service account full control over objects in Google Cloud Storage. - Click CONTINUE.
Create a JSON Key:
- In the final step, Grant users access to this service account, you can skip adding users.
- Click DONE. You will be returned to the service accounts list.
- Find the service account you just created and click on the three dots (
⋮) under Actions, then select Manage keys. - Click ADD KEY > Create new key.
- Choose JSON as the key type and click CREATE.
- A JSON file will be automatically downloaded to your computer. This is your service account key.
Place the Key File:
- Rename the downloaded JSON file to
service-account.json. - Move this file to the root directory of the API (
/api).
- Rename the downloaded JSON file to
SECURITY WARNING
The service-account.json file contains sensitive credentials. DO NOT commit this file to version control (e.g., Git). Ensure that service-account.json is listed in your .gitignore file to prevent accidental exposure.
Running the Application
Development Mode
Start the server in development mode with auto-reload:
npm run devThe server will start at http://localhost:5000
Production Mode
Start the server in production mode:
npm startQuick Start Examples
1. Register a New User
curl -X POST http://localhost:5000/authentication/register \
-H "Content-Type: application/json" \
-d '{
"username": "johndoe",
"fullName": "John Doe",
"email": "john@example.com",
"password": "securepassword123",
"confirmPassword": "securepassword123",
"role": "pengelola"
}'2. Login
curl -X POST http://localhost:5000/authentication/login \
-H "Content-Type: application/json" \
-d '{
"username": "johndoe",
"password": "securepassword123"
}'3. Access Protected Endpoint
curl -X GET http://localhost:5000/api/users/profile \
-H "Authorization: Bearer your-jwt-token-here"Authentication Flow
- Register: Create a new user account
- Admin Verification: Admin must verify new accounts
- Login: Get JWT token for authenticated requests
- Access: Use token in Authorization header for protected endpoints
API Response Format
All API responses follow a consistent format:
Success Response
{
"status": "success",
"message": "Operation completed successfully",
"data": {
// Response data here
}
}Error Response
{
"status": "error",
"message": "Error description",
"errors": [
// Detailed error information
]
}User Roles
The system supports three main user roles:
admin: Full system access, user managementdinas: Tourism department officials, monitoring accesspengelola: Village managers, limited to their village data
Next Steps
- Explore the API Endpoints documentation
- Check out the Database Schemas reference
- Review the example requests and responses
- Set up your client application to consume the API