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:Template Identifiers
Templates have two primary identifiers:
Usage by operation:
- 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:Example: Environment Variables
When creating an instance, theenv field is passed as a JSON object (dict). When you provide env in your request, it is merged with the template’s env, existing values are retained and new values are added.
Templates store
env as a Docker flag string (e.g., "-e VAR=val -p 8000:8000"), but instance creation uses a dict format. The API handles the conversion automatically when merging.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:
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
Override template environment variables with new values. Instance creation uses the dict format forenv.
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:
Create template options:
--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:
Recommendation: Use
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. It replaces the image’s Docker CMD, if the image defines an ENTRYPOINT, args_str is passed as arguments to it. If the image has no ENTRYPOINT (only CMD), args_str replaces the command entirely.
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
Template creation and instance creation use different formats for the
env field:- Templates: Docker flag string format,
"-e VAR1=value1 -e VAR2=value2 -p 8000:8000" - Instance creation: Dict format,
{"VAR1": "value1", "VAR2": "value2", "-p 8000:8000": "1"}
env (dict) is merged with the template env, existing keys are overwritten, new keys are added.Ports aren't open on my instance
Ports aren't open on my instance
When creating instances, port mappings are specified in the For SSH access, use
env dict using the -p syntax as keys: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
Related Resources
Templates Introduction
Web interface guide for templates
Create Instance API
Full API reference for instance creation
CLI Commands
Command-line interface for templates
Search Offers API
Find available machines to rent