Introduction
A template in the Vast.ai API is a configuration bundle that stores default settings for instance creation. Instead of specifying every parameter each time you create an instance, you can reference a template by itshash_id and optionally override specific values.
Templates are useful for:
- Standardization: Ensure all team members launch instances with consistent configurations
- Convenience: Avoid repeating the same parameters across multiple API calls
- Sharing: Share configurations via template hash ID
Template Fields Reference
When creating a template, the following fields can be configured:| Field | Type | Description |
|---|---|---|
name | string | Required. Human-readable name for the template |
image | string | Required. Docker image path (e.g., vastai/pytorch) |
tag | string | Docker image tag. Defaults to latest |
desc | string | Description of the template |
readme | string | Longer documentation/readme content |
env | string | Environment variables and port mappings in Docker flag format (e.g., "-e VAR=val -p 8000:8000") |
onstart | string | Shell commands to run when instance starts |
runtype | string | Launch mode: ssh, jupyter, or args. Defaults to args |
args_str | string | Arguments string passed to the container entrypoint |
ssh_direct | boolean | Enable direct SSH connection (recommended with runtype: "ssh") |
use_ssh | boolean | Enable SSH access |
jup_direct | boolean | Enable direct Jupyter connection |
jupyter_dir | string | Directory to launch Jupyter from |
use_jupyter_lab | boolean | Use JupyterLab instead of Jupyter Notebook |
docker_login_repo | string | Private Docker registry URL |
docker_login_user | string | Username for private registry |
docker_login_pass | string | Access token for private registry |
href | string | Link to Docker Hub or image documentation |
repo | string | Repository identifier (e.g., library/ubuntu) |
extra_filters | object | Default machine search filters (e.g., {"cuda_max_good": {"gte": 12.6}}) |
recommended_disk_space | number | Recommended disk space in GB (default: 8) |
private | boolean | Whether the template is private |
volume_info | object | UI hint for volume configuration (not used for actual instance creation) |
Template Identifiers
Templates have two primary identifiers:| Identifier | Type | Description |
|---|---|---|
id | integer | Numeric identifier. Used for deleting templates |
hash_id | string | Content-based hash. Used for creating instances and editing templates |
- Create instance: Use
template_hash_id - Edit template: Use
hash_id(via PUT) - Delete template: Use numeric
id
Precedence Rules
When you create an instance with both a template and additional parameters, the following precedence rules apply:| Field Type | Behavior |
|---|---|
| Scalar fields (image, disk, runtype, etc.) | Request value overrides template value |
env (string) | Merged. Template values retained, request values appended. Conflicting keys use request value |
extra_filters (dict) | Merged by key. Request values win on conflicts |
Example: Environment Variables
Theenv field uses Docker flag format as a string. When you provide env in your request, it is merged with the template’s env - existing values are retained and new values are appended.
Template configuration:
MODEL_ID=mistralai/Mistral-7B-v0.1(request overrides template)MAX_TOKENS=4096(retained from template)HF_TOKEN=hf_xxx(added from request)
Cookbook Examples
Search for Templates
Search for templates usingselect_filters with comparison operators.
Query Syntax:
eq, neq, lt, lte, gt, gte, in, notin
Available Fields:
| Field | Type | Description |
|---|---|---|
creator_id | int | ID of creator |
created_at | float | Time of initial template creation (UTC epoch) |
count_created | int | Number of instances created (popularity) |
default_tag | string | Image default tag |
docker_login_repo | string | Image docker repository |
id | int | Template unique ID |
image | string | Image used for template |
jup_direct | bool | Supports jupyter direct |
hash_id | string | Unique hash ID of template |
name | string | Displayable name |
recent_create_date | float | Last time of instance creation (UTC epoch) |
recommended_disk_space | float | Min disk space required |
recommended | bool | Is template on recommended list |
ssh_direct | bool | Supports SSH direct |
tag | string | Image tag |
use_ssh | bool | Supports SSH (direct or proxy) |
Create a New Template
Create a reusable template. This example shows a recommended configuration with SSH direct access.Full Template Example
Here’s a complete template creation request with all common fields:Edit a Template
Edit an existing template using itshash_id. You only need to include the fields you want to change - unchanged fields retain their existing values.
The
hash_id will change after editing since it is derived from the template content.Delete a Template
Delete a template by passing its numericid (not hash_id) in the request body.
Create Instance from Template
Launch an instance using a template. No need to specifyimage as the template provides it.
Create Instance with Image Override
Use a template but specify a different Docker image.Override Environment Variables
Replace template environment variables with new values.Create Instance with Volume
Attach a volume when creating an instance. You can either link an existing volume or create a new one.The
volume_info field stored in templates is a UI hint only. To actually attach a volume, you must include volume_info in the instance creation request.CLI Reference
The Vast.ai CLI provides commands for template management:| Command | Description |
|---|---|
vastai create template --name <name> --image <image> [options] | Create a new template |
vastai search templates [filters] | Search for templates |
vastai update template <hash_id> [options] | Update a template (uses hash_id) |
vastai delete template --template-id <id> | Delete a template (uses numeric id) |
--name NAME- Template name--image IMAGE- Docker image--image_tag TAG- Image tag--env ENV- Docker options (env vars and ports)--ssh- Launch as SSH instance--jupyter- Launch as Jupyter instance--direct- Use direct connections--onstart-cmd CMD- Onstart script--disk_space GB- Disk space in GB--desc DESC- Description--readme README- Readme content--public- Make template public
The CLI
update template command takes hash_id as its argument, while delete template uses the numeric id.Runtype and Connection Options
Theruntype field controls the launch mode of your instance:
| Runtype | Description |
|---|---|
args | Default. Container runs with arguments from args_str |
ssh | SSH access enabled. Recommended with ssh_direct: true |
jupyter | Jupyter notebook/lab access |
runtype: "ssh" with ssh_direct: true and use_ssh: true for reliable SSH access to your instances.
The args_str field is used when runtype is args to pass arguments to the container entrypoint:
Common Pitfalls
I'm getting an error using template_id for instance creation
I'm getting an error using template_id for instance creation
Instance creation requires
template_hash_id, not template_id. The numeric id is only used for deleting templates. Use the hash_id returned when you create or search for templates:I passed template + image and got the wrong image
I passed template + image and got the wrong image
When you specify both
template_hash_id and image, the request’s image overrides the template’s image. If you want to use the template’s image, omit the image field from your request.My environment variable didn't apply
My environment variable didn't apply
The
env field is a string using Docker flag format, not a JSON object. If your variable isn’t appearing:- Use the format:
"-e VAR1=value1 -e VAR2=value2" - Include port mappings with
-p:"-e VAR=val -p 8000:8000" - Request
envis merged with templateenv- existing keys are overwritten, new keys are appended
Ports aren't open on my instance
Ports aren't open on my instance
Port mappings are specified in the For SSH access, use
env string using Docker’s -p flag:runtype: "ssh" with ssh_direct: true.Volume didn't mount
Volume didn't mount
Volume mounting uses the To create a new volume:Where:
volume_info structure in the instance creation request. Note that volume_info in templates is just a UI hint and doesn’t affect instance creation.To link an existing volume:volume_idis either an existing volume ID (fromshow volumes) or a volume offer ID (fromsearch volumes)sizeis only used whencreate_newis truemount_pathis where the volume mounts inside the container
Template search returns no results
Template search returns no results
Template search uses
select_filters with comparison operators, not free-text search:- Use the correct filter syntax:
{"field": {"op": value}} - Valid operators:
eq,neq,lt,lte,gt,gte,in,notin - Verify your API key has
user_readpermissions - Check available fields in the search documentation above