Skip to content

About

REST API for Tron wallets: create wallets, check balances, look up transactions, and send TRX. Built with FastAPI.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

Β 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Tron Wallet API (FastAPI)

A high-performance REST API built with FastAPI for managing Tron wallets and transactions.

Features

  • ⚑ FastAPI - High performance async framework
  • πŸ” Secure API key authentication
  • πŸ’Ό Create new Tron wallets
  • πŸ’° Check wallet balances
  • πŸ” Get transaction details
  • πŸ’Έ Send TRX transactions
  • πŸ“ Auto-generated interactive API documentation
  • 🐳 Docker support
  • βœ… Input validation with Pydantic

Prerequisites

  • Python 3.8 or 3.9 β€” tronapi pins eth-account==0.4.0, which fails to import on Python 3.10 and newer (from collections import Mapping was removed in 3.10)
  • pip

Installation

  1. Clone the repository:
git clone https://github.com/RayanErfan/Tron-Wallet-API.git
cd Tron-Wallet-API
  1. Install dependencies:
pip install -r requirements.txt
  1. Set up environment variables:
cp .env.example .env
# Edit .env with your configuration

Configuration

Create a .env file with the following variables:

API_KEY=your_secure_api_key_here
TRON_FULL_NODE=https://api.trongrid.io
TRON_SOLIDITY_NODE=https://api.trongrid.io
TRON_EVENT_SERVER=https://api.trongrid.io
CHANNEL=YourChannel
OWNER=YourName
PORT=8000
RELOAD=False

Usage

Running the Server

# Development mode with auto-reload
uvicorn app:app --reload --host 0.0.0.0 --port 8000

# Production mode
uvicorn app:app --host 0.0.0.0 --port 8000 --workers 4

Or simply:

python app.py

The server will start on http://0.0.0.0:8000 by default.

Interactive API Documentation

FastAPI automatically generates interactive API documentation:

You can test all endpoints directly from the browser!

API Endpoints

Authentication

All endpoints require API key authentication via the X-API-Key header:

X-API-Key: your_api_key

1. Create Wallet

Creates a new Tron wallet.

Endpoint: POST /wallet/create

cURL Example:

curl -X POST "http://localhost:8000/wallet/create" \
  -H "X-API-Key: your_api_key"

Response:

{
  "ok": true,
  "channel": "YourChannel",
  "owner": "YourName",
  "data": {
    "private_key": "...",
    "public_key": "...",
    "address_base58": "TRx...",
    "address_hex": "41...",
    "is_valid": true
  }
}

2. Check Balance

Get the balance of a Tron wallet.

Endpoint: GET /wallet/balance

Query Parameters:

  • address (string, required): The Tron wallet address

cURL Example:

curl -X GET "http://localhost:8000/wallet/balance?address=TRx..." \
  -H "X-API-Key: your_api_key"

Python Example:

import requests

response = requests.get(
    "http://localhost:8000/wallet/balance",
    params={"address": "TRx..."},
    headers={"X-API-Key": "your_api_key"}
)
print(response.json())

Response:

{
  "ok": true,
  "channel": "YourChannel",
  "owner": "YourName",
  "data": {
    "address": "TRx...",
    "balance_sun": 1000000,
    "balance_trx": 1.0
  }
}

3. Get Transaction

Retrieve transaction details by ID.

Endpoint: GET /transaction/get

Query Parameters:

  • tx_id (string, required): The transaction ID

cURL Example:

curl -X GET "http://localhost:8000/transaction/get?tx_id=..." \
  -H "X-API-Key: your_api_key"

4. Send Transaction

Send TRX from one address to another.

Endpoint: POST /transaction/send

Request Body:

{
  "private_key": "...",
  "from_address": "TRx...",
  "to_address": "TRx...",
  "amount": 1000000
}

cURL Example:

curl -X POST "http://localhost:8000/transaction/send" \
  -H "X-API-Key: your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "private_key": "...",
    "from_address": "TRx...",
    "to_address": "TRx...",
    "amount": 1000000
  }'

Python Example:

import requests

response = requests.post(
    "http://localhost:8000/transaction/send",
    headers={"X-API-Key": "your_api_key"},
    json={
        "private_key": "...",
        "from_address": "TRx...",
        "to_address": "TRx...",
        "amount": 1000000  # in SUN
    }
)
print(response.json())

Docker Deployment

Build and run with Docker:

docker build -t Tron-Wallet-API .
docker run -p 8000:8000 --env-file .env Tron-Wallet-API

FastAPI Advantages

βœ… Automatic validation - Pydantic models ensure data integrity
βœ… Auto-generated docs - Interactive API documentation out of the box
βœ… Type hints - Better IDE support and fewer bugs
βœ… Async support - High performance for I/O operations
βœ… Modern Python - Uses Python 3.6+ features
βœ… Production ready - Built on Starlette and Uvicorn

Security Considerations

⚠️ IMPORTANT SECURITY NOTES:

  1. Never commit your .env file - Contains sensitive API keys
  2. Use strong API keys - Generate random, complex keys (32+ characters)
  3. Use HTTPS in production - Never send private keys over HTTP
  4. Private keys are sensitive - Never log or expose them
  5. Rate limiting - Consider adding rate limiting for production
  6. CORS - Configure CORS properly for your use case

Generating a Secure API Key

import secrets
api_key = secrets.token_urlsafe(32)
print(api_key)

Development

Run in development mode with auto-reload:

export RELOAD=True
python app.py

Or:

uvicorn app:app --reload

Testing

Access the interactive API documentation at /docs to test all endpoints visually.

Project Structure

Tron-Wallet-API/
β”œβ”€β”€ app.py              # Main FastAPI application
β”œβ”€β”€ requirements.txt    # Python dependencies
β”œβ”€β”€ .env.example       # Environment variables template
β”œβ”€β”€ .env               # Your configuration (not in git)
β”œβ”€β”€ .gitignore         # Git ignore rules
β”œβ”€β”€ Dockerfile         # Docker container config
└── README.md          # This file

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT License - see LICENSE file for details

Disclaimer

This software is provided as-is. Use at your own risk. Always test thoroughly with testnet before using mainnet. Never expose private keys or API keys.

Support

About

REST API for Tron wallets: create wallets, check balances, look up transactions, and send TRX. Built with FastAPI.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages