Skip to main content

Python library

urlquery-api is the Python client for the v2 API. Every operation is a method, and every response is a typed pydantic model generated from the API's OpenAPI description, down to a report's HTTP transactions, scripts and artifacts. The source is at github.com/urlquery/urlquery-api-python, under the MIT license.

Install​

pip install urlquery-api

It needs Python 3.10 or later, and an API key.

from urlquery_api import Client

client = Client() # the API key from URLQUERY_APIKEY, or Client("<key>")

Options: user_agent="my-tool/1.2" names your tool in the User-Agent, http_client=httpx.Client(...) sets your own client (for a proxy, say), timeout bounds each request, and max_retry_wait bounds retries. Use the client as a context manager, or call close(), to release its connections.

Quick start​

with Client() as client:
# 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 alert in report.detection.urlquery:
print(alert.severity, alert.alert)

:::caution Choose the visibility Without access, a submission is public: every account can see the report. See Report visibility. :::

Async​

AsyncClient has the same methods as Client, for asyncio: await each call, use async with to close it (or await client.aclose()), and async for over the iter_… methods.

import asyncio

from urlquery_api import AsyncClient


async def main(report_ids):
async with AsyncClient() as client:
# Overviews of many reports at once.
overviews = await asyncio.gather(*(client.report_overview(i) for i in report_ids))

# Waiting for an analysis doesn't block other work.
s = await client.submit("https://example.com/login", access="restricted", wait=True)

# Other reports with the same page structure.
report = await client.report(s.report_id)
async for hit in client.iter_search_results(f"final.dom.dom_hash:{report.final.dom.dom_hash}"):
print(hit.report_id, hit.url.addr)

Its options, retries, waiting and errors are the same as the sync client's. Requests sent at the same time share your API key's rate limits: a request that hits one waits for its Retry-After, so a large gather slows down instead of failing. Use an asyncio.Semaphore to keep a handful of requests in flight.

Common tasks​

TaskMethods
Submit and follow an analysissubmit, submission, wait_for_submission
Read a reportreport_overview, report, screenshot, resource_content
Compare two scanscompare_reports
Find reportsrecent_reports, related_reports, search_reports, and their iter_… generators
Reputationlookup_reputation, export_reputation
Host Profilehost, list_hosts, search_hosts, relations, and their iter_… generators
YARAyara_sets, create_yara_set, create_yara_rule, update_yara_rule, validate_yara, test_yara, yara_rule_stats, iter_yara_rule_matches
Monitoringcreate_watchlist, add_watchlist_indicators, iter_watchlist_reports, watchlist_stats, create_page_monitor, page_monitor_runs, set_page_monitor_baseline

These continue from the quick start's client, with values from a report you are investigating:

sha256 = "<a script's sha256 from the report>"
cert_sha256 = "<a certificate's fingerprint.sha256>"
report_id = "<the report's ID>"
indicators = ["login.example.com", "192.0.2.10", "*/gate.php"]

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

# Hosts sharing a certificate.
page = client.list_hosts(certificate=cert_sha256, limit=50)

# A watchlist from a list of indicators; more than 500 are sent in batches.
created = client.create_watchlist("Kit X", indicators)
for s in created.skipped:
print("skipped", s.input, s.code)

# Download a script the page ran, without running it.
script = client.resource_content(report_id, sha256)

The workflows and CLI workflows show complete investigations; the calls map one to one. In Jupyter, the models print readably and convert with model_dump(), so a report or a list of results goes straight into pandas.

How it behaves​

  • Paging. List methods return one page. Their iter_… methods are generators over every item that follow cursors for you; stop when you have enough. A page can hold fewer items than its limit, or none, while more remain, and search returns at most the first 10,000 results.
  • Rate limits and retries. A rate-limit answer (429, or 503 RATE_LIMIT_UNAVAILABLE) comes before the request is processed, so any call is retried after its Retry-After. A GET is also retried after a server error or a network failure, three times. All waiting is bounded by max_retry_wait (120 seconds by default; zero turns retries off). A submission is never retried once it may have been queued.
  • Waiting. submit(..., wait=True) and wait_for_submission poll every five seconds until the analysis is done or has failed; a failed analysis is not an error, so check status. The timeout bounds the whole wait, retries included. When the wait ends first, because a check failed or the timeout passed, it raises WaitError (WaitTimeoutError for the timeout): its submission is the last state it saw, with the queue and report IDs, and its __cause__ is the error that stopped it.
  • Errors. API errors raise APIError with the API's error code in code, and is_not_found for a missing or hidden resource. All the library's errors derive from URLQueryError.
  • Models. Results are models from urlquery_api.models, with snake_case fields; IDs are plain strings and times are timezone-aware datetime values. Fields the library doesn't know yet are ignored, so new API fields don't break it.
  • Hostile content. Report content comes from the analyzed sites. Treat titles, scripts and downloads as untrusted data; resource_content returns captured content as it is, so don't open or run it outside an isolated environment.

Versions​

The package follows the v2 API and starts at 0.8.0, in step with the command-line client; 0.x releases may still change names. Releases are published from GitHub with trusted publishing and carry provenance attestations. Report bugs in the issues.