Skip to main content

A low-level API for Vast.ai

This project is not intended to replace the open-source vast.ai library but rather to complement it for those who want to have an easy-to-use API to build their own projects. The goal of this project is not to re-implement every functionality in the original library but to simplify it for future developers and end users.

It also integrates pandas dataframes for easier data manipulation.

Installation/Setup instructions

vast-ai-api is available on PyPi:

$ pip install vast-ai-api

or

$ poetry add vast-ai-api

Export your Vast-AI API key:

$ export VAST_AI_API_KEY=<YOUR_API_KEY_HERE>

After you reserve an instance, in order to change its state or interact with it in any way, you will need to setup an SSH key on Vast.ai. Then, add your private key to your ssh agent (see here).

Contributing

Currently, the API covers most of the functionality provided by the original Vast AI library, except for host actions (hosting machines, listing hosted machines, etc.). You can contribute by forking this repo, adding your changes and making a pull request.

Usage

Initializing the API Helper:

import pandas as pd
from vast_ai_api import VastAPIHelper

api = VastAPIHelper()

List all instances available to be rented

instances: pd.DataFrame = api.list_available_instances()

Pick an instance from the list and reserve it using its instance_id

instance: pd.Series = instances.iloc[50]
instance_id = instance["id"]
machine_id = instance["machine_id"]  # Needed after reserving
api.launch_instance(instance_id)

Instance is now launched and starting up with default parameters

launched_instances = api.list_current_instances()

Note that the instance_id that we got before reserving the instance changes after reservation. Instead, we have to use the machine_id to find the instance again and get its new id

newly_launched_instance = launched_instances[launched_instances["machine_id"] == machine_id]
new_instance_id = newly_launched_instance["id"]

Now we can perform actions on this launched instance:

api.stop_instance(new_instance_id)
api.start_instance(new_instance_id)
api.reboot_instance(new_instance_id)  # Equivalent to stopping and starting the instance

api.get_instance_logs(new_instance_id)

Connecting through SSH

Prerequisites: You must have initialized the instance as api.launch_instance(instance_id, use_jupyter_lab=False)

You can connect to the instance in 2 ways: with or without a proxy server (provided by Vast.ai). Using a proxy server is recommended as it allows you to stay anonymous when connecting to the gpu provider, but will slightly increase the latency to the machine.

ssh_client = api.connect_ssh(new_instance_id, use_vast_proxy=True)
stdin, stdout, stderr = ssh_client.exec_command("<your_command_here>")
print(stdout.readlines())

Alternatively, you can connect directly via the command line by reading the necessary host and port of the machine:

ssh_host = newly_launched_instance["ssh_host"]
ssh_port = newly_launched_instance["ssh_port"]

and then use ssh and replace ssh_host and ssh_port with the values above to connect your terminal to the instance:

$ ssh -p ${ssh_port} root@${ssh_host} -L 8080:localhost:8080

Transferring files via sftp

Prerequisites: You must have initialized the instance as api.launch_instance(instance_id, use_jupyter_lab=False)

"""
    src and dst format:
    
    localhost:22:<local_path> for the local machine
    <remote_host>:<remote_port>:<remote_path> for the remote machine

"""
api.copy("localhost:22:./polkadots.jpg", "ssh.vastai5.com:/home/workdir/polkadots.jpg", ssh_client)
api.copy("remote:/home/workdir/polkadots.jpg", "localhost:~/images/polka_dots.jpg", connect_ssh(new_instance_id))
api.copy("localhost:22:./polkadots.jpg", "ssh.vastai5.com:29347:/home/workdir/polkadots.jpg")

Alternatively, you can get your ssh_host and ssh_port as described above and use sftp locally:

sftp -P ${ssh_port} root@${ssh_host}

After connecting, you can interactively move files or do other commands:

put ./helloWorld.py ./helloWorld.py
get passwds passwds
chmod 775 ./script.sh
...

Deployment instructions

This repo is deployed to pypi using poetry. Simply run poetry build, then poetry publish

Metadata

Release files for vast-api 1.0.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 vast-api 1.0.0
File Size Uploaded Provenance Actions
vast_api-1.0.0.tar.gz 7.4 kB Without provenance Details for vast_api-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vast-api 1.0.0
File Interpreter ABI Platform Actions
vast_api-1.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details for vast_api-1.0.0-py2.py3-none-any.whl

Total release size: 16.2 kB

Release files / vast_api-1.0.0.tar.gz

Download URL vast_api-1.0.0.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e1fc347875095ec6d762beb1cd37d0282799c80ddf72b0d5bf61e7b9f4872fed
BLAKE2b-256 checksum
How to use checksums
43ed8a8230cedf121eecf83635e098db8ea343d21dbb3d761e526a6322477b3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.7

Release files / vast_api-1.0.0-py2.py3-none-any.whl

Download URL vast_api-1.0.0-py2.py3-none-any.whl
Size 8.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
efd81fd98dd3eb5df2f7534e928508ffb5b6c627672ebb21250b0d3f8b21b68f
BLAKE2b-256 checksum
How to use checksums
3b18f77112e28c4e70d4e6127c84107a0f731fa13c62ad3b1c55e424164f7c2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.7

Security for vast-api 1.0.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

1.0.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