Skip to main content

Python SDK for DefiLlama API

Project description

DefiLlama Python SDK

Official Python SDK for the DefiLlama API. Access DeFi protocol data including TVL, prices, yields, volumes, fees, bridges, and more.

Installation

pip install defillama-sdk
uv pip install defillama-sdk

Quick Start

from defillama_sdk import DefiLlama

# Free tier
client = DefiLlama()

# Pro tier (required for premium endpoints)
pro_client = DefiLlama({"api_key": "your-api-key"})

# Get all protocols
protocols = client.tvl.getProtocols()

# Get current token prices
prices = client.prices.getCurrentPrices(
    [
        "ethereum:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
        "coingecko:bitcoin",
    ]
)

Configuration

config = {
    "api_key": "your-api-key",
    "timeout": 30000,
}

client = DefiLlama(config)

Modules

  • TVL
  • Prices
  • Stablecoins
  • Yields (Pro)
  • Volumes
  • Fees
  • Emissions (Pro)
  • Bridges (Pro)
  • Ecosystem (Pro)
  • ETFs (Pro)
  • DAT (Pro)
  • Account (Pro)

๐Ÿ” = Requires Pro API key


TVL

Total Value Locked data for protocols and chains.

getProtocols

Get all protocols with current TVL.

protocols = client.tvl.getProtocols()

getProtocol

Get detailed protocol information including historical TVL.

aave = client.tvl.getProtocol("aave")

getTvl

Get only current TVL for a protocol.

tvl = client.tvl.getTvl("uniswap")

getChains

Get current TVL for all chains.

chains = client.tvl.getChains()

getHistoricalChainTvl

Get historical TVL data.

all_history = client.tvl.getHistoricalChainTvl()
eth_history = client.tvl.getHistoricalChainTvl("Ethereum")

getTokenProtocols ๐Ÿ”

Get protocols holding a specific token.

holders = pro_client.tvl.getTokenProtocols("ETH")

getInflows ๐Ÿ”

Get token inflows/outflows between timestamps.

inflows = pro_client.tvl.getInflows(
    "lido",
    1704067200,
    1704153600,
    "ETH,USDC",
)

getChainAssets ๐Ÿ”

Get asset breakdown for all chains.

assets = pro_client.tvl.getChainAssets()

Prices

Token price data and historical charts.

getCurrentPrices

prices = client.prices.getCurrentPrices(
    [
        "ethereum:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
        "coingecko:bitcoin",
        "solana:So11111111111111111111111111111111111111112",
    ]
)

getHistoricalPrices

prices = client.prices.getHistoricalPrices(
    1704067200,
    ["ethereum:0xdac17f958d2ee523a2206206994597c13d831ec7"],
)

getBatchHistoricalPrices

prices = client.prices.getBatchHistoricalPrices(
    {
        "ethereum:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48": [
            1704067200,
            1704153600,
            1704240000,
        ]
    }
)

getChart

chart = client.prices.getChart(
    ["coingecko:ethereum"],
    {"start": 1704067200, "period": "1d", "span": 30},
)

getPercentageChange

change = client.prices.getPercentageChange(
    ["coingecko:bitcoin"],
    {"period": "24h"},
)

getFirstPrices

first = client.prices.getFirstPrices(
    ["ethereum:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"]
)

getBlockAtTimestamp

block = client.prices.getBlockAtTimestamp("ethereum", 1704067200)

Stablecoins

Stablecoin market cap and dominance data.

getStablecoins

stables = client.stablecoins.getStablecoins(True)

getAllCharts

charts = client.stablecoins.getAllCharts()

getChartsByChain

eth_charts = client.stablecoins.getChartsByChain("Ethereum")

getStablecoin

usdt = client.stablecoins.getStablecoin("1")

getChains

chains = client.stablecoins.getChains()

getPrices

prices = client.stablecoins.getPrices()

getDominance ๐Ÿ”

dominance = pro_client.stablecoins.getDominance("ethereum", 1)

Yields ๐Ÿ”

Yield farming, lending, staking, and perpetual funding rates.

getPools

pools = pro_client.yields.getPools()

getPoolsOld

pools = pro_client.yields.getPoolsOld()

getPoolChart

chart = pro_client.yields.getPoolChart("pool-uuid-here")

getBorrowPools

borrow_pools = pro_client.yields.getBorrowPools()

getLendBorrowChart

chart = pro_client.yields.getLendBorrowChart("pool-uuid-here")

getPerps

perps = pro_client.yields.getPerps()

getLsdRates

lsd_rates = pro_client.yields.getLsdRates()

Volumes

DEX, options, and derivatives trading volume data.

getDexOverview

overview = client.volumes.getDexOverview()
overview = client.volumes.getDexOverview(
    {"excludeTotalDataChart": True, "dataType": "dailyVolume"}
)

getDexOverviewByChain

eth_volume = client.volumes.getDexOverviewByChain("Ethereum")

getDexSummary

uniswap = client.volumes.getDexSummary("uniswap")

getOptionsOverview

options = client.volumes.getOptionsOverview()

getOptionsOverviewByChain

eth_options = client.volumes.getOptionsOverviewByChain("Ethereum")

getOptionsSummary

derive = client.volumes.getOptionsSummary("derive")

getDerivativesOverview ๐Ÿ”

derivatives = pro_client.volumes.getDerivativesOverview()

getDerivativesSummary ๐Ÿ”

gmx = pro_client.volumes.getDerivativesSummary("gmx")

getDexMetrics ๐Ÿ”

Get summary of DEX metrics with protocol list (no charts).

metrics = pro_client.volumes.getDexMetrics()

# With data type
metrics = pro_client.volumes.getDexMetrics({"dataType": "dailyVolume"})

getDexMetricsByProtocol ๐Ÿ”

Get DEX metrics for a specific protocol.

uniswap = pro_client.volumes.getDexMetricsByProtocol("uniswap")

getDerivativesMetrics ๐Ÿ”

Get summary of derivatives metrics with protocol list (no charts).

metrics = pro_client.volumes.getDerivativesMetrics()

getDerivativesMetricsByProtocol ๐Ÿ”

Get derivatives metrics for a specific protocol.

hyperliquid = pro_client.volumes.getDerivativesMetricsByProtocol("hyperliquid")

getOptionsMetrics ๐Ÿ”

Get summary of options metrics with protocol list (no charts).

metrics = pro_client.volumes.getOptionsMetrics()

getOptionsMetricsByProtocol ๐Ÿ”

Get options metrics for a specific protocol.

hegic = pro_client.volumes.getOptionsMetricsByProtocol("hegic")

Fees

Protocol fees and revenue data.

Fee Data Types

from defillama_sdk import FeeDataType

FeeDataType.DAILY_FEES
FeeDataType.DAILY_REVENUE
FeeDataType.DAILY_HOLDERS_REVENUE
FeeDataType.DAILY_SUPPLY_SIDE_REVENUE
FeeDataType.DAILY_BRIBES_REVENUE
FeeDataType.DAILY_TOKEN_TAXES
FeeDataType.DAILY_APP_FEES
FeeDataType.DAILY_APP_REVENUE

getOverview

fees = client.fees.getOverview()
revenue = client.fees.getOverview({"dataType": FeeDataType.DAILY_REVENUE})

getOverviewByChain

eth_fees = client.fees.getOverviewByChain("Ethereum")

getSummary

uniswap_fees = client.fees.getSummary("uniswap")

getChart ๐Ÿ”

chart = pro_client.fees.getChart()
eth_chart = pro_client.fees.getChartByChain("Ethereum")
protocol_chart = pro_client.fees.getChartByProtocol("aave")

getChartByProtocolChainBreakdown ๐Ÿ”

breakdown = pro_client.fees.getChartByProtocolChainBreakdown("aave")

getChartByProtocolVersionBreakdown ๐Ÿ”

breakdown = pro_client.fees.getChartByProtocolVersionBreakdown("uniswap")

getChartByChainProtocolBreakdown ๐Ÿ”

breakdown = pro_client.fees.getChartByChainProtocolBreakdown("Ethereum")

getChartChainBreakdown ๐Ÿ”

breakdown = pro_client.fees.getChartChainBreakdown()

getMetrics ๐Ÿ”

metrics = pro_client.fees.getMetrics()
chain_metrics = pro_client.fees.getMetricsByChain("Ethereum")
protocol_metrics = pro_client.fees.getMetricsByProtocol("aave")

Emissions ๐Ÿ”

Token unlock schedules and vesting data.

getAll

emissions = pro_client.emissions.getAll()

getByProtocol

arbitrum = pro_client.emissions.getByProtocol("arbitrum")

Bridges ๐Ÿ”

Cross-chain bridge volume and transaction data.

getAll

bridges = pro_client.bridges.getAll()
bridges = pro_client.bridges.getAll({"includeChains": True})

getById

bridge = pro_client.bridges.getById(1)

getVolumeByChain

volume = pro_client.bridges.getVolumeByChain("Ethereum")

getDayStats

stats = pro_client.bridges.getDayStats(1704067200, "Ethereum")

getTransactions

txs = pro_client.bridges.getTransactions(
    1,
    {
        "limit": 100,
        "startTimestamp": 1704067200,
        "endTimestamp": 1704153600,
        "sourceChain": "Ethereum",
        "address": "0x...",
    },
)

Ecosystem ๐Ÿ”

Ecosystem-level data.

getCategories

categories = pro_client.ecosystem.getCategories()

getForks

forks = pro_client.ecosystem.getForks()

getOracles

oracles = pro_client.ecosystem.getOracles()

getEntities

entities = pro_client.ecosystem.getEntities()

getTreasuries

treasuries = pro_client.ecosystem.getTreasuries()

getHacks

hacks = pro_client.ecosystem.getHacks()

getRaises

raises = pro_client.ecosystem.getRaises()

ETFs ๐Ÿ”

Bitcoin and Ethereum ETF data.

getOverview

btc_etfs = pro_client.etfs.getOverview()

getOverviewEth

eth_etfs = pro_client.etfs.getOverviewEth()

getHistory

history = pro_client.etfs.getHistory()

getHistoryEth

history_eth = pro_client.etfs.getHistoryEth()

getFdvPerformance

perf = pro_client.etfs.getFdvPerformance("30")

DAT ๐Ÿ”

Digital Asset Treasury data and institutional holdings.

getInstitutions

data = pro_client.dat.getInstitutions()

getInstitution

mstr = pro_client.dat.getInstitution("MSTR")

Account ๐Ÿ”

API usage management.

getUsage

usage = pro_client.account.getUsage()

Error Handling

from defillama_sdk import ApiKeyRequiredError, RateLimitError, NotFoundError, ApiError

try:
    data = pro_client.yields.getPools()
except ApiKeyRequiredError:
    print("Pro API key required for this endpoint")
except RateLimitError as exc:
    print(f"Rate limited. Retry after {exc.retry_after} seconds")
except NotFoundError:
    print("Resource not found")
except ApiError as exc:
    print(f"API error: {exc.status_code}")

Type Exports

All types are available from defillama_sdk.types, and are re-exported at the top level:

from defillama_sdk import Protocol, CoinPricesResponse, Stablecoin, YieldPool

Constants

from defillama_sdk import AdapterType, FeeDataType, VolumeDataType

AdapterType.DEXS
VolumeDataType.DAILY_VOLUME
FeeDataType.DAILY_FEES

Requirements

  • Python >= 3.9

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

defillama_sdk-0.1.4.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

defillama_sdk-0.1.4-py3-none-any.whl (30.1 kB view details)

Uploaded Python 3

File details

Details for the file defillama_sdk-0.1.4.tar.gz.

File metadata

  • Download URL: defillama_sdk-0.1.4.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for defillama_sdk-0.1.4.tar.gz
Algorithm Hash digest
SHA256 0c723da4ec5e7250404e17e9f522296404121b0230d4c55d72f563c34ffa51e4
MD5 cddb87fafab83204648bdc8da4dcd363
BLAKE2b-256 4b8877450b82d1e1ddee45ac1e9553f2a6f15d56c6bebb38a5767557f5d421a3

See more details on using hashes here.

File details

Details for the file defillama_sdk-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: defillama_sdk-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 30.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for defillama_sdk-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1d919f2f3c23bb0a3cc3671df22238d3658f5876bf46bb72ae9fb19b997f8ce6
MD5 0f5efaa3ba32590b4f200054de75b2b1
BLAKE2b-256 4f8e6b4e2522f237080745aaed6ae34f1cd5a538c60cc5e538d2ef3208fd00a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page