Prerequisites
- A Vast.ai account with credit (~$0.01–0.05, depending on test instance run time)
- Python 3 with the
vastaipackage installed
Install the SDK
Import and Initialize
Resource Methods
Most CLI commands have a direct SDK equivalent. For example,vastai show instances becomes vast.show_instances(). Your IDE will surface type hints and available parameters automatically.
1. Get Your API Key
Generate an API key from the Keys page by clicking +New. Copy the key — you’ll only see it once. Export it so scripts can pick it up:2. Verify Authentication
Confirm your key works by fetching your account info:credit field shows your available balance, and ssh_key is the public key that will be injected into new instances.
If you get an authentication error, double-check your API key.
3. Search for GPUs
Find available machines usingsearch_offers(). This query returns the top 5 on-demand RTX 4090s sorted by deep learning performance per dollar:
query string controls a different filter:
| Filter | Meaning |
|---|---|
gpu_name=RTX_4090 | Filter to a specific GPU model |
num_gpus=1 | Exactly 1 GPU per instance |
verified=True | Only machines verified by Vast.ai |
rentable=True | Only machines currently available to rent |
direct_port_count>=1 | At least 1 directly accessible port (needed for SSH) |
order parameter sorts results — a trailing - means descending. The type parameter selects on-demand pricing (vs. interruptible spot/bid).
Note the id of the offer you want — you’ll use it in the next step. If no offers are returned, try relaxing your filters (e.g. a different GPU model or removing direct_port_count>=1).
4. Register Your SSH Key
Do this before creating an instance. Your SSH public key must be registered on your account — it is applied at container creation time.5. Create an Instance
Rent a machine by callingcreate_instance() with an offer id from step 3 (search):
new_contract value — this is your instance ID.
| Parameter | Meaning |
|---|---|
id | The offer ID from search_offers() |
image | Docker image to run |
disk | Disk space in GB |
onstart_cmd | Shell command(s) to run when the instance boots |
direct | True for direct SSH (lower latency than proxy SSH) |
Setting
direct=True is equivalent to "runtype": "ssh_direct" in the REST API. Recommended for interactive work.For long
onstart_cmd scripts, gzip and base64 encode them — see the Template Settings page for the workaround.6. Wait Until Ready
The instance needs time to pull the Docker image and boot. Poll withshow_instance() until actual_status is "running":
actual_status field progresses through these states:
actual_status | Meaning |
|---|---|
null | Instance is being provisioned |
"loading" | Docker image is downloading |
"running" | Ready to use |
7. Connect via SSH
Use thessh_host and ssh_port from the status response to connect:
SSH_HOST and SSH_PORT with the values printed by the polling loop above.
8. Copy Data
The SDK providescopy() for file transfers. Use the instance ID to reference remote paths:
9. Clean Up
When you’re done, destroy the instance to stop all billing:stop_instance(). Stopping halts compute billing but disk storage charges continue:
Next Steps
You’ve completed the full instance lifecycle through the SDK. From here:- SSH setup — See the SSH guide for key configuration and advanced connection options.
- Use templates — Avoid repeating image and config parameters on every create call. The Templates API guide covers creating, sharing, and launching from templates.
- Full method reference — Browse all available SDK methods in the VastAI Client reference.