Backend API for creating and processing crypto pay orders for merchant organizations.
Built with FastAPI + SQLAlchemy + PostgreSQL, this service allows organizations to:
- Create sale/deposit pay orders.
- Request quote options based on wallet balances.
- Generate payment details (deposit address + routing details).
- Submit an on-chain transaction hash for payment verification and state transitions.
- Overview
- Core Concepts
- Architecture
- Tech Stack
- Project Structure
- API Endpoints
- Authentication
- Environment Variables
- Local Development
- Run with Docker
- Operational Notes
- Roadmap / Known Gaps
This service is the infrastructure API for crypto payment orchestration. It handles pay order lifecycle transitions and coordinates integrations with external quote/routing providers.
At startup, it:
- Initializes FastAPI app and middleware.
- Creates DB tables from SQLAlchemy models.
- Exposes pay order routes under
/pay-orders.
- SALE: Merchant sells goods/services and receives value in configured settlement currencies or a specified destination currency.
- DEPOSIT: User deposits one crypto asset to be routed into a specified destination currency/address.
PENDING → AWAITING_PAYMENT → AWAITING_CONFIRMATION | EXECUTING_ORDER → COMPLETED | FAILED | EXPIRED | REFUNDED
An API consumer (merchant) identified by:
api_keyapi_secretsettlement_currencies(used for SALE flow when destination currency is not explicitly provided)
- Client creates a pay order.
- Client requests quote options for a wallet.
- Client chooses source currency and requests payment details.
- Service creates an exchange route (currently via ChangeNow) and returns deposit details.
- Client submits tx hash to process payment.
- Service validates transfer details and advances pay order state.
- CoinGecko: token metadata and pricing.
- ChangeNow: routing/exchange execution.
- Alchemy/Solana/Sui RPC: chain data, balances, transaction validation.
- Additional utility modules exist for Uniswap/Jupiter/CCTP and chain-specific logic.
- Python 3.12
- FastAPI
- SQLAlchemy
- PostgreSQL
- Uvicorn
- Docker
src/
main.py # FastAPI app bootstrap + middleware + router registration
config.py # App config constants
routes/
payorder.py # Pay order HTTP endpoints
services/
payorder.py # Pay order business logic
quote.py # Quote calculation/orchestration
changenow.py # ChangeNow integration
coingecko.py # CoinGecko integration
database/
database.py # SQLAlchemy engine/session setup
dependencies.py # FastAPI auth/db dependencies
models/
database_models.py # SQLAlchemy models (Organization, PayOrder)
schemas/payorder.py # Request/response Pydantic schemas
enums.py # Domain enums
utils/ # Chain, currency, signature, and provider helpers
Base path: /pay-orders
| Method | Path | Purpose |
|---|---|---|
| POST | / |
Create a pay order |
| POST | /{payorder_id}/quote |
Get quote options for available source currencies |
| POST | /{payorder_id}/payment-details |
Generate final payment/deposit details |
| GET | /{payorder_id}/process?tx_hash=... |
Process submitted payment transaction hash |
| GET | /{payorder_id} |
Get single pay order |
| GET | / |
List all pay orders for current organization |
Swagger docs are available at:
http://localhost:8000/docshttp://localhost:8000/redoc
Two auth mechanisms are used depending on endpoint/flow:
-
API Key header
- Header:
X-API-KEY: <organization_api_key>
- Header:
-
Signature Authorization header (used for SALE creation flow)
- Header format:
APIKey=<api_key>,signature=<sha512>,timestamp=<unix_seconds> - Signature is generated from:
SHA512(APIKey + api_secret + timestamp) - Timestamp validity window: ±5 minutes.
- Header format:
Create a .env file in repository root.
POSTGRES_DB=crypto_payments
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
ALCHEMY_API_KEY=your_alchemy_key
SOLANA_RPC_URL=https://...
SUI_RPC_URL=https://...
CHANGENOW_API_KEY=your_changenow_key
COINGECKO_API_KEY=your_coingecko_pro_keyJUPITER_API_KEY=optional_if_usedNote: several modules read env vars at import/runtime. Missing required values may raise startup/runtime exceptions.
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtuvicorn src.main:app --reload --port 8000docker build -t coin-voyage-api .
docker run -p 8000:8000 --env-file .env coin-voyage-apichmod +x run.sh
./run.shrun.sh builds the image, stops existing container on port 8000, and runs a new container with .env.
- CORS is currently open (
allow_origins=["*"]). - GZip middleware is enabled (
minimum_size=1024). - DB tables are auto-created on startup via
Base.metadata.create_all(engine). - Current process endpoint updates status but does not yet include retry/background orchestration for confirmation execution.
- Add Alembic migrations for schema versioning.
- Tighten auth/error handling consistency in route/service layers.
- Add background workers/retry mechanism for asynchronous settlement states.
- Add health/readiness endpoints.
- Add automated test coverage (unit + integration).
- Harden production configuration (CORS allowlist, secret management, observability, and Gunicorn/HTTPS deployment strategy).