Skip to main content

Elfa AI Python SDK

PyPI version Python 3.9+ License: MIT

Official Python SDK for the Elfa API v2 — social intelligence, AI chat, and the Auto condition engine for crypto. Sync and async clients, fully typed with Pydantic.

Features

  • Social intelligence — trending tokens, mentions, narratives, smart stats, event summaries
  • AI chat — market analysis and conversational chat via client.chat, streamed via client.chat_stream
  • Auto condition engine — build EQL queries that watch markets and notify via client.auto
  • Sync and async — ElfaClient and AsyncElfaClient, same surface
  • Typed — Pydantic v2 models, full type hints
  • Robust — retries with backoff, typed errors

The SDK returns processed metadata and tweet links only — never raw tweet content. For raw tweets, call the X (Twitter) API directly using the returned links/ids.

Installation

pip install elfa-sdk

Quick start

Synchronous

from elfa import ElfaClient

client = ElfaClient(api_key="your-api-key")

trending = client.get_trending_tokens(time_window="24h")
for token in trending.data.data:
    print(token.token, token.current_count, f"{token.change_percent:+.1f}%")

mentions = client.get_keyword_mentions(keywords="bitcoin,ethereum", time_window="1h")
for mention in mentions.data:
    print(mention.link, mention.like_count)

answer = client.chat("What's the sentiment on Bitcoin today?")
print(answer.data.message)

Asynchronous

import asyncio
from elfa import AsyncElfaClient

async def main():
    async with AsyncElfaClient(api_key="your-api-key") as client:
        stats = await client.get_account_smart_stats("elonmusk")
        print(stats.data.smart_following_count)

asyncio.run(main())

Configuration

client = ElfaClient(
    api_key="your-api-key",
    base_url="https://api.elfa.ai",  # default (production)
    timeout=30.0,                    # per-request timeout, seconds
    retries=3,                       # retries for idempotent (GET) requests
    retry_delay=1.0,                 # base delay for exponential backoff
    headers=None,                    # extra headers sent on every request
    app_name=None,                   # your product, appended to the User-Agent, e.g. "my-bot/1.2"
)

# Quick reachability/auth check
assert client.test_connection() is True

The Auto engine is also constructable standalone if that is all you need:

from elfa import AutoClient

auto = AutoClient(api_key="your-api-key")
auto.close()

The API key is sent as the x-elfa-api-key header on every request. Read it from the environment in your app:

import os
from elfa import ElfaClient

client = ElfaClient(api_key=os.environ["ELFA_API_KEY"])

Core data & chat

All methods exist on both ElfaClient (sync) and AsyncElfaClient (async).

Method Endpoint
ping() /v2/ping
get_api_key_status() /v2/key-status
get_trending_tokens(...) /v2/aggregations/trending-tokens
get_account_smart_stats(username) /v2/account/smart-stats
get_keyword_mentions(...) /v2/data/keyword-mentions
get_token_news(...) /v2/data/token-news
get_trending_cas_twitter(...) /v2/aggregations/trending-cas/twitter
get_trending_cas_telegram(...) /v2/aggregations/trending-cas/telegram
get_top_mentions(ticker, ...) /v2/data/top-mentions
get_event_summary(keywords, ...) /v2/data/event-summary
get_trending_narratives(...) /v2/data/trending-narratives
chat(message, ...) /v2/chat
chat_stream(message, ...) /v2/chat/stream

Time-ranged endpoints accept either time_window="24h" or both from_time and to_time (unix seconds).

get_account_smart_stats is legacy — it still works, but will be removed on 28 October 2026. It returns raw metrics, not a reputation score, and is not being extended before then; use get_keyword_mentions instead, which returns the posting account alongside each mention's engagement metrics.

get_token_news returns X posts from accounts tagged as news sources, not articles published by news outlets.

Streaming chat (SSE)

chat_stream takes the same arguments as chat and yields one event per data: frame, ending on the terminating [DONE] frame. Requires a PAYG or Enterprise API key.

for event in client.chat_stream("What is the sentiment on SOL?"):
    if event.type == "text":
        print(event.content, end="")
    elif event.type == "complete":
        print("\ncredits:", event.creditsConsumed)

# async
async for event in async_client.chat_stream("What is the sentiment on SOL?"):
    print(event.type)

Event types are session_info, title, text, text_complete, status, credits, complete, invalid_request and error. Payload fields vary by type and are preserved as model extras.

Auto condition engine (client.auto)

Build EQL queries that watch conditions and fire actions (notify, webhook, telegram_bot, llm). Every route authenticates with the API key alone.

query = {
    "query": {
        "conditions": {
            "AND": [{
                "source": "price", "method": "current",
                "args": {"symbol": "BTC", "exchange": "hyperliquid"},
                "operator": ">", "value": 250000,
            }]
        },
        "actions": [{"stepId": "notify", "type": "notify", "params": {"message": "BTC > 250k"}}],
        "expiresIn": "24h",
    },
    "title": "btc breakout alert",
}

client.auto.validate_query(query)
created = client.auto.create_query(query)
query_id = created.id or created.query_id

status = client.auto.get_query(query_id)
client.auto.cancel_query(query_id)
client.auto.delete_query(query_id)

Also available: chat, list_queries, drafts (list_drafts/get_draft/upsert_draft/delete_draft/validate_draft/convert_draft), list_sessions/get_session, list_executions/get_execution, and validate_symbol.

Builder Chat is dynamically priced, so it reports what the turn cost:

reply = client.auto.chat("Alert me when BTC breaks 100k")
print(reply.credits)  # e.g. 104 — same total as the x-elfa-credits header

Response shapes are extensible

Every response model sets extra="allow", so fields the API adds are kept as model extras rather than raising. Do the same in your own code — pinning an Elfa response with an exact-shape assertion (Pydantic extra="forbid", z.strictObject, additionalProperties: false) means the next additive field breaks your client even though the API stayed backwards compatible.

Streaming notifications (SSE)

for event in client.auto.stream_query(query_id):
    print(event.event, event.data)

# async
async for event in async_client.auto.stream_all():
    print(event.event, event.data)

Error handling

from elfa import (
    ElfaAPIError,
    ElfaAuthenticationError,
    ElfaRateLimitError,
    ElfaValidationError,
    ElfaNetworkError,
)

try:
    client.get_trending_tokens(time_window="24h")
except ElfaAuthenticationError:
    ...  # bad/missing API key
except ElfaRateLimitError as e:
    print("retry after", e.retry_after, "reset", e.reset_time)
except ElfaValidationError as e:
    print("invalid params", e.validation_errors)
except ElfaNetworkError:
    ...  # connection problem
except ElfaAPIError as e:
    print("api error", e.status_code, e)

Idempotent (GET) requests are retried with exponential backoff on network errors, rate limits, and 5xx responses. Mutations are not retried automatically.

Development

git clone https://github.com/elfa-ai/elfa-sdk-python.git
cd elfa-sdk-python
pip install -e ".[dev]"

make check   # flake8 + mypy + pytest
make format  # black + isort

Live integration tests run only when ELFA_API_KEY is set (optionally ELFA_BASE_URL); otherwise they skip.

Support

License

MIT — see LICENSE.

Metadata

Release files for elfa-sdk 6.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for elfa-sdk 6.0.0
File Size Uploaded
elfa_sdk-6.0.0.tar.gz 35.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for elfa-sdk 6.0.0
File Interpreter ABI Platform
elfa_sdk-6.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 63.5 kB

Release files / elfa_sdk-6.0.0.tar.gz

Download URL elfa_sdk-6.0.0.tar.gz
Size 35.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e88a7d17181412a3c9501fe7ddb414fa809b75bc1242b10181d5c449bc5b9229
BLAKE2b-256 checksum
How to use checksums
4d8f6daaa5153251e6534e307f251d35aa3e360765b47c9df0fc3646786a1371
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / elfa_sdk-6.0.0-py3-none-any.whl

Download URL elfa_sdk-6.0.0-py3-none-any.whl
Size 27.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ea493266d80d1dee004015d1c82871d85f40c3901fc80563686f723d328e3c4
BLAKE2b-256 checksum
How to use checksums
8151b4e787c0988464249379fe11b5115c7262f6a6c92496a28bb46dcd97b63b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release history Release notifications | RSS feed

6.3.0

2 release files

6.2.0

2 release files

6.1.0

2 release files

This release

6.0.0 This release

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.0.0

2 release files

3.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page