Simpler server for accessing some Swarm features.
β οΈ ALPHA SOFTWARE - PROOF OF CONCEPT This software is in Alpha stage and should be considered a Proof of Concept. Use for testing and experimentation only. Not recommended for production use.
β οΈ DATA PERSISTENCE WARNING Storage on Swarm is rented storage with limited time periods. The default configuration uses very short rental periods (approximately 1 day). Do not expect uploaded data to persist longer than the rental period. Data will become unavailable when the postage stamp expires.
swarm_connect/
βββ app/ # Main application package
β βββ __init__.py
β βββ main.py # FastAPI app instantiation and router inclusion
β βββ api/ # API specific modules
β β βββ __init__.py
β β βββ endpoints/ # API route definitions
β β β βββ __init__.py
β β β βββ stamps.py # Endpoints for Swarm stamp management
β β β βββ data.py # Endpoints for data upload/download
β β β βββ wallet.py # Endpoints for wallet information
β β βββ models/ # Pydantic models for request/response validation
β β βββ __init__.py
β β βββ stamp.py # Pydantic models for stamp data
β β βββ data.py # Pydantic models for data operations
β β βββ wallet.py # Pydantic models for wallet information
β βββ core/ # Core application logic/configuration
β β βββ __init__.py
β β βββ config.py # Configuration management (e.g., loading .env)
β βββ services/ # Logic for interacting with external services
β βββ __init__.py
β βββ swarm_api.py # Functions to call the EthSwarm Bee API
β
βββ tests/ # Unit and integration tests (Recommended)
β βββ ...
β
βββ .env # Environment variables (API keys, URLs - NOT committed to Git)
βββ .env.example # Example environment file (Committed to Git)
βββ .gitignore # Files/directories to ignore in Git
βββ requirements.txt # Python package dependencies
βββ README.md # Project description, setup, and usage instructions
βββ run.py # Script to easily run the development server
# Create virtual environment
python3 -m venv venv
# Activate virtual environment (Linux/Mac)
source venv/bin/activate
# On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Set up environment file
cp .env.example .env
# Edit .env and ensure SWARM_BEE_API_URL points to your Bee node's API endpoint
# (e.g., http://localhost:1633 or the public gateway https://api.gateway.ethswarm.org)# Start the development server (with auto-reload)
python run.py
# Optional: Use different port if 8000 is taken
PORT=8001 python run.py
# For HTTPS development (requires SSL certificates)
SSL_KEYFILE=./localhost+2-key.pem SSL_CERTFILE=./localhost+2.pem python run.pyThe server will be available at:
- HTTP: http://127.0.0.1:8000
- API Documentation: http://127.0.0.1:8000/docs
- Alternative docs: http://127.0.0.1:8000/redoc
Swarm Connect is a FastAPI-based API gateway that provides comprehensive access to Ethereum Swarm (distributed storage network) functionality. It offers complete postage stamp management and data operations through a clean, RESTful interface, eliminating the need for clients to interact directly with complex Swarm Bee node APIs.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CLIENT APPLICATIONS β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ βββββββββββββββββββββββ β
β β Web Apps β β Mobile App β β CLI Tools β β Third-party Apps β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ βββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
HTTP/HTTPS Requests
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SWARM CONNECT API GATEWAY β
β (FastAPI) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β API LAYER β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β β β Health Check β β OpenAPI Docs β β Stamps & Data APIs β β β
β β β GET / β β /docs /redoc β β Complete CRUD β β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β VALIDATION LAYER β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β β β Request/Responseβ β StampDetails β β Error Handling & β β β
β β β Validation β β Pydantic Modelβ β HTTP Status Codes β β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β BUSINESS LOGIC LAYER β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β β β Stamp Filteringβ β TTL Calculationβ β Expiration Time β β β
β β β by Batch ID β β & Processing β β Formatting β β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β SERVICE LAYER β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β β β Swarm API β β HTTP Client β β Error Recovery & β β β
β β β Integration β β (Requests) β β Retry Logic β β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β CONFIGURATION LAYER β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β β β Environment β β Settings β β URL Validation & β β β
β β β Variables β β Management β β Caching β β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
HTTP Requests (10s timeout)
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SWARM BEE NODE β
β (localhost:1633) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β BEE API ENDPOINTS β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β β β GET /batches β β Stamp Data β β Blockchain β β β
β β β (All Stamps) β β Repository β β Integration β β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Purchase Stamps: Create new postage stamps with specified amount and depth
- Extend Stamps: Add funds to existing stamps to extend their validity
- List All Stamps: Retrieve comprehensive list of all available stamps with enhanced data
- Get Stamp Details: Fetch specific stamp information by batch ID
- Expiration Calculation: Automatically calculates stamp expiration time (current time + TTL)
- Data Merging: Merges global network data with local node information for complete stamp details
- Local Ownership Detection: Identifies stamps owned/managed by the connected node
- Enhanced Field Mapping: Handles different field names between global and local APIs
- Unified Data Upload: Single endpoint handles both JSON and binary data automatically
- SWIP-Compliant Examples: Pre-filled with SWIP standard provenance data structure
- Content-Type Detection: Automatic handling based on Content-Type header
- Raw Data Download: Download data as binary stream or base64-encoded JSON
- Reference-Based Access: Access data using Swarm reference hashes
- Provenance Support: Built-in examples for data lineage and provenance tracking
- FastAPI Framework: Modern, fast web framework with automatic OpenAPI documentation
- Auto-Documentation: Interactive API docs at
/docsand/redoc - Type Validation: Pydantic models ensure data integrity and type safety
- Error Handling: Comprehensive error responses with appropriate HTTP status codes
- Configuration Management: Environment-based settings with validation
- Development Server: Hot-reload development server with SSL support
- Binary Data Support: Direct binary upload/download with optional JSON wrapping
- Modular Design: Separate endpoints for stamps and data operations
- Request Timeouts: 10-second timeout for external API calls
- Error Recovery: Multiple layers of exception handling
- Flexible Response Parsing: Handles different Swarm API response formats
- Logging: Structured logging for debugging and monitoring
- Health Checks: Basic health check endpoint for monitoring
- Loads environment variables on startup
- Validates Swarm Bee API URL format
- Provides cached settings to all components
- Receives HTTP requests and routes them
- Applies path parameters and validation
- Returns structured JSON responses
- Makes HTTP calls to Swarm Bee node (both
/batchesand/stampsendpoints) - Handles network errors and timeouts
- Parses and normalizes API responses
- Data Merging Logic: Combines global stamp data with local node information
- Field Mapping: Handles different field names between endpoints (
immutablevsimmutableFlag) - Usability Calculation: Determines stamp usability based on TTL, depth, and immutability
- Local Detection: Identifies stamps owned by the connected node
- Validates response data structure with enhanced fields
- Handles optional fields and type conversion
- Local Ownership Field: Boolean indicator for node-owned stamps
- Enhanced Nullable Fields: Proper handling of potentially missing data from different endpoints
- Formats output for API consumers
1. Client β FastAPI Router β Endpoint Handler
2. Endpoint β Service Layer β External Swarm API
3. Service β Business Logic β Data Processing
4. Response β Pydantic Model β Formatted Data
GET /: Health check endpoint
POST /api/v1/stamps/: Purchase new postage stamps with time-based or advanced parametersGET /api/v1/stamps/: List all available stamps with expiration calculationsGET /api/v1/stamps/{stamp_id}: Retrieve specific stamp batch detailsPATCH /api/v1/stamps/{stamp_id}/extend: Extend existing stamps with additional funds
POST /api/v1/data/?stamp_id={id}&content_type={type}: Upload raw data to SwarmGET /api/v1/data/{reference}: Download raw data from Swarm (returns bytes directly)GET /api/v1/data/{reference}/json: Download data with JSON metadata (base64-encoded)
GET /api/v1/wallet: Get the wallet address and BZZ balance of the Bee nodeGET /api/v1/chequebook: Get the chequebook address and balance information of the Bee node
- Complete Gateway Solution: Full stamp lifecycle and data operations in one service
- Simplified Interface: Clean REST API vs complex Swarm protocols
- Enhanced Data: Adds calculated expiration times to raw stamp data
- Reliability: Robust error handling and timeout management
- Developer Experience: Auto-generated docs and type safety
- Flexibility: Configurable for different Swarm node endpoints
- Binary Support: Native handling of raw data with multiple access patterns
Purchase a new postage stamp.
- Request Body:
{"amount": 2000000000, "depth": 17, "label": "my-stamp"} - Response:
{"batchID": "...", "message": "Postage stamp purchased successfully"}
List all available postage stamps.
- Response:
{"stamps": [...], "total_count": N}
Get detailed information about a specific stamp.
- Response: Detailed stamp information with calculated expiration time
Extend an existing stamp by adding more funds.
- Request Body:
{"amount": 2000000000} - Response:
{"batchID": "...", "message": "Postage stamp extended successfully"}
Upload data to Swarm (JSON or binary).
- Request Body: JSON data (default) or raw binary data
- Content-Type:
application/json(default) orapplication/octet-streamfor binary - Response:
{"reference": "...", "message": "Data uploaded successfully"} - Features: Pre-filled with SWIP-compliant provenance data example structure
Download raw data from Swarm as a file (triggers browser download).
- Use case: End users downloading files, browser integration
- Response: Raw binary data with user-friendly filename
- Headers:
Content-Disposition: attachment; filename="provenance-abc12345.json"Content-Type: Auto-detected (application/json, image/png, etc.)
- Filenames:
- JSON data β
provenance-{hash}.json - Images β
image-{hash}.png/jpg - PDFs β
document-{hash}.pdf - Text β
text-{hash}.txt - Binary β
data-{hash}.bin
- JSON data β
Download data as JSON with metadata (for API clients).
- Use case: Web apps, mobile apps, API integrations needing metadata
- Response:
{"data": "base64-encoded-content", "content_type": "application/json", "size": 2048, "reference": "abc..."} - Benefits: Get file metadata without triggering download, programmatic access
Get the wallet address and BZZ balance of the connected Bee node.
- Response:
{"walletAddress": "0x...", "bzzBalance": "254399000000000"} - Use case: Identify the Ethereum wallet address and check BZZ token balance
- BZZ Balance: Returned in wei (smallest unit of BZZ token)
- Note: Only available when connected to local Bee nodes, not public gateways
Get the chequebook address and balance information of the connected Bee node.
- Response:
{"chequebookAddress": "0x...", "availableBalance": "1000000000", "totalBalance": "1000000000"} - Use case: Identify the chequebook smart contract address and check available funds
- Balance Fields:
availableBalance: Funds available for creating new postage stamps (in wei)totalBalance: Total funds in the chequebook (in wei)
- Note: Only available when connected to local Bee nodes, not public gateways
This project is licensed under the MIT License - see the LICENSE file for details.
- β Commercial use - Use in commercial applications
- β Distribution - Distribute copies or substantial portions
- β Modification - Modify and create derivative works
- β Private use - Use privately without restrictions
β οΈ Include license - Include MIT license and copyright notice- β No liability - No warranty or liability from authors
The MIT License encourages adoption while maintaining attribution, making it ideal for:
- Research and academic projects
- Commercial integrations
- Open-source ecosystem growth
- Ethereum/Web3 community standards