Skip to main content

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 its hash_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
For information about managing templates in the web interface, see Templates Introduction.

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

The env 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:
Instance creation request:
Resulting instance environment:
  • 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 using select_filters with comparison operators. Query Syntax:
Operators: 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 its hash_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 numeric id (not hash_id) in the request body.

Create Instance from Template

Launch an instance using a template. No need to specify image 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.
Link an existing volume:
Create a new volume:
CLI equivalent:

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

The runtype 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 to pass arguments to the container entrypoint:

Common Pitfalls

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:
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.
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 env is merged with template env - existing keys are overwritten, new keys are appended
Port mappings are specified in the env string using Docker’s -p flag:
For SSH access, use runtype: "ssh" with ssh_direct: true.
Volume mounting uses the 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:
To create a new volume:
Where:
  • volume_id is either an existing volume ID (from show volumes) or a volume offer ID (from search volumes)
  • size is only used when create_new is true
  • mount_path is where the volume mounts inside the container
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_read permissions
  • Check available fields in the search documentation above

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