Skip to main content

contextfetch

Python client for the ContextFetch API: web search, pages as Markdown, extractive answers, research, crawl and site maps for AI agents.

  • No dependencies: standard library only (urllib, json).
  • Typed: ships py.typed; responses are TypedDicts.
  • Python 3.9 or newer.

Install

pip install contextfetch

Quickstart

from contextfetch import ContextFetch

client = ContextFetch()  # reads CONTEXTFETCH_API_KEY; with no key it uses the keyless tier

res = client.search("python asyncio timeout", num_results=5, highlights=True, pruned_context=True)
for r in res["results"]:
    print(r["url"], r.get("title"))
    print(r.get("prunedContext", "")[:300])

page = client.read("https://docs.python.org/3/library/asyncio-task.html", query="task groups")
print(page.markdown)

No key yet? Get a free starter key (one per network, no signup):

key = ContextFetch().free_key()["apiKey"]
client = ContextFetch(key)

Methods

Method Endpoint
search(query, *, num_results, include_domains, exclude_domains, start_published_date, text, highlights, pruned_context) POST /v1/search
find_similar(url, *, ...same options as search) POST /v1/findSimilar
contents(urls, *, text=True, highlights, pruned_context) POST /v1/contents
answer(query, *, num_results, text) POST /v1/answer (extractive: passages quoted verbatim with [n] citations)
research(query, *, sub_queries, num_results_per_query) POST /v1/research
crawl(url, *, limit, max_depth, max_breadth, query, include_paths, exclude_paths) POST /v1/crawl
map(url, *, limit, include_paths, exclude_paths) POST /v1/map
read(url, *, query) GET /r/{url}, returns a Page (markdown, raw_tokens, output_tokens, token_savings)
balance() GET /v1/balance
free_key() POST /v1/keys/free

text, highlights and pruned_context take True or an options object as described in the API reference, for example highlights={"query": "task groups", "highlightsPerUrl": 2}. Responses are the API's JSON as dicts, with the API's field names (publishedDate, prunedContext, ...).

Options: ContextFetch(api_key=None, *, base_url="https://api.contextfetch.dev", timeout=60.0).

Errors

Every error is a ContextFetchError with status, code (the body's error field) and body.

  • AuthenticationError: HTTP 401, the key is not recognised.
  • PaymentRequiredError: HTTP 402, credits or the keyless allowance ran out. payment_url is a one-click checkout link and prompt_to_user is a sentence to show your user; stop retrying until they pay.
  • APIConnectionError: no response (DNS, connection or timeout).
from contextfetch import ContextFetch, PaymentRequiredError

try:
    ContextFetch().search("rust async runtimes")
except PaymentRequiredError as e:
    print(e.prompt_to_user)  # includes e.payment_url

Development

python -m unittest discover tests                                       # unit tests, local fake server
CONTEXTFETCH_API_KEY=... python -m unittest tests.test_live -v          # live smoke test, a few credits

License

MIT

Metadata

Release files for contextfetch 0.2.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 contextfetch 0.2.0
File Size Uploaded Provenance Actions
contextfetch-0.2.0.tar.gz 10.4 kB Without provenance Details for contextfetch-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for contextfetch 0.2.0
File Interpreter ABI Platform Actions
contextfetch-0.2.0-py3-none-any.whl Python 3 none any Details for contextfetch-0.2.0-py3-none-any.whl

Total release size: 18.9 kB

Release files / contextfetch-0.2.0.tar.gz

Download URL contextfetch-0.2.0.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d237c6f15bedf346e0b0dced13d6fd14f2bd0c7328b46d3cb3e66f9e8c3bd1df
BLAKE2b-256 checksum
How to use checksums
3867dcd68369d77a8f930566f5f50f7c8a73d6d9b29660a0ce71b1900645086d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / contextfetch-0.2.0-py3-none-any.whl

Download URL contextfetch-0.2.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06b3139af15768f4856c13a00a8af69931002463127cbb47376199836962e1eb
BLAKE2b-256 checksum
How to use checksums
0a1fd2a32aefa4dfcf504af4c5ab0a564887898e87d92b588b6e6bc6b56f2fd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for contextfetch 0.2.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

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