This repository contains the implementation of the backend assignment for the KoinX Backend Internship. The project is designed using Node.js and MongoDB, and it follows modular structure.
-
Background Job:
- Fetches cryptocurrency data for Bitcoin, Ethereum, and Matic every 2 hours.
- Data includes the current price in USD, market cap in USD, and 24-hour change.
- Uses the CoinGecko API for fetching cryptocurrency data.
-
APIs:
- /stats: Retrieves the latest data for a specific cryptocurrency.
- /deviation: Calculates the standard deviation of the last 100 prices for a cryptocurrency.
-
Database:
- Stores cryptocurrency data in MongoDB Atlas.
-
Deployment:
- Hosted on AWS Elastic Beanstalk.
- Public URL: Crypto API Environment
- Backend: Node.js, Express.js
- Database: MongoDB
- Scheduler: node-cron
- HTTP Client: Axios
project-root/
├── src/
│ ├── jobs/ # Background jobs
│ │ └── fetchCryptoData.js # Cryptocurrency data fetching job
│ │
│ ├── controllers/ # Request handlers
│ │ ├── statsController.js # Cryptocurrency stats controller
│ │ └── deviationController.js # Price deviation controller
│ │
│ ├── models/ # Database models
│ │ └── Crypto.js # Cryptocurrency price record schema
│ │
│ ├── routes/ # API routes
│ │ ├── stats.js # Stats endpoint routes
│ │ └── deviation.js # Deviation endpoint routes
│ │
│ ├── utils/ # Utility functions
│ │ └── calculateDeviation.js # Standard deviation calculator
│ │
│ └── config/ # Configuration files
│ └── db.js # Database configuration
│
├── .env # Environment variables
├── .gitignore # Git ignore rules
├── server.js # Express Application (server)
├── package.json # Project dependencies and scripts
├── package-lock.json # Locked dependencies
├── eslint.config.mjs # Lint config to enforce coding standards
└── README.md # Project documentation
- Node.js (v22 or higher)
- MongoDB (local or hosted on MongoDB Atlas)
-
Clone the Repository:
git clone https://github.com/your-username/koinx-backend-assignment.git cd koinx-backend-assignment
-
Install Dependencies:
npm install
-
Configure Environment Variables: Create a
.env
file in the root directory and add the following:MONGO_URI=<your-mongodb-connection-string> PORT=3000
-
Start the Application: bash
npm start
- Description: Fetches the latest stats for a specific cryptocurrency.
- Query Parameters:
- coin (required): One of bitcoin, ethereum, or matic-network.
- Description: Calculates the standard deviation of the last 100 prices for a cryptocurrency.
- Query Parameters:
- coin (required): One of bitcoin, ethereum, or matic-network.
Retrieves the latest price, market cap, and 24-hour change for a specific cryptocurrency.
Endpoint: /api/stats
Method: GET
Query Parameters:
coin
: The cryptocurrency ID (bitcoin, ethereum, or matic-network)
Example Request:
curl "http://crypto-api-env.eba-2xu2tz2m.ap-south-1.elasticbeanstalk.com/api/stats?coin=bitcoin"
Sample Response:
{
"price": 42956.78,
"marketCap": 842956834521,
"24hChange": -1.23
}
Calculates the standard deviation of prices from the last 100 records for a specific cryptocurrency.
Endpoint: /api/deviation
Method: GET
Query Parameters:
coin
: The cryptocurrency ID (bitcoin, ethereum, or matic-network)
Example Request:
curl "http://crypto-api-env.eba-2xu2tz2m.ap-south-1.elasticbeanstalk.com/api/deviation?coin=bitcoin"
Sample Response:
{
"deviation": 1234.56
}
- Postman: Recommended for API testing and exploration
- cURL: Command-line tool for making HTTP requests
All API requests should be made to:
http://crypto-api-env.eba-2xu2tz2m.ap-south-1.elasticbeanstalk.com
In case of errors, the API will return appropriate HTTP status codes along with error messages:
{
"error": "Invalid cryptocurrency specified",
"status": 400
}
Common status codes:
- 200: Successful request
- 400: Bad request (e.g., invalid parameters)
- 404: Resource not found
- 500: Internal server error