Powerbank Rental API (pbrsAPI)
A RESTful API for a powerbank sharing ecosystem. This service allows users to register, authenticate, view available powerbanks at specific stations, start a rental session, and securely return powerbanks.
Backend: Node.js, Express.js
Database: PostgreSQL (using pg & pg-pool)
Authentication: JSON Web Tokens (JWT)
git clone https://github.com/Kristian-Banar/pbrServiceAPI.git
2. Configure Environment Variables (.env)
PORT=3000
JWT_SECRET=your_super_secure_jwt_secret_key
DB_USER=user
DB_HOST=0.0.0.0
DB_NAME=pbrs_db
DB_PASSWORD=your_postgres_password
DB_PORT=5432
docker-compose up -d --build
The server will be live and running at http://localhost:3000
API Documentation (Endpoints)
Authentication & Authorization
Method
Endpoint
Description
Access
POST
/api/register
Register a new user
Public
POST
/api/login
Authenticate user & receive JWT
Public
GET
/api/profile
Retrieve the logged-in user's profile
Private (JWT Required)
Method
Endpoint
Description
Access
GET
/api/stations/:station_id/powerbanks
Get all powerbanks at a specific station
Public
Method
Endpoint
Description
Access
POST
/rents/:id/powerbank
Start renting a powerbank
Private (JWT Required)
POST
/rents/:id/return
Return a powerbank (terminate rental session)
Private (JWT Required)
Sample Request & Response Payloads
1. User Login (POST /api/login)
URL Parameters: None
Body (JSON):
{
"username": "user",
"password": "password"
}
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ey..."
}
2. View Powerbanks (GET /api/stations/2/powerbanks)
URL Parameters: station_id (e.g., 2)
Response (200 OK):
[
{
"id": 105,
"station_id": 2,
"status": "available",
"charge_level": 89
},
{
"id": 108,
"station_id": 2,
"status": "available",
"charge_level": 94
}
]
3. Handle Errors (e.g., Missing Station ID)
Response (400 Bad Request):
{
"error": "Station ID not specified"
}
The relational PostgreSQL database consists of the following core entities:
id
username
password
powerbank_id
age
id
cost
station_id
status
id
number_of_powerbanks
location