This API allows users to create, manage, and track habits. Users can log daily completions for habits and retrieve or delete habits as needed.
-
Clone the repository:
git clone https://github.com/your-username/habit-tracker-api.git cd habit-tracker-api
-
Install dependencies:
npm install
-
Start the server:
npm start
The server will start on
http://localhost:3000
.
You can use tools like Postman
or curl
to interact with the API.
curl -X POST http://localhost:3000/habits \
-H "Content-Type: application/json" \
-d '{
"name": "Exercise",
"description": "Daily morning workout",
"target_days_per_week": 5
}'
curl -X POST http://localhost:3000/habits/:id/log \
-H "Content-Type: application/json" \
-d '{
"date": "2024-08-23"
}'
-
URL:
/habits
-
Method:
POST
-
Body Parameters:
name
(string): Name of the habit.description
(string): Description of the habit.target_days_per_week
(number): Target days per week for the habit.
-
Success Response:
- Code: 200 OK
- Content:
{ "id": "123", "name": "Exercise", "description": "Daily morning workout", "target_days_per_week": 5, "completed_days": [] }
-
Error Response:
- Code: 400 Bad Request
- Content:
"400 Bad Request: Invalid input data"
-
URL:
/habits/:id/log
-
Method:
POST
-
URL Parameters:
id
(string): The habit's unique identifier.
-
Body Parameters:
date
(string): The date of completion inYYYY-MM-DD
format.
-
Success Response:
- Code: 201 Created
- Content:
{ "id": "123", "date": "2024-08-23", "message": "Completion logged successfully." }
-
Error Responses:
-
Code: 400 Bad Request
-
Content:
"400 Bad Request: Invalid date format."
-
Code: 404 Not Found
-
Content:
"404 Not Found: Habit not found."
-
Code: 409 Conflict
-
Content:
"409 Conflict: Completion already logged for this date."
-
-
URL:
/habits
-
Method:
GET
-
Success Response:
- Code: 200 OK
- Content:
{ "habits": [ { "id": "123", "name": "Exercise", "description": "Daily morning workout", "target_days_per_week": 5, "completed_days": [ { "date": "2024-08-23" } ] } ], "total": 1 }
-
URL:
/habits/:id
-
Method:
DELETE
-
URL Parameters:
id
(string): The habit's unique identifier.
-
Success Response:
- Code: 200 OK
- Content:
"Habit deleted successfully."
-
Error Response:
- Code: 404 Not Found
- Content:
"Habit not found."