# Define template
<subtitle>How to create your own template</subtitle>

## Method chain

All template methods return template instances, allowing streaming API usage:

```python
template = (
    Template()
    .from_ubuntu_image("22.04")
    .set_workdir("/app")
    .copy("package.json", "/app/package.json")
    .run_cmd("npm install")
    .set_start_cmd("npm start", wait_for_timeout(10_000)))
```

## User and working directory

Set the working directory and user for the template:

```python
# Set working directory
template.set_workdir("/app")

#Set user (subsequent commands will be run as this user)
template.set_user("node")
template.set_user("1000:1000") # User ID and group ID
```

## Copy files

Copy files from local file system to template:

```python
# Copy a single file
template.copy("package.json", "/app/package.json")

# Copy multiple files to the same destination
template.copy(["file1", "file2"], "/app/file")

# Use copy_items for multiple copy operations
template.copy_items([
    {"src": "src/", "dest": "/app/src/"},
    {"src": "package.json", "dest": "/app/package.json"},
])

# Copy using user and mode options
template.copy("config.json", "/app/config.json", user="appuser", mode=0o644)
```

## File operations

Perform various file operations during template building:

```python
# Delete files or directories
template.remove("/tmp/old-file")
template.remove("/tmp/old-dir", recursive=True)
template.remove("/tmp/file", force=True) # Forced deletion

# Rename file or directory
template.rename("/old/path", "/new/path")
template.rename("/old/path", "/new/path", force=True) # Force rename

#Create directory
template.make_dir("/app/data")
template.make_dir("/app/data", mode=0o755) # Set permissions

#Create symbolic link
template.make_symlink("/path/to/target", "/path/to/link")
```

## Installation package

Install the package using the package manager:

```python
#Install Python package
template.pip_install(["requests", "pandas", "numpy"])

# Install Python package (user)
template.pip_install(["requests", "pandas", "numpy"], g=False)

# Install Node.js package
template.npm_install(["express", "lodash"])

# Install Node.js package (global)
template.npm_install(["express", "lodash"], g=True)

# Install Bun package
template.bun_install(["express", "lodash"])

#Install Bun package (global)
template.bun_install(["express", "lodash"], g=True)

# Install system packages (Ubuntu/Debian)
template.apt_install(["curl", "wget", "git"])
```

## Git operations

Clone the Git repository during template build (requires `git` to be installed):

```python
# Clone repository
template.git_clone("https://github.com/user/repo.git")

# Clone the repository to a specific path
template.git_clone("https://github.com/user/repo.git", "/app/repo")

# Clone a specific branch
template.git_clone("https://github.com/user/repo.git", "/app/repo", branch="main")

# Shallow clone (limited depth)
template.git_clone("https://github.com/user/repo.git", "/app/repo", depth=1)
```

## Environment variables

> Environment variables set in template definitions are only available during template build. [How to set environment variables in sandbox? ](/docs/agent-sandbox/sandbox/environment-variables.md)

Set environment variables in the template:

```python
template.set_envs({
    "NODE_ENV": "production",
    "API_KEY": "your-api-key",
    "DEBUG": "true",
})
```

## Run command

Execute shell commands during template building:

```python
# Run a single command
template.run_cmd("apt-get update && apt-get install -y curl")

# Run multiple commands
template.run_cmd(["apt-get update", "apt-get install -y curl", "curl --version"])

#Run commands as a specific user
template.run_cmd("npm install", user="node")
```
