derive.session

Documentation for eth_defi.derive.session Python module.

HTTP session management for Derive API.

This module provides session creation with retry logic and rate limiting for Derive API requests.

Rate limiting is thread-safe using SQLite backend, so the session can be shared across multiple threads when using joblib.Parallel or similar.

Functions

create_derive_session([retries, ...])

Create a requests Session configured for Derive API.

create_derive_session(retries=5, backoff_factor=0.5, requests_per_second=2.0, pool_maxsize=32, rate_limit_db_path=PosixPath('/home/runner/.tradingstrategy/derive/rate-limit.sqlite'))

Create a requests Session configured for Derive API.

The session is configured with:

  • Rate limiting to respect Derive API throttling (thread-safe via SQLite)

  • Retry logic for handling transient errors using exponential backoff

The rate limiter uses SQLite backend for thread-safe coordination across multiple threads (e.g., when using joblib.Parallel with threading backend).

Example:

from eth_defi.derive.session import create_derive_session

session = create_derive_session()
response = session.post("https://api.lyra.finance/private/get_account", json={...})
Parameters
  • retries (int) – Maximum number of retry attempts for failed requests

  • backoff_factor (float) – Backoff factor for exponential retry delays

  • requests_per_second (float) – Maximum requests per second to avoid rate limiting. Defaults to 2.0 (conservative estimate).

  • pool_maxsize (int) – Maximum number of connections to keep in the connection pool. Should be at least as large as max_workers when using parallel requests. Defaults to 32.

  • rate_limit_db_path (pathlib.Path) – Path to SQLite database for storing rate limit state. Using SQLite ensures thread-safe rate limiting across multiple threads. Defaults to ~/.tradingstrategy/derive/rate-limit.sqlite.

Returns

Configured requests Session with rate limiting and retry logic

Return type

requests.sessions.Session