Metadata-Version: 2.4
Name: answerline
Version: 0.6.0
Summary: Python client for the AnswerLine answer and search monitoring API.
Author-email: AnswerLine <support@answerline.dev>
License-Expression: MIT
Project-URL: Homepage, https://answerline.dev
Keywords: answerline,ai,llm,search-api,monitoring,chatgpt,perplexity,gemini
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: httpx<1,>=0.27
Dynamic: license-file

# answerline

Python client for the [AnswerLine](https://answerline.dev) API — structured JSON answers from AI assistants
(ChatGPT, Perplexity, Gemini, Copilot, Grok) and Google surfaces (Search, News, AI Overview, AI Mode), on httpx.

All eight engines are served. [Coverage](https://answerline.dev/docs/coverage) lists the countries each one is served from.

```bash
pip install answerline
```

```python
from answerline import Client

with Client("sk_...") as client:
    # Synchronous monitor calls: one method per engine (chatgpt, perplexity, gemini,
    # copilot, grok, aimode, google, google_news), or client.monitor(engine, **body).
    answer = client.monitor.chatgpt(prompt="iPhone 17 or Galaxy S26?", country="US")

    # A US state: ChatGPT answers from inside Texas; every other engine answers it for the whole US.
    # A state implies country "US"; with another country it is refused with a 400.
    texas = client.monitor.chatgpt(prompt="Best pickup truck for towing?", state="TX")

    # Async tasks: create, poll, or run to a terminal status.
    task = client.async_tasks.run(
        "CHATGPT",
        {"prompt": "iPhone 17 or Galaxy S26?", "country": "US"},
        idempotency_key="phone-compare-1",
    )

    client.credits()  # {"remaining": USD balance, "perCycle": None, "cycleResetsAt": None}
    client.engines()  # {"windowRequests": n, "engines": [{"taskType", "name", "status", "successRate", "yourSuccessRate", "yourRequests"}]}
```

`AsyncClient` has the same methods, awaitable. `async_tasks.create_batch` submits up to 500 tasks in one call;
`async_tasks.wait` polls a task to COMPLETED or FAILED; `async_tasks.status` and `async_tasks.clear_queue` manage
the queue. Prefer a webhook (`webhook_url=` on the task) over polling for many tasks — verify deliveries with
`verify_webhook(body, header, secret)` against the `Webhook-Signature` header.

Calls raise `ApiError` (`status`, stable `code`, parsed `body`, and `meta` with the rate-limit, concurrency, USD balance
and request-id headers); transport failures raise `TimeoutError` or `ConnectionError`. 429s and safely replayable
failures are retried with jittered backoff; `timeout`, `sync_timeout`, `max_retries` and `http_client` are
constructor options.

## Measuring brand visibility

One answer is one draw from an engine that varies its answers. `visibility.measure` asks each engine the same prompt
`samples` times as separate async tasks and returns the results per engine; with a `brand` it also runs
`brand_check` over them: how many answers mention the brand (with a Wilson 95% interval), its average rank among the
brands you list, how many cite its own domains, and which domains get cited when it is absent. `samples` is any whole
number from 1 (default 10); a round of more than 500 tasks goes out in several batch calls.

```python
from answerline import brand_check

measured = client.visibility.measure(
    "Which CRM is best for a 10-person sales team?",
    ["chatgpt", "gemini"],
    "US",
    samples=12,
    rounds=4,
    spacing_seconds=2 * 60 * 60,  # 4 rounds of 3, two hours apart
    brand={"name": "Acme CRM", "aliases": ["Acme"], "domains": ["acme.com"]},
    competitors=[{"name": "Globex"}, {"name": "Initech"}],
)
brand = measured["chatgpt"]["brand_check"]["brand"]  # mentioned, runs, interval, average_rank, ...

# Or check results you already have, one engine at a time:
brand_check(results, {"name": "Acme CRM"}, [{"name": "Globex"}])
```

Samples spread over hours measure how much the engine varies its own answer to that prompt from that country, not
what different users see. Small samples give wide intervals: take 10 or more per engine. See
[Measuring visibility](https://answerline.dev/docs/measuring-visibility).
