An open API service indexing awesome lists of open source software.

https://github.com/saurabhkumardev/crypto-tracker-backend


https://github.com/saurabhkumardev/crypto-tracker-backend

Last synced: 12 months ago
JSON representation

Awesome Lists containing this project

README

          

# ๐Ÿช™ CryptoTracker Backend

This is the **backend API** for the CryptoTracker application. It fetches, stores, and serves cryptocurrency data using the **CoinGecko API**, **MongoDB**, and **Express.js**. A cron job runs every hour to update current and historical coin data automatically.

---

## ๐Ÿš€ Tech Stack

- **Node.js**
- **Express.js**
- **MongoDB + Mongoose**
- **CoinGecko API**
- **Node-Cron** (for scheduled data fetching)
- **dotenv** (for environment variable management)

---

## ๐Ÿ“ฆ Project Structure

```
crypto-tracker-backend/
โ”œโ”€โ”€ models/
โ”‚ โ””โ”€โ”€ Coin.js
โ”œโ”€โ”€ routes/
โ”‚ โ””โ”€โ”€ coins.js
โ”œโ”€โ”€ services/
โ”‚ โ”œโ”€โ”€ coinGeckoService.js
โ”‚ โ””โ”€โ”€ cronService.js
โ”œโ”€โ”€ .env
โ”œโ”€โ”€ index.js
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ package-lock.js
โ””โ”€โ”€ readme.md
```

---

## โš™๏ธ Setup & Installation

### 1. Clone the repository

```bash
git clone https://github.com/saurabhkumardev/crypto-tracker-backend.git
cd crypto-tracker-backend
```

### 2. Install dependencies

```bash
npm install
```

### 3. Configure environment variables

- Create a .env file in the root directory:

```env
MONGODB_URI=mongodb://localhost:27017/cryptotracker
PORT=5000
CLIENT_URL=http://localhost:3000
NODE_ENV=development
```

### 4. Run the server

```bash
npm run dev
```

---

- Server will run at: http://localhost:5000


---

## โฐ Cron Job โ€“ Hourly Coin Data Sync

- A cron job runs every hour to:
- Fetch top 10 coin data from the CoinGecko API.
- Update the CurrentCoin collection using upsert.
- Insert a snapshot into the HistoryCoin collection.
- Delete historical data older than 30 days.

### Cron Job Code:

- Located at: services/cronService.js

```
cron.schedule("0 * * * *", () => {
this.updateCoinData()
}, { timezone: "UTC" })
```

- Also runs immediately after server starts (with 5-second delay).

## ๐Ÿ“ก External API Used
- CoinGecko API
- Base URL: https://api.coingecko.com/api/v3
- Endpoint Used: /coins/markets?vs_currency=usd&order=market_cap_desc&per_page=10&page=1

## ๐Ÿ“ MongoDB Collections
๐Ÿ”ธ CurrentCoin
Stores latest data of top 10 coins (upserted each hour).

๐Ÿ”ธ HistoryCoin
Stores hourly snapshots of coin data for tracking trends and charts.

๐Ÿ”Œ API Endpoints
- All routes are prefixed with /api

```
Method Endpoint Description
GET /api/coins Get current coin data
GET /api/coins/history Get historical data by coinId and days
```

# ๐ŸŒ Deployment Links
๐Ÿ”— Backend API: https://your-backend.onrender.com/api

๐Ÿ”— Frontend App: https://your-frontend.vercel.app

## ๐Ÿงช Testing the API

- You can test the endpoints using:
- Postman
- Browser (for GET)

Example:
- curl http://localhost:5000/api/coins

## ๐Ÿ“ž Contact

- ๐Ÿ’ผ linkedin.com/in/saurabhkumardev
- ๐Ÿ“ง saurabhkumarmehto@gmail.com

## DB (History crypto)
![App Screenshot](./public/mongo.png)

## Cron job (Code)
![App Screenshot](./public/cron%20job.png)