Quickstart
Get started with the DFlow Python SDK in minutes
Quickstart
This guide will help you get up and running with the DFlow Python SDK in just a few minutes.
Initialize the Client
from dflow import DFlowClient
# Development (default) - no API key required
# Uses dev-*.dflow.net endpoints for testing with real capital
client = DFlowClient()
# Production - API key required
# Uses *.dflow.net endpoints for production deployments
prod_client = DFlowClient(environment="production", api_key="your-api-key")Fetch Markets
# Get all active markets sorted by volume
response = client.markets.get_markets(status="active", sort="volume")
for market in response.markets[:5]:
print(f"{market.ticker}: YES={market.yes_ask}")Get Market Details
# Get a specific market by ticker
market = client.markets.get_market("BTCD-25DEC0313-T92749.99")
print(f"Market: {market.ticker}")
print(f"Status: {market.status}")
print(f"YES Ask: {market.yes_ask}")Fetch Orderbook
# Get orderbook for a market
orderbook = client.orderbook.get_orderbook("BTCD-25DEC0313-T92749.99")
print("YES Side:")
print(f" Bids: {len(orderbook.yes_bid)} levels")
print(f" Asks: {len(orderbook.yes_ask)} levels")Browse Events
# Get active events
response = client.events.get_events(status="active")
for event in response.events[:5]:
print(f"{event.ticker}: {event.title}")Real-time WebSocket Updates
import asyncio
from dflow import DFlowClient
async def main():
client = DFlowClient()
# Connect to WebSocket
await client.ws.connect()
# Subscribe to price updates
client.ws.subscribe_prices(["BTCD-25DEC0313-T92749.99"])
# Handle updates
def on_price(update):
print(f"{update.ticker}: YES={update.yes_price} NO={update.no_price}")
client.ws.on_price(on_price)
# Run for a while
await asyncio.sleep(30)
# Cleanup
client.ws.disconnect()
asyncio.run(main())Get a Swap Quote
from dflow import USDC_MINT
# First get a market
market = client.markets.get_market("BTCD-25DEC0313-T92749.99")
# Get a quote for buying YES tokens
quote = client.swap.get_quote(
input_mint=USDC_MINT,
output_mint=market.accounts["usdc"].yes_mint,
amount=1000000, # 1 USDC
slippage_bps=50,
)
print(f"Input: {quote.in_amount}")
print(f"Output: {quote.out_amount}")
print(f"Price Impact: {quote.price_impact_pct}%")Execute a Swap (Requires Wallet)
from solders.keypair import Keypair
from solana.rpc.api import Client
from dflow import USDC_MINT, sign_send_and_confirm
# Load your wallet
keypair = Keypair.from_base58_string("your-private-key")
connection = Client("https://api.mainnet-beta.solana.com")
# Get swap transaction
swap = client.swap.create_swap(
input_mint=USDC_MINT,
output_mint=market.accounts["usdc"].yes_mint,
amount=1000000,
slippage_bps=50,
user_public_key=str(keypair.pubkey()),
)
# Sign and send
result = sign_send_and_confirm(connection, swap.swap_transaction, keypair)
print(f"Transaction: {result.signature}")Error Handling
from dflow import DFlowClient, DFlowApiError
client = DFlowClient()
try:
market = client.markets.get_market("invalid-id")
except DFlowApiError as e:
print(f"API Error: {e.message}")
print(f"Status Code: {e.status_code}")Using Pagination
from dflow import collect_all, paginate
# Collect all markets (auto-paginate)
all_markets = collect_all(
lambda params: client.markets.get_markets(**params),
get_items=lambda r: r.markets,
)
# Or iterate page by page using async generator
for market in paginate(
lambda params: client.markets.get_markets(**params),
get_items=lambda r: r.markets,
max_items=100,
):
print(market.ticker)Using Retry Utilities
from dflow import with_retry, DFlowApiError
# Basic usage with default options
markets = with_retry(lambda: client.markets.get_markets())
# With custom options
events = with_retry(
lambda: client.events.get_events(limit=100),
max_retries=5,
initial_delay_ms=500,
)Next Steps
- Explore the API Reference for detailed documentation
- Learn about WebSocket connections for real-time data
- Check out Solana helpers for transaction utilities