VirusTotal Python Client Library

0.22.0 · active · verified Wed Apr 15

vt-py is the official Python client library for the VirusTotal API v3. This library enables interaction with the VirusTotal REST API v3 to automate security workflows, including scanning files and URLs, retrieving comprehensive information about various entities (files, URLs, domains), managing VirusTotal Intelligence searches, LiveHunt rulesets, and launching Retrohunt jobs. It is actively maintained with regular releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fetch information about a file using its SHA256 hash. It initializes an asynchronous `vt.Client` with an API key from an environment variable, retrieves a `vt.Object` representing the file, and prints some of its attributes. The client is automatically closed using `async with`.

import os
import asyncio
import vt

async def get_file_info(api_key: str, file_hash: str):
    """Fetches information about a file hash from VirusTotal."""
    async with vt.Client(api_key) as client:
        try:
            file_obj = await client.get_object(f"/files/{file_hash}")
            print(f"File SHA256: {file_obj.sha256}")
            print(f"File Size: {file_obj.size} bytes")
            print(f"Detection Stats: {file_obj.last_analysis_stats}")
        except vt.APIError as e:
            print(f"VirusTotal API error: {e}")
        except Exception as e:
            print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    VIRUSTOTAL_API_KEY = os.environ.get('VIRUSTOTAL_API_KEY', '')
    if not VIRUSTOTAL_API_KEY:
        print("Please set the VIRUSTOTAL_API_KEY environment variable.")
    else:
        # Example SHA256 hash of an EICAR test file
        example_hash = "275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f"
        asyncio.run(get_file_info(VIRUSTOTAL_API_KEY, example_hash))

view raw JSON →