Skip to content

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

urlquery-api-python

PyPI License: MIT

The Python client for the urlquery v2 API. urlquery analyzes URLs in an isolated browser and records what they do: redirects, HTTP traffic, scripts, downloads, detections and a screenshot.

With this package you can submit URLs and wait for the reports, read and compare reports, search a year of reports, look up reputation, explore Host Profile, and manage YARA rules, watchlists and page monitors. Results are typed pydantic models.

Documentation: https://docs.urlquery.net/docs/libraries/python

Install

pip install urlquery-api

It needs Python 3.10 or later, and an API key from your urlquery.net account.

Example

from urlquery_api import Client

with Client() as client:  # the API key from URLQUERY_APIKEY
    # Analyze a URL as a restricted report and wait for it.
    s = client.submit("https://example.com/login", access="restricted", wait=True)

    # The full report: every HTTP transaction, script and artifact.
    report = client.report(s.report_id)
    for t in report.http:
        print(t.response.status_code, t.url.addr)

    # Every other report that loaded the same scripts.
    for script in report.javascript.script:
        for hit in client.iter_search_results(f"javascript.script.sha256:{script.sha256}"):
            print(script.sha256[:12], hit.report_id, hit.url.addr)

Async

AsyncClient has the same methods for asyncio. Await them, and use async with and async for:

import asyncio

from urlquery_api import AsyncClient


async def main(report_ids):
    async with AsyncClient() as client:
        overviews = await asyncio.gather(*(client.report_overview(i) for i in report_ids))
        async for hit in client.iter_search_results("tags:phishing AND url.tld:xyz"):
            print(hit.report_id, hit.url.addr)

Requests made at the same time still share your API key's rate limits; the client waits them out as the sync client does.

More in examples/: triage a URL, analyze a batch with AsyncClient, export search results to CSV, and keep a watchlist in step with a file.

How it behaves

  • Typed results. The models in urlquery_api.models are generated from the API's OpenAPI description, so every field of every response, down to the report's transactions, scripts and artifacts, is typed.
  • Paging. Each list method returns one page; its iter_… method yields every item, following cursors for you.
  • Rate limits and retries. A call waits out a rate limit for its Retry-After, and a GET is retried after a server error or a network failure; max_retry_wait bounds the waiting. Submissions are never retried once they may have been queued.
  • Waiting for an analysis. submit(..., wait=True) and wait_for_submission poll until the analysis is done or has failed, within the timeout, retries included. When the wait ends first, it raises WaitError (WaitTimeoutError on a timeout), whose submission is the last state, with the queue and report IDs, and whose __cause__ says why.
  • Errors. API errors raise APIError with the API's error code, such as NOT_FOUND or RATE_LIMIT_EXCEEDED.
  • Hostile content. Report content comes from the analyzed sites: treat titles, scripts and downloads as untrusted data.

Contributing

Report bugs and ask for features in the issues. Pull requests are welcome: this repository is published from urlquery's main repository, so an accepted change is applied there and arrives here with the next release. src/urlquery_api/models.py is generated from the API description, and the sync client, src/urlquery_api/_client.py, from the async one in _async_client.py; don't edit them by hand. For other questions, write to contact@urlquery.net.

pip install -e '.[test]'
pytest

tests/test_live.py runs against the real API when URLQUERY_E2E_APIKEY is set. Use a key of a test account: it analyzes example.com and creates, then deletes, a YARA rule, a watchlist and a page monitor.

CHANGELOG.md lists the changes in each release. Report vulnerabilities privately, as SECURITY.md describes, not in an issue.

License

MIT © urlquery.net, see LICENSE.

Releases

Packages

Contributors

Languages