Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” CryptoVault API β€” Personal Crypto Portfolio Tracker

A production-ready REST API for real-time crypto portfolio tracking. JWT authentication, live prices via CoinGecko, PnL analytics, and full CRUD for portfolios, holdings & transactions β€” built with FastAPI + PostgreSQL.

Python FastAPI PostgreSQL JWT CoinGecko License


πŸ” What is This?

CryptoVault API is a production-ready personal crypto portfolio tracker REST API built entirely in Python. It provides secure JWT authentication, full CRUD for portfolios and holdings, transaction history logging, real-time prices from CoinGecko, and PnL analytics β€” all via a clean, predictable REST surface.

This is the second major project of the Web3 Python Toolkit by @rizalcodes.


✨ Features

# Feature Description
1 πŸ” JWT Authentication Secure register, login, access & refresh tokens via python-jose
2 πŸ“ Portfolio Management Create and manage multiple crypto portfolios per user
3 πŸ’Ž Holdings Tracker Track crypto assets with quantity and average buy price
4 πŸ“Š Transaction History Log every buy/sell with auto-calculated total value
5 πŸ’° Live Prices Real-time prices, 24h change, market cap via CoinGecko API
6 πŸ“ˆ PnL Analytics Full portfolio analytics β€” invested vs current value, profit/loss per holding

πŸš€ Quick Start

1. Clone the repo

git clone https://github.com/rizalcodes/cryptovault-api.git
cd cryptovault-api

2. Create virtual environment

python -m venv venv
venv\Scripts\activate  # Windows
source venv/bin/activate  # Linux/Mac

3. Install dependencies

pip install -r requirements.txt

4. Configure environment

Create a .env file in the root directory:

DATABASE_URL=postgresql://username:password@localhost:5432/cryptovault
SECRET_KEY=your-super-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=7

5. Run the API

uvicorn main:app --reload

6. Open docs

http://127.0.0.1:8000/docs

πŸ“‘ API Endpoints

πŸ” Authentication

Method Endpoint Description
POST /auth/register Register new user
POST /auth/login Login + get JWT tokens
GET /auth/me Get current user profile

πŸ“ Portfolios

Method Endpoint Description
POST /portfolios/ Create new portfolio
GET /portfolios/ List all portfolios
GET /portfolios/{id} Get single portfolio
PUT /portfolios/{id} Update portfolio
DELETE /portfolios/{id} Delete portfolio

πŸ’Ž Holdings

Method Endpoint Description
POST /portfolios/{id}/holdings Add holding to portfolio
GET /portfolios/{id}/holdings List all holdings
GET /portfolios/{id}/holdings/{holding_id} Get single holding
PUT /portfolios/{id}/holdings/{holding_id} Update holding
DELETE /portfolios/{id}/holdings/{holding_id} Delete holding

πŸ“Š Transactions

Method Endpoint Description
POST /transactions/ Log new transaction
GET /transactions/ List all transactions
GET /transactions/{id} Get single transaction
GET /transactions/holding/{id} Transactions by holding
DELETE /transactions/{id} Delete transaction

πŸ’° Live Prices

Method Endpoint Description
GET /prices/{coin_id} Live price for single coin
GET /prices/?coins=bitcoin,ethereum Live prices for multiple coins

πŸ“ˆ Analytics

Method Endpoint Description
GET /analytics/{portfolio_id} Full PnL analytics for portfolio

πŸ“Š Analytics Response Example

{
    "portfolio_id": 1,
    "portfolio_name": "My Crypto Portfolio",
    "total_invested": 30000.0,
    "current_value": 32546.5,
    "total_pnl": 2546.5,
    "total_pnl_percentage": 8.49,
    "holdings": [
        {
            "coin_id": "bitcoin",
            "coin_symbol": "BTC",
            "coin_name": "Bitcoin",
            "quantity": 0.5,
            "average_buy_price": 60000.0,
            "current_price": 65093.0,
            "total_invested": 30000.0,
            "current_value": 32546.5,
            "pnl": 2546.5,
            "pnl_percentage": 8.49
        }
    ]
}

πŸ—οΈ Project Structure

cryptovault-api/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ user.py          β†’ User model
β”‚   β”‚   β”œβ”€β”€ portfolio.py     β†’ Portfolio model
β”‚   β”‚   β”œβ”€β”€ holding.py       β†’ Holding model
β”‚   β”‚   └── transaction.py   β†’ Transaction model
β”‚   β”œβ”€β”€ routers/
β”‚   β”‚   β”œβ”€β”€ auth.py          β†’ JWT auth endpoints
β”‚   β”‚   β”œβ”€β”€ portfolios.py    β†’ Portfolio CRUD
β”‚   β”‚   β”œβ”€β”€ holdings.py      β†’ Holdings CRUD
β”‚   β”‚   β”œβ”€β”€ transactions.py  β†’ Transaction history
β”‚   β”‚   β”œβ”€β”€ prices.py        β†’ CoinGecko live prices
β”‚   β”‚   └── analytics.py     β†’ PnL analytics
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   β”œβ”€β”€ user.py          β†’ User schemas
β”‚   β”‚   β”œβ”€β”€ portfolio.py     β†’ Portfolio schemas
β”‚   β”‚   β”œβ”€β”€ holding.py       β†’ Holding schemas
β”‚   β”‚   └── transaction.py   β†’ Transaction schemas
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   └── auth.py          β†’ JWT utilities
β”‚   └── database.py          β†’ DB connection & session
β”œβ”€β”€ main.py                  β†’ FastAPI app entry point
β”œβ”€β”€ requirements.txt         β†’ Dependencies
β”œβ”€β”€ CLAUDE.md                β†’ AI context file
└── .env                     β†’ Environment variables

πŸ“‘ Data Sources

Source Used For Auth
CoinGecko API Live prices, market data, 24h change Free
PostgreSQL 16 User data, portfolios, holdings, transactions Local/VPS

πŸ”§ Requirements

fastapi
uvicorn
sqlalchemy
psycopg2-binary
python-jose[cryptography]
bcrypt
pydantic[email]
python-dotenv
requests
alembic

βš™οΈ Environment Variables

Variable Description
DATABASE_URL PostgreSQL connection string
SECRET_KEY JWT signing secret key
ALGORITHM JWT algorithm (HS256)
ACCESS_TOKEN_EXPIRE_MINUTES Access token expiry
REFRESH_TOKEN_EXPIRE_DAYS Refresh token expiry

πŸ—οΈ Tech Stack

Technology Version Purpose
FastAPI Latest REST API framework
PostgreSQL 16 Primary database
SQLAlchemy Latest ORM
python-jose Latest JWT tokens
bcrypt Latest Password hashing
Pydantic V2 Data validation
CoinGecko API Free Live crypto prices
Alembic Latest DB migrations

⚠️ Disclaimer

This API is for personal portfolio tracking and educational purposes only. Nothing here constitutes financial advice. Always do your own research before making any investment decisions.


πŸ‘€ Author

RIZAL β€” @rizalcodes

Building Web3 tools with Python πŸβ›“οΈ Follow on X: @rizalcodes_


πŸ“„ License

MIT License β€” free to use, modify, and distribute.

About

πŸš€ CryptoVault API β€” FastAPI + PostgreSQL REST API for crypto portfolio tracking. Features JWT auth, holdings management, transaction history & real-time price integration via CoinGecko.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages