A simple, secure, and terminal-friendly API for tracking moods and journaling. Built with Node.js, Express, and Better-SQLite3, this API allows users to log their daily moods and journal entries, retrieve statistics, and manage their data with ease.
- User Registration: Create a unique username and API key for authentication.
- Mood Tracking: Log moods with optional notes and feelings; retrieve entries by user or date.
- Journaling: Write journal entries with tags and automatic sentiment analysis; view stats like average sentiment and top tags.
- Public Read Access: Anyone can view mood and journal entries for a username (GET requests).
- Private Write Access: Only the owner (via API key) can create or delete entries.
- Pagination: Retrieve large datasets efficiently with
pageandlimitquery parameters. - Rate Limiting: Protects against abuse with 100 requests per 15 minutes per IP.
- Input Validation: Ensures data integrity with robust validation.
- API Documentation: Available via
curlor web browser at/api/docs.
- Clone the repository:
git clone https://github.com/notyourdoraemon/mental-wellness-api.git cd mental-wellness-api - Install dependencies and configure .env:
npm install mv .env.example .env
- Start the server:
The API will be available at
npm start
http://localhost:3000.
curl -X POST -H "Content-Type: application/json" -d '{"username":"aditya"}' http://localhost:3000/api/registerResponse:
{
"username": "aditya",
"api_key": "550e8400-e29b-41d4-a716-446655440000"
}Save the api_key for authenticated requests.
curl -X POST -H "X-API-Key: <your-api-key>" -H "Content-Type: application/json" -d '{"mood":"happy","feeling":"excited","notes":"Great day!"}' http://localhost:3000/api/moodscurl "http://localhost:3000/api/moods/aditya?page=1&limit=5"curl -X POST -H "X-API-Key: <your-api-key>" -H "Content-Type: application/json" -d '{"title":"Day Reflection","content":"I had a great day","tags":"friends, positivity"}' http://localhost:3000/api/journalcurl http://localhost:3000/api/journal/stats/aditya- Via
curl:Response: JSON version of the OpenAPI spec (seecurl http://localhost:3000/api/docs
docs/openapi.yamlfor structure). Example snippet:{ "openapi": "3.0.0", "info": { "title": "Mental Wellness API", "version": "1.0.0", "description": "A secure, terminal-friendly API for tracking daily moods and journal entries..." }, "paths": { "/api/register": { "post": { "summary": "Register a new user", "description": "Creates a new user with a unique username and generates an API key..." } } } } - Via Web Browser: Visit
http://localhost:3000/api/docsto see a formatted HTML page with the API spec.
The API documentation is available via curl http://localhost:3000/api/docs (JSON) or in a browser at http://localhost:3000/api/docs (HTML). The raw spec is also in docs/openapi.yaml. Below is a summary table and detailed reference with examples.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/register |
Register a new user | No |
| POST | /api/moods |
Add a mood entry | Yes |
| GET | /api/moods/:username |
Get all mood entries | No |
| GET | /api/moods/date/:username/:date |
Get mood entries for a date | No |
| GET | /api/stats/:username |
Get mood statistics | No |
| DELETE | /api/moods/:id |
Delete a mood entry | Yes |
| POST | /api/journal |
Add a journal entry | Yes |
| GET | /api/journal/:username |
Get all journal entries | No |
| GET | /api/journal/stats/:username |
Get journal statistics | No |
| DELETE | /api/journal/:id |
Delete a journal entry | Yes |
| GET | /api/docs |
Get API documentation | No |
- Header:
X-API-Key: <your-api-key> - Required for all POST and DELETE requests.
- Description: Register a new user and get an API key.
- Request Body:
{ "username": "aditya" } - Response (201):
{ "username": "aditya", "api_key": "550e8400-e29b-41d4-a716-446655440000" }
- Description: Add a mood entry (requires authentication).
- Request Body:
{ "mood": "happy", // Required "feeling": "excited", // Optional "notes": "Great day!" // Optional } - Response (201):
{ "id": 1, "mood": "happy", "feeling": "excited", "notes": "Great day!" }
- Description: Get all mood entries for a user (public).
- Path Parameters:
username: The user's username (e.g., "aditya").
- Query Parameters:
page: Page number (default: 1).limit: Entries per page (default: 10, max: 100).
- Example:
curl "http://localhost:3000/api/moods/aditya?page=1&limit=5" - Response (200):
{ "entries": [ { "id": 1, "mood": "happy", "feeling": "excited", "notes": "Great day!", "created_at": "2025-03-02T10:00:00Z" } ], "pagination": { "page": 1, "limit": 5, "total": 25, "pages": 5 } }
- Description: Get mood entries for a specific date (public).
- Path Parameters:
username: The user's username (e.g., "aditya").date: Date in YYYY-MM-DD format (e.g., "2025-03-02").
- Example:
curl http://localhost:3000/api/moods/date/aditya/2025-03-02
- Response (200):
[ { "id": 1, "mood": "happy", "feeling": "excited", "notes": "Great day!", "created_at": "2025-03-02T10:00:00Z" } ]
- Description: Get mood statistics for a user (public).
- Path Parameters:
username: The user's username (e.g., "aditya").
- Example:
curl http://localhost:3000/api/stats/aditya
- Response (200):
{ "topMoods": [ { "mood": "happy", "count": 5 }, { "mood": "calm", "count": 3 } ], "topFeelings": [ { "feeling": "excited", "count": 3 }, { "feeling": "relaxed", "count": 2 } ] }
- Description: Delete a mood entry (requires authentication).
- Path Parameters:
id: The mood entry ID (e.g., 1).
- Example:
curl -X DELETE -H "X-API-Key: <your-api-key>" http://localhost:3000/api/moods/1 - Response (200):
{ "message": "Mood entry deleted" }
- Description: Add a journal entry (requires authentication).
- Request Body:
{ "title": "Day Reflection", // Required "content": "I had a great day", // Required "tags": "friends, positivity" // Optional } - Response (201):
{ "id": 1, "title": "Day Reflection", "content": "I had a great day", "tags": "friends, positivity", "sentiment_score": 0.2 }
- Description: Get all journal entries for a user (public).
- Path Parameters:
username: The user's username (e.g., "aditya").
- Query Parameters:
page: Page number (default: 1).limit: Entries per page (default: 10, max: 100).
- Example:
curl "http://localhost:3000/api/journal/aditya?page=1&limit=5" - Response (200):
{ "entries": [ { "id": 1, "title": "Day Reflection", "content": "I had a great day", "tags": "friends, positivity", "sentiment_score": 0.2, "created_at": "2025-03-02T10:00:00Z" } ], "pagination": { "page": 1, "limit": 5, "total": 15, "pages": 3 } }
- Description: Get journal statistics for a user (public).
- Path Parameters:
username: The user's username (e.g., "aditya").
- Example:
curl http://localhost:3000/api/journal/stats/aditya
- Response (200):
{ "average_sentiment": 0.15, "top_tags": [ { "tags": "friends, positivity", "count": 3 }, { "tags": "work", "count": 2 } ] }
- Description: Delete a journal entry (requires authentication).
- Path Parameters:
id: The journal entry ID (e.g., 1).
- Example:
curl -X DELETE -H "X-API-Key: <your-api-key>" http://localhost:3000/api/journal/1 - Response (200):
{ "message": "Journal entry deleted" }
To host this API on a platform like Heroku, Render, or Vercel:
- Update
server.js:- Ensure
PORTusesprocess.env.PORT(already done).
- Ensure
- Add a Procfile (for Heroku):
web: node server.js - Deploy:
- Push to GitHub.
- Connect to your hosting platform and deploy.
- Example for Heroku:
heroku create git push heroku main
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -m "Add your feature"). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.