Vast.ai Python SDK

0.6.0 · active · verified Fri Apr 10

The official Python SDK for Vast.ai, providing programmatic access to GPU cloud resources. Users install the `vastai-sdk` package, which then exposes the `VastAI` client class through the `vastai` module. This SDK allows for searching, launching, and managing GPU instances, mirroring much of the functionality of the Vast.ai CLI. The current version is 0.6.0, with active development.

Warnings

Install

Imports

Quickstart

Initialize the VastAI client using an API key from an environment variable or direct parameter, then search for available GPU offers.

import os
from vastai import VastAI

# Ensure VAST_API_KEY is set in your environment
# or pass it directly: vast = VastAI(api_key="YOUR_API_KEY")
api_key = os.environ.get('VAST_API_KEY', '')
if not api_key:
    print("Error: VAST_API_KEY environment variable not set.")
    print("Please visit https://cloud.vast.ai/cli/ to get your API key.")
else:
    vast = VastAI(api_key=api_key)
    try:
        offers = vast.search_offers(query='num_gpus >= 1 gpu_name = RTX_3090', limit=1)
        if offers:
            print(f"Found an offer: {offers.get('gpu_name')} at {offers.get('dph_total')}$/hr")
        else:
            print("No matching offers found.")
    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →