← Back to all products
$15
Aggregator API
REST API for swap quotes across DEX aggregators with unified response format and caching.
DockerTOMLPythonShellConfigYAMLMarkdownFastAPIReactAWS
📁 File Structure 29 files
aggregator-api/
├── LICENSE
├── README.md
├── docker/
│ ├── Dockerfile
│ └── docker-compose.yml
├── examples/
│ ├── curl-examples.sh
│ └── python-client.py
├── pyproject.toml
├── security-notes.md
└── src/
├── __init__.py
├── config.py
├── main.py
├── models/
│ ├── __init__.py
│ └── quote.py
├── providers/
│ ├── __init__.py
│ ├── base.py
│ ├── cowswap.py
│ ├── oneinch.py
│ ├── paraswap.py
│ └── zerox.py
├── routers/
│ ├── __init__.py
│ ├── health.py
│ └── quotes.py
├── services/
│ ├── __init__.py
│ ├── aggregator.py
│ ├── cache.py
│ └── fee_injector.py
└── utils/
├── __init__.py
└── multicall.py
📖 Documentation Preview README excerpt
Aggregator API
REST API template wrapping multiple DEX aggregators (1inch, 0x, Paraswap, CowSwap) with your own fee layer.
Product 5 of the [defi-router](../../) store. Part of the CryptoForge DeFi toolkit.
Price: $14.99 | License: MIT | Python: >=3.11 | Framework: FastAPI
Overview
A production-ready FastAPI backend that aggregates swap quotes from four major DEX aggregators, compares across providers, selects the best route by net output, and injects your protocol fee before returning the swap calldata to the caller.
Key Features
- Multi-provider aggregation — Queries 1inch, 0x, Paraswap, and CowSwap in parallel
- Smart ranking — Ranks quotes by
dst_amount - (gas_estimate * gas_price)for true best output - Protocol fee injection — Configurable BPS fee deducted from swap output, sent to your fee recipient
- Redis caching — 5-second TTL prevents redundant upstream calls during user quote browsing
- Multi-chain — Supports Ethereum, Optimism, Polygon, Arbitrum, and Base out of the box
- OpenAPI docs — Auto-generated Swagger UI at
/docs - Docker-ready — Production Dockerfile + docker-compose with Redis
Architecture
┌─────────────────────────────────────────────────────────────────────┐
│ CryptoForge Aggregator API │
│ │
│ ┌──────────────────────┐ ┌────────────────────────────────────┐ │
│ │ FastAPI Router │ │ AggregatorService │ │
│ │ GET /quote │───▶│ 1. Parallel fetch all providers │ │
│ │ GET /quotes/compare │ │ 2. Rank by net output │ │
│ │ POST /swap/build │ │ 3. Inject protocol fee │ │
│ └──────────────────────┘ │ 4. Cache results (Redis, 5s TTL) │ │
│ └────────────┬───────────────────────┘ │
│ │ │
│ ┌─────────────┐ ┌──────────┐ ┌───────────┐ ┌──────────┐ │
│ │ 1inch v6 │ │ 0x API │ │ Paraswap │ │ CowSwap │ │
│ │ Provider │ │ Provider │ │ Provider │ │ Provider │ │
│ └──────┬──────┘ └────┬─────┘ └─────┬─────┘ └────┬─────┘ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ FeeInjector Service │ │
│ │ fee = dst_amount * fee_rate_bps / 10000 │ │
│ │ net = dst_amount - fee │ │
│ │ fee → fee_recipient address │ │
│ └─────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
Quick Start
Prerequisites
- Python 3.11+
- Redis (for caching)
- API keys for 1inch and 0x (Paraswap key optional, CowSwap has none)
... continues with setup instructions, usage examples, and more.
📄 Code Sample .py preview
src/config.py
"""Application settings loaded from environment variables."""
from __future__ import annotations
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
"""All configuration is read from env vars (or .env file)."""
# --- Server ---
host: str = "0.0.0.0"
port: int = 8000
debug: bool = False
cors_origins: list[str] = ["*"]
# --- Provider API keys ---
oneinch_api_key: str = ""
zerox_api_key: str = ""
paraswap_api_key: str = ""
# CowSwap has no API key requirement
# --- Fee configuration ---
fee_recipient: str = "" # Ethereum address receiving protocol fees
fee_rate_bps: int = 30 # 30 bps = 0.30 %
max_fee_rate_bps: int = 100 # Hard cap: 1 %
# --- Supported chains ---
supported_chain_ids: list[int] = [1, 10, 137, 42161, 8453] # ETH, OP, POLY, ARB, BASE
# --- RPC endpoints (chain_id → URL) ---
rpc_url_1: str = "" # Ethereum mainnet
rpc_url_10: str = "" # Optimism
rpc_url_137: str = "" # Polygon
rpc_url_42161: str = "" # Arbitrum
rpc_url_8453: str = "" # Base
# --- Redis ---
redis_url: str = "redis://localhost:6379/0"
cache_ttl_seconds: int = 5
# --- HTTP ---
http_timeout_seconds: float = 10.0
model_config = {"env_file": ".env", "env_file_encoding": "utf-8"}
def rpc_url(self, chain_id: int) -> str:
"""Return the RPC URL for a given chain ID."""
mapping: dict[int, str] = {
1: self.rpc_url_1,
# ... 13 more lines ...