Console Playground ▶

CLI

Fast command-line client for code execution and interactive sessions. 42+ languages, 30+ shells/REPLs.

Official OpenAPI Swagger Docs ↗

Quick Start — Tcl

# Download + setup
curl -O https://git.unturf.com/engineering/unturf/un-inception/-/raw/main/clients/tcl/sync/src/un.tcl && chmod +x un.tcl && ln -sf un.tcl un
export UNSANDBOX_PUBLIC_KEY="unsb-pk-xxxx-xxxx-xxxx-xxxx"
export UNSANDBOX_SECRET_KEY="unsb-sk-xxxxx-xxxxx-xxxxx-xxxxx"
# Run code
./un script.tcl

Downloads

Install Guide →
Static Binary
Linux x86_64 (5.3MB)
un
Tcl SDK
un.tcl (66.1 KB)
Download

Features

  • 42+ languages - Python, JS, Go, Rust, C++, Java...
  • Sessions - 30+ shells/REPLs, tmux persistence
  • Files - Upload files, collect artifacts
  • Services - Persistent containers with domains
  • Snapshots - Point-in-time backups
  • Images - Publish, share, transfer

Integration Quickstart ⚡

Add unsandbox superpowers to your existing Tcl app:

1
Download
curl -O https://git.unturf.com/engineering/unturf/un-inception/-/raw/main/clients/tcl/sync/src/un.tcl
2
Set API Keys
# Option A: Environment variables
export UNSANDBOX_PUBLIC_KEY="unsb-pk-xxxx-xxxx-xxxx-xxxx"
export UNSANDBOX_SECRET_KEY="unsb-sk-xxxxx-xxxxx-xxxxx-xxxxx"

# Option B: Config file (persistent)
mkdir -p ~/.unsandbox
echo "unsb-pk-xxxx-xxxx-xxxx-xxxx,unsb-sk-xxxxx-xxxxx-xxxxx-xxxxx" > ~/.unsandbox/accounts.csv
3
Hello World
# In your Tcl app:
source un.tcl

set result [execute_code "tcl" {puts "Hello from Tcl running on unsandbox!"}]
puts [dict get $result stdout]  ;# Hello from Tcl running on unsandbox!
Demo cooldown: s
stdout:

                      
JSON Response:

                      
4
Run
tclsh myapp.tcl
Source Code 📄 (1725 lines)
MD5: ecc5a2c9295ec600a5eabd80947b735b SHA256: 63a9ce867f459793303b85c12da206dfb678bea78d42d8c6755d05f3843eb30b
#!/usr/bin/env tclsh
# PUBLIC DOMAIN - NO LICENSE, NO WARRANTY
#
# unsandbox.com TCL SDK (Synchronous)
# Full API with execution, sessions, services, snapshots, and images.
#
# Library Usage:
#     source un.tcl
#     set result [Un::execute "python" "print(42)"]
#     puts [dict get $result stdout]
#
# CLI Usage:
#     tclsh un.tcl script.py
#     tclsh un.tcl -s python 'print(42)'
#     tclsh un.tcl session --list
#     tclsh un.tcl service --list
#
# Copyright 2025 TimeHexOn & foxhop & russell@unturf

package require http
package require json
package require json::write
package require tls
package require base64
package require sha256

# Register https support
::http::register https 443 ::tls::socket

namespace eval Un {
    variable VERSION "4.2.50"
    variable API_BASE [string trimright [expr {[info exists ::env(UNSANDBOX_API_URL)] && $::env(UNSANDBOX_API_URL) ne "" ? $::env(UNSANDBOX_API_URL) : "https://api.unsandbox.com"}] /]
    variable PORTAL_BASE [string trimright [expr {[info exists ::env(UNSANDBOX_PORTAL_URL)] && $::env(UNSANDBOX_PORTAL_URL) ne "" ? $::env(UNSANDBOX_PORTAL_URL) : "https://unsandbox.com"}] /]
    variable LANGUAGES_CACHE_TTL 3600
    variable LANGUAGES_CACHE_FILE [file join $::env(HOME) ".unsandbox" "languages.json"]
    variable LAST_ERROR ""
    variable ACCOUNT_INDEX -1
    # Target OS (--os): "" = API default (linux), else linux | freebsd
    variable TARGET_OS ""

    # Colors
    variable BLUE "\033\[34m"
    variable RED "\033\[31m"
    variable GREEN "\033\[32m"
    variable YELLOW "\033\[33m"
    variable RESET "\033\[0m"

    # Extension to language mapping
    variable EXT_MAP
    array set EXT_MAP {
        .py python .js javascript .ts typescript
        .rb ruby .php php .pl perl .lua lua
        .sh bash .go go .rs rust .c c
        .cpp cpp .cc cpp .cxx cpp
        .java java .kt kotlin .cs csharp .fs fsharp
        .hs haskell .ml ocaml .clj clojure .scm scheme
        .lisp commonlisp .erl erlang .ex elixir .exs elixir
        .jl julia .r r .R r .cr crystal
        .d d .nim nim .zig zig .v vlang
        .dart dart .groovy groovy .scala scala
        .f90 fortran .f95 fortran .cob cobol
        .pro prolog .forth forth .4th forth
        .tcl tcl .raku raku .m objc
    }

    # ============================================================================
    # Utility Functions
    # ============================================================================

    proc version {} {
        variable VERSION
        return $VERSION
    }

    proc last_error {} {
        variable LAST_ERROR
        return $LAST_ERROR
    }

    proc set_error {msg} {
        variable LAST_ERROR
        set LAST_ERROR $msg
    }

    proc detect_language {filename} {
        variable EXT_MAP
        if {$filename eq ""} { return "" }
        set ext [file extension $filename]
        if {[info exists EXT_MAP($ext)]} {
            return $EXT_MAP($ext)
        }
        return ""
    }

    proc hmac_sign {secret message} {
        if {$secret eq "" || $message eq ""} { return "" }
        return [::sha2::hmac -hex -key $secret $message]
    }

    proc health_check {} {
        variable API_BASE
        if {[catch {
            set token [::http::geturl "$API_BASE/health" -timeout 5000]
            set ncode [::http::ncode $token]
            ::http::cleanup $token
            return [expr {$ncode == 200}]
        }]} {
            return 0
        }
    }

    # ============================================================================
    # Credential Management
    # ============================================================================

    proc load_accounts_csv {path {row 0}} {
        if {![file exists $path]} { return "" }
        if {[catch {open $path r} fp]} { return "" }
        set n 0
        set result ""
        while {[gets $fp line] >= 0} {
            if {[string index $line 0] eq "#" || [string trim $line] eq ""} { continue }
            if {$n == $row} { set result $line; break }
            incr n
        }
        close $fp
        return $result
    }

    proc default_account_row {} {
        if {[info exists ::env(UNSANDBOX_ACCOUNT)] && [string is integer -strict $::env(UNSANDBOX_ACCOUNT)]} {
            return $::env(UNSANDBOX_ACCOUNT)
        }
        return 0
    }

    # The accounts.csv our credentials came from, or "" (arguments / env vars).
    # Mirrors get_credentials' tiers.
    proc credentials_csv_path {{public_key ""} {secret_key ""}} {
        variable ACCOUNT_INDEX
        if {$public_key ne "" && $secret_key ne ""} { return "" }
        set row $ACCOUNT_INDEX
        if {$row < 0} {
            if {([info exists ::env(UNSANDBOX_PUBLIC_KEY)] && [info exists ::env(UNSANDBOX_SECRET_KEY)]) || [info exists ::env(UNSANDBOX_API_KEY)]} {
                return ""
            }
            set row [default_account_row]
        }
        foreach path [list [file join $::env(HOME) ".unsandbox" "accounts.csv"] "./accounts.csv"] {
            if {[load_accounts_csv $path $row] ne ""} { return $path }
        }
        return ""
    }

    # Replace our secret (2nd field) on the line whose 1st field equals pk.
    # Every other line, field, comment and our order stay as they are.
    # Temp file 0600 in the same directory, then rename.
    proc rewrite_accounts_csv {path pk new_secret} {
        if {[catch {open $path r} fp]} { return 0 }
        fconfigure $fp -translation binary
        set lines [split [read $fp] "\n"]
        close $fp
        set changed 0
        for {set i 0} {$i < [llength $lines]} {incr i} {
            set line [lindex $lines $i]
            if {[string index [string trimleft $line] 0] eq "#"} { continue }
            set fields [split $line ","]
            if {[llength $fields] >= 2 && [string trim [lindex $fields 0]] eq $pk} {
                set cr [expr {[llength $fields] == 2 && [string index $line end] eq "\r" ? "\r" : ""}]
                lset fields 1 "$new_secret$cr"
                lset lines $i [join $fields ","]
                set changed 1
                break
            }
        }
        if {!$changed} { return 0 }
        set tmp [file join [file dirname $path] ".[file tail $path].[pid].tmp"]
        if {[catch {
            set out [open $tmp {WRONLY CREAT TRUNC} 0600]
            fconfigure $out -translation binary
            puts -nonewline $out [join $lines "\n"]
            close $out
            file attributes $tmp -permissions 0600
            file rename -force $tmp $path
        }]} {
            catch {file delete $tmp}
            return 0
        }
        return 1
    }

    # POST /keys/self/rotate-secret, body {}, signed. Returns {ncode response}.
    proc rotate_secret_request {pk sk {otp ""} {challenge ""}} {
        variable API_BASE
        set endpoint "/keys/self/rotate-secret"
        set body "{}"
        set timestamp [clock seconds]
        set headers [list Authorization "Bearer $pk" \
            X-Timestamp $timestamp \
            X-Signature [hmac_sign $sk "${timestamp}:POST:${endpoint}:${body}"]]
        if {$otp ne ""} {
            lappend headers X-Sudo-OTP $otp X-Sudo-Challenge $challenge
        }
        if {[catch {
            set token [::http::geturl "${API_BASE}${endpoint}" -method POST -headers $headers \
                -type application/json -query $body -timeout 120000]
        } err]} {
            return [list 0 "{\"error\": \"[string map {\\ \\\\ \" \\\"} $err]\"}"]
        }
        set ncode [::http::ncode $token]
        set response [::http::data $token]
        ::http::cleanup $token
        return [list $ncode $response]
    }

    proc json_field {response key} {
        if {[catch {::json::json2dict $response} d] || [catch {dict exists $d $key} has] || !$has} {
            return ""
        }
        return [dict get $d $key]
    }

    # un key --rotate [--otp CODE]: rotate this key's secret (see CLI_SPEC.md).
    proc cmd_key_rotate {otp_given otp} {
        variable RED; variable RESET
        if {[catch {get_credentials} creds]} {
            puts stderr "${RED}Error: no credentials found${RESET}"
            exit 3
        }
        lassign $creds pk sk
        if {$sk eq ""} {
            puts stderr "${RED}Error: no credentials found${RESET}"
            exit 3
        }
        set dir [file join $::env(HOME) ".unsandbox"]
        set challenge_file [file join $dir "rotate-[string range [::sha2::sha256 -hex $pk] 0 15].challenge"]

        if {$otp_given} {
            if {[catch {open $challenge_file r} fp]} {
                puts stderr "${RED}Error: no rotation in progress; run: un key --rotate${RESET}"
                exit 2
            }
            set challenge [string trim [read $fp]]
            close $fp
            lassign [rotate_secret_request $pk $sk [string trim $otp] $challenge] ncode response
            catch {file delete $challenge_file}
        } else {
            lassign [rotate_secret_request $pk $sk] ncode response
            if {$ncode == 428} {
                set challenge [json_field $response challenge_id]
                if {[catch {exec test -t 0 <@ stdin}] == 0} {
                    puts stderr "One-time code emailed to the key owner."
                    puts -nonewline stderr "Code: "
                    flush stderr
                    if {[gets stdin code] < 0} { set code "" }
                    set code [string trim $code]
                    if {$code eq ""} {
                        puts stderr "${RED}Error: cancelled${RESET}"
                        exit 1
                    }
                    lassign [rotate_secret_request $pk $sk $code $challenge] ncode response
                } else {
                    if {![file isdirectory $dir]} { file mkdir $dir }
                    file attributes $dir -permissions 0700
                    set fp [open $challenge_file {WRONLY CREAT TRUNC} 0600]
                    puts -nonewline $fp $challenge
                    close $fp
                    file attributes $challenge_file -permissions 0600
                    puts "One-time code emailed to the key owner. Finish with: un key --rotate --otp CODE"
                    return
                }
            }
        }

        if {$ncode != 200} {
            if {$ncode == 403 && [json_field $response error] eq "email_required"} {
                puts stderr "${RED}Error: this key has no email; add one or use POST /keys/recover${RESET}"
                exit 3
            }
            if {$ncode == 401} {
                puts stderr "${RED}Error: authentication failed${RESET}"
                exit 3
            }
            set msg [json_field $response message]
            if {$msg eq ""} { set msg [json_field $response error] }
            if {$msg eq ""} { set msg "HTTP $ncode" }
            puts stderr "${RED}Error: ${msg}${RESET}"
            exit 4
        }
        set new_secret [json_field $response secret]
        if {$new_secret eq ""} {
            puts stderr "${RED}Error: API returned no secret${RESET}"
            exit 4
        }

        set csv_path [credentials_csv_path]
        if {$csv_path ne "" && [rewrite_accounts_csv $csv_path $pk $new_secret]} {
            puts "Secret rotated. Updated $csv_path (public key $pk)."
        } else {
            puts "New secret (shown once, store it now): $new_secret"
            puts "Warning: UNSANDBOX_SECRET_KEY / -k still hold the old, now invalid, secret."
        }
    }

    proc get_credentials {{public_key ""} {secret_key ""} {account_index -1}} {
        variable ACCOUNT_INDEX

        # Tier 1: Arguments
        if {$public_key ne "" && $secret_key ne ""} {
            return [list $public_key $secret_key]
        }

        # Tier 2: --account N flag → bypass env vars, load CSV row N directly
        set ai $account_index
        if {$ai < 0} { set ai $ACCOUNT_INDEX }
        if {$ai >= 0} {
            set creds [load_accounts_csv [file join $::env(HOME) ".unsandbox" "accounts.csv"] $ai]
            if {$creds eq ""} { set creds [load_accounts_csv "./accounts.csv" $ai] }
            if {$creds ne ""} {
                set parts [split $creds ","]
                return [list [lindex $parts 0] [lindex $parts 1]]
            }
            set_error "Account index $ai not found in accounts.csv"
            error "Account index $ai not found in accounts.csv"
        }

        # Tier 3: Environment
        if {[info exists ::env(UNSANDBOX_PUBLIC_KEY)] && [info exists ::env(UNSANDBOX_SECRET_KEY)]} {
            return [list $::env(UNSANDBOX_PUBLIC_KEY) $::env(UNSANDBOX_SECRET_KEY)]
        }

        # Legacy fallback
        if {[info exists ::env(UNSANDBOX_API_KEY)]} {
            return [list $::env(UNSANDBOX_API_KEY) ""]
        }

        # Tier 4: Home directory (row UNSANDBOX_ACCOUNT, default 0)
        set row [default_account_row]
        set creds [load_accounts_csv [file join $::env(HOME) ".unsandbox" "accounts.csv"] $row]
        if {$creds ne ""} {
            set parts [split $creds ","]
            return [list [lindex $parts 0] [lindex $parts 1]]
        }

        # Tier 5: Local directory
        set creds [load_accounts_csv "./accounts.csv" $row]
        if {$creds ne ""} {
            set parts [split $creds ","]
            return [list [lindex $parts 0] [lindex $parts 1]]
        }

        set_error "No credentials found"
        error "No credentials found"
    }

    # ============================================================================
    # API Communication
    # ============================================================================

    proc api_request {endpoint method body {public_key ""} {secret_key ""} {extra_headers {}} {content_type "application/json"}} {
        variable API_BASE

        lassign [get_credentials $public_key $secret_key] pk sk

        # Sign exactly the body we send: a GET sends none
        if {$method eq "GET"} { set body "" }

        set headers [list Authorization "Bearer $pk" Content-Type $content_type]

        # Add HMAC signature if secret key exists
        if {$sk ne ""} {
            set timestamp [clock seconds]
            # The server verifies against the path without its query string
            # (conn.request_path), so sign only the part before '?'.
            set sign_path [lindex [split $endpoint ?] 0]
            set sig_input "${timestamp}:${method}:${sign_path}:${body}"
            set signature [hmac_sign $sk $sig_input]
            lappend headers X-Timestamp $timestamp
            lappend headers X-Signature $signature
        }

        # Add extra headers
        foreach {k v} $extra_headers {
            lappend headers $k $v
        }

        set url "${API_BASE}${endpoint}"

        if {$method eq "GET"} {
            set token [::http::geturl $url -headers $headers -timeout 300000]
        } elseif {$method eq "DELETE" && $body eq ""} {
            set token [::http::geturl $url -method DELETE -headers $headers -timeout 300000]
        } else {
            set token [::http::geturl $url -method $method -headers $headers -query $body -timeout 300000]
        }

        set ncode [::http::ncode $token]
        set response [::http::data $token]
        ::http::cleanup $token

        return [list $ncode $response]
    }

    proc api_request_json {endpoint method body {public_key ""} {secret_key ""}} {
        lassign [api_request $endpoint $method $body $public_key $secret_key] ncode response
        if {$ncode >= 200 && $ncode < 300} {
            if {[catch {::json::json2dict $response} result]} {
                return $response
            }
            return $result
        }
        set_error "API error ($ncode): $response"
        error "API error ($ncode)"
    }

    proc api_request_with_sudo {endpoint method body {public_key ""} {secret_key ""}} {
        variable YELLOW; variable RED; variable GREEN; variable RESET

        lassign [api_request $endpoint $method $body $public_key $secret_key] ncode response

        # Handle 428 - Sudo OTP required
        if {$ncode == 428} {
            set challenge_id ""
            if {[catch {::json::json2dict $response} resp_dict] == 0} {
                if {[dict exists $resp_dict challenge_id]} {
                    set challenge_id [dict get $resp_dict challenge_id]
                }
            }

            puts stderr "${YELLOW}Confirmation required. Check your email for a one-time code.${RESET}"
            puts -nonewline stderr "Enter OTP: "
            flush stderr

            gets stdin otp
            set otp [string trim $otp]

            if {$otp eq ""} {
                set_error "Operation cancelled"
                error "Operation cancelled"
            }

            # Retry with sudo headers
            set extra_headers [list X-Sudo-OTP $otp]
            if {$challenge_id ne ""} {
                lappend extra_headers X-Sudo-Challenge $challenge_id
            }

            lassign [api_request $endpoint $method $body $public_key $secret_key $extra_headers] ncode response
        }

        if {$ncode >= 200 && $ncode < 300} {
            if {[catch {::json::json2dict $response} result]} {
                return $response
            }
            return $result
        }
        set_error "API error ($ncode)"
        error "API error ($ncode)"
    }

    # Target OS: returns lowercase linux or freebsd, raises an error otherwise
    proc normalize_os {value} {
        set os [string tolower $value]
        if {$os eq "linux" || $os eq "freebsd"} {
            return $os
        }
        set_error "invalid os '$value' (expected linux or freebsd)"
        error "invalid os '$value' (expected linux or freebsd)"
    }

    # Add "os" to a JSON object body when a target OS is given (or set via --os)
    proc with_os {body target_os} {
        variable TARGET_OS
        if {$target_os eq ""} { set target_os $TARGET_OS }
        if {$target_os eq ""} { return $body }
        set os [normalize_os $target_os]
        set body [string trimright $body "\}"]
        append body ",\"os\":\"$os\"\}"
        return $body
    }

    # ============================================================================
    # Execution Functions (8)
    # ============================================================================

    proc execute {language code {network_mode "zerotrust"} {public_key ""} {secret_key ""} {target_os ""}} {
        set body [::json::write object \
            language [::json::write string $language] \
            code [::json::write string $code] \
            network_mode [::json::write string $network_mode] \
            ttl 60]
        set body [with_os $body $target_os]
        return [api_request_json "/execute" "POST" $body $public_key $secret_key]
    }

    proc execute_async {language code {network_mode "zerotrust"} {public_key ""} {secret_key ""} {target_os ""}} {
        set body [::json::write object \
            language [::json::write string $language] \
            code [::json::write string $code] \
            network_mode [::json::write string $network_mode] \
            ttl 300]
        set body [with_os $body $target_os]
        return [api_request_json "/execute/async" "POST" $body $public_key $secret_key]
    }

    proc wait_job {job_id {public_key ""} {secret_key ""}} {
        set delays {300 450 700 900 650 1600 2000}
        for {set i 0} {$i < 120} {incr i} {
            set job [get_job $job_id $public_key $secret_key]
            set status [dict get $job status]
            if {$status eq "completed"} { return $job }
            if {$status eq "failed"} {
                set_error "Job failed"
                error "Job failed"
            }
            set delay [lindex $delays [expr {$i % 7}]]
            after $delay
        }
        set_error "Max polls exceeded"
        error "Max polls exceeded"
    }

    proc get_job {job_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/jobs/$job_id" "GET" "" $public_key $secret_key]
    }

    proc cancel_job {job_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/jobs/$job_id" "DELETE" "" $public_key $secret_key]
    }

    proc list_jobs {{public_key ""} {secret_key ""}} {
        return [api_request_json "/jobs" "GET" "" $public_key $secret_key]
    }

    proc get_languages {{public_key ""} {secret_key ""}} {
        variable LANGUAGES_CACHE_FILE; variable LANGUAGES_CACHE_TTL

        # Check cache
        if {[file exists $LANGUAGES_CACHE_FILE]} {
            set mtime [file mtime $LANGUAGES_CACHE_FILE]
            set age [expr {[clock seconds] - $mtime}]
            if {$age < $LANGUAGES_CACHE_TTL} {
                if {[catch {open $LANGUAGES_CACHE_FILE r} fp] == 0} {
                    set content [read $fp]
                    close $fp
                    if {[catch {::json::json2dict $content} cache]} {
                        return [dict get $cache languages]
                    }
                }
            }
        }

        # Fetch from API
        set result [api_request_json "/languages" "GET" "" $public_key $secret_key]

        # Save to cache
        file mkdir [file dirname $LANGUAGES_CACHE_FILE]
        set cache_json [::json::write object \
            languages [dict get $result languages] \
            timestamp [clock seconds]]
        set fp [open $LANGUAGES_CACHE_FILE w]
        puts -nonewline $fp $cache_json
        close $fp

        return [dict get $result languages]
    }

    # ============================================================================
    # Session Functions (9)
    # ============================================================================

    proc session_list {{public_key ""} {secret_key ""}} {
        return [api_request_json "/sessions" "GET" "" $public_key $secret_key]
    }

    proc session_get {session_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/sessions/$session_id" "GET" "" $public_key $secret_key]
    }

    proc session_create {{shell "bash"} {network ""} {vcpu ""} {public_key ""} {secret_key ""} {target_os ""}} {
        set body [::json::write object shell [::json::write string $shell]]
        set body [with_os $body $target_os]
        if {$network ne ""} {
            set body [string trimright $body "\}"]
            append body ",\"network\":\"$network\"\}"
        }
        if {$vcpu ne ""} {
            set body [string trimright $body "\}"]
            append body ",\"vcpu\":$vcpu\}"
        }
        return [api_request_json "/sessions" "POST" $body $public_key $secret_key]
    }

    proc session_destroy {session_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/sessions/$session_id" "DELETE" "" $public_key $secret_key]
    }

    proc session_freeze {session_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/sessions/$session_id/freeze" "POST" "\{\}" $public_key $secret_key]
    }

    proc session_unfreeze {session_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/sessions/$session_id/unfreeze" "POST" "\{\}" $public_key $secret_key]
    }

    proc session_boost {session_id {vcpu 2} {public_key ""} {secret_key ""}} {
        set body "\{\"vcpu\":$vcpu\}"
        return [api_request_json "/sessions/$session_id/boost" "POST" $body $public_key $secret_key]
    }

    proc session_unboost {session_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/sessions/$session_id/unboost" "POST" "\{\}" $public_key $secret_key]
    }

    proc session_execute {session_id command {public_key ""} {secret_key ""}} {
        set body [::json::write object command [::json::write string $command]]
        return [api_request_json "/sessions/$session_id/execute" "POST" $body $public_key $secret_key]
    }

    # ============================================================================
    # Service Functions (17)
    # ============================================================================

    proc service_list {{public_key ""} {secret_key ""}} {
        return [api_request_json "/services" "GET" "" $public_key $secret_key]
    }

    proc service_get {service_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/services/$service_id" "GET" "" $public_key $secret_key]
    }

    proc service_create {name {ports ""} {bootstrap ""} {public_key ""} {secret_key ""} {target_os ""}} {
        set body [::json::write object name [::json::write string $name]]
        set body [with_os $body $target_os]
        if {$ports ne ""} {
            set body [string trimright $body "\}"]
            append body ",\"ports\":\[$ports\]\}"
        }
        if {$bootstrap ne ""} {
            set body [string trimright $body "\}"]
            append body ",\"bootstrap\":\"[string map {\" \\\" \\ \\\\} $bootstrap]\"\}"
        }
        return [api_request_json "/services" "POST" $body $public_key $secret_key]
    }

    proc service_destroy {service_id {public_key ""} {secret_key ""}} {
        return [api_request_with_sudo "/services/$service_id" "DELETE" "" $public_key $secret_key]
    }

    proc service_freeze {service_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/services/$service_id/freeze" "POST" "\{\}" $public_key $secret_key]
    }

    proc service_unfreeze {service_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/services/$service_id/unfreeze" "POST" "\{\}" $public_key $secret_key]
    }

    proc service_lock {service_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/services/$service_id/lock" "POST" "\{\}" $public_key $secret_key]
    }

    proc service_unlock {service_id {public_key ""} {secret_key ""}} {
        return [api_request_with_sudo "/services/$service_id/unlock" "POST" "\{\}" $public_key $secret_key]
    }

    proc service_set_unfreeze_on_demand {service_id enabled {public_key ""} {secret_key ""}} {
        set body "\{\"unfreeze_on_demand\":$enabled\}"
        return [api_request_json "/services/$service_id" "PATCH" $body $public_key $secret_key]
    }

    proc service_redeploy {service_id {bootstrap ""} {public_key ""} {secret_key ""}} {
        set body "\{\}"
        if {$bootstrap ne ""} {
            set body [::json::write object bootstrap [::json::write string $bootstrap]]
        }
        return [api_request_json "/services/$service_id/redeploy" "POST" $body $public_key $secret_key]
    }

    proc service_logs {service_id {lines ""} {public_key ""} {secret_key ""}} {
        set endpoint "/services/$service_id/logs"
        if {$lines ne ""} {
            append endpoint "?lines=$lines"
        }
        return [api_request_json $endpoint "GET" "" $public_key $secret_key]
    }

    proc service_execute {service_id command {public_key ""} {secret_key ""}} {
        set body [::json::write object command [::json::write string $command]]
        return [api_request_json "/services/$service_id/execute" "POST" $body $public_key $secret_key]
    }

    proc service_env_get {service_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/services/$service_id/env" "GET" "" $public_key $secret_key]
    }

    proc service_env_set {service_id env_content {public_key ""} {secret_key ""}} {
        lassign [api_request "/services/$service_id/env" "PUT" $env_content $public_key $secret_key {} "text/plain"] ncode response
        return [expr {$ncode >= 200 && $ncode < 300}]
    }

    proc service_env_delete {service_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/services/$service_id/env" "DELETE" "" $public_key $secret_key]
    }

    proc service_env_export {service_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/services/$service_id/env/export" "POST" "\{\}" $public_key $secret_key]
    }

    proc service_resize {service_id vcpu {public_key ""} {secret_key ""}} {
        set body "\{\"vcpu\":$vcpu\}"
        return [api_request_json "/services/$service_id" "PATCH" $body $public_key $secret_key]
    }

    # ============================================================================
    # Snapshot Functions (9)
    # ============================================================================

    proc snapshot_list {{public_key ""} {secret_key ""}} {
        return [api_request_json "/snapshots" "GET" "" $public_key $secret_key]
    }

    proc snapshot_get {snapshot_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/snapshots/$snapshot_id" "GET" "" $public_key $secret_key]
    }

    proc snapshot_session {session_id {name ""} {hot false} {public_key ""} {secret_key ""}} {
        set body "\{\}"
        if {$name ne "" || $hot} {
            set parts [list]
            if {$name ne ""} { lappend parts "\"name\":\"$name\"" }
            if {$hot} { lappend parts "\"hot\":true" }
            set body "\{[join $parts ","]\}"
        }
        return [api_request_json "/sessions/$session_id/snapshot" "POST" $body $public_key $secret_key]
    }

    proc snapshot_service {service_id {name ""} {hot false} {public_key ""} {secret_key ""}} {
        set body "\{\}"
        if {$name ne "" || $hot} {
            set parts [list]
            if {$name ne ""} { lappend parts "\"name\":\"$name\"" }
            if {$hot} { lappend parts "\"hot\":true" }
            set body "\{[join $parts ","]\}"
        }
        return [api_request_json "/services/$service_id/snapshot" "POST" $body $public_key $secret_key]
    }

    proc snapshot_restore {snapshot_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/snapshots/$snapshot_id/restore" "POST" "\{\}" $public_key $secret_key]
    }

    proc snapshot_delete {snapshot_id {public_key ""} {secret_key ""}} {
        return [api_request_with_sudo "/snapshots/$snapshot_id" "DELETE" "" $public_key $secret_key]
    }

    proc snapshot_lock {snapshot_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/snapshots/$snapshot_id/lock" "POST" "\{\}" $public_key $secret_key]
    }

    proc snapshot_unlock {snapshot_id {public_key ""} {secret_key ""}} {
        return [api_request_with_sudo "/snapshots/$snapshot_id/unlock" "POST" "\{\}" $public_key $secret_key]
    }

    proc snapshot_clone {snapshot_id {clone_type "session"} {name ""} {public_key ""} {secret_key ""}} {
        set parts [list "\"clone_type\":\"$clone_type\""]
        if {$name ne ""} { lappend parts "\"name\":\"$name\"" }
        set body "\{[join $parts ","]\}"
        return [api_request_json "/snapshots/$snapshot_id/clone" "POST" $body $public_key $secret_key]
    }

    # ============================================================================
    # Image Functions (13)
    # ============================================================================

    proc image_list {{filter ""} {public_key ""} {secret_key ""}} {
        set endpoint "/images"
        if {$filter ne ""} {
            append endpoint "?filter=$filter"
        }
        return [api_request_json $endpoint "GET" "" $public_key $secret_key]
    }

    proc image_get {image_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/images/$image_id" "GET" "" $public_key $secret_key]
    }

    proc image_publish {source_type source_id {name ""} {public_key ""} {secret_key ""}} {
        set parts [list "\"source_type\":\"$source_type\"" "\"source_id\":\"$source_id\""]
        if {$name ne ""} { lappend parts "\"name\":\"$name\"" }
        set body "\{[join $parts ","]\}"
        return [api_request_json "/images/publish" "POST" $body $public_key $secret_key]
    }

    proc image_delete {image_id {public_key ""} {secret_key ""}} {
        return [api_request_with_sudo "/images/$image_id" "DELETE" "" $public_key $secret_key]
    }

    proc image_lock {image_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/images/$image_id/lock" "POST" "\{\}" $public_key $secret_key]
    }

    proc image_unlock {image_id {public_key ""} {secret_key ""}} {
        return [api_request_with_sudo "/images/$image_id/unlock" "POST" "\{\}" $public_key $secret_key]
    }

    proc image_set_visibility {image_id visibility {public_key ""} {secret_key ""}} {
        set body "\{\"visibility\":\"$visibility\"\}"
        return [api_request_json "/images/$image_id/visibility" "POST" $body $public_key $secret_key]
    }

    proc image_grant_access {image_id trusted_key {public_key ""} {secret_key ""}} {
        set body "\{\"api_key\":\"$trusted_key\"\}"
        return [api_request_json "/images/$image_id/access" "POST" $body $public_key $secret_key]
    }

    proc image_revoke_access {image_id trusted_key {public_key ""} {secret_key ""}} {
        return [api_request_json "/images/$image_id/access/$trusted_key" "DELETE" "" $public_key $secret_key]
    }

    proc image_list_trusted {image_id {public_key ""} {secret_key ""}} {
        return [api_request_json "/images/$image_id/access" "GET" "" $public_key $secret_key]
    }

    proc image_transfer {image_id to_key {public_key ""} {secret_key ""}} {
        set body "\{\"to_api_key\":\"$to_key\"\}"
        return [api_request_json "/images/$image_id/transfer" "POST" $body $public_key $secret_key]
    }

    proc image_spawn {image_id {name ""} {ports ""} {public_key ""} {secret_key ""}} {
        set parts [list]
        if {$name ne ""} { lappend parts "\"name\":\"$name\"" }
        if {$ports ne ""} { lappend parts "\"ports\":\[$ports\]" }
        set body "\{[join $parts ","]\}"
        return [api_request_json "/images/$image_id/spawn" "POST" $body $public_key $secret_key]
    }

    proc image_clone {image_id {name ""} {public_key ""} {secret_key ""}} {
        set body "\{\}"
        if {$name ne ""} {
            set body "\{\"name\":\"$name\"\}"
        }
        return [api_request_json "/images/$image_id/clone" "POST" $body $public_key $secret_key]
    }

    # ============================================================================
    # PaaS Logs Functions (2)
    # ============================================================================

    # Platform logs live on the portal: GET $PORTAL_BASE/logs/{source}[/stream]
    # (partner keys with log access). HMAC covers the path only: "ts:GET:/logs/api:".

    proc uri_escape {v} {
        set out ""
        foreach b [split [encoding convertto utf-8 $v] ""] {
            if {[string match {[A-Za-z0-9._~-]} $b]} {
                append out $b
            } else {
                binary scan $b cu n
                append out [format "%%%02X" $n]
            }
        }
        return $out
    }

    proc logs_query {args} {
        set q {}
        foreach {k v} $args {
            if {$v ne ""} { lappend q "$k=[uri_escape $v]" }
        }
        if {[llength $q] == 0} { return "" }
        return "?[join $q &]"
    }

    proc portal_headers {path public_key secret_key} {
        lassign [get_credentials $public_key $secret_key] pk sk
        set headers [list Authorization "Bearer $pk"]
        if {$sk ne ""} {
            set timestamp [clock seconds]
            lappend headers X-Timestamp $timestamp X-Signature [hmac_sign $sk "${timestamp}:GET:${path}:"]
        }
        return $headers
    }

    # Raw portal fetch: returns {ncode body}; raises on network errors.
    proc logs_fetch_raw {source lines since grep level public_key secret_key} {
        variable PORTAL_BASE
        set path "/logs/$source"
        set headers [portal_headers $path $public_key $secret_key]
        set url "${PORTAL_BASE}${path}[logs_query lines $lines since $since grep $grep level $level]"
        set token [::http::geturl $url -headers $headers -timeout 120000 -binary 1]
        set status [::http::status $token]
        set ncode [::http::ncode $token]
        set body [encoding convertfrom utf-8 [::http::data $token]]
        ::http::cleanup $token
        if {$status ne "ok"} { error "request $status" }
        return [list $ncode $body]
    }

    # Library: fetch logs; returns the decoded dict (raises on non-200).
    proc logs_fetch {{source "all"} {lines 100} {since "5m"} {grep ""} {public_key ""} {secret_key ""} {level ""}} {
        lassign [logs_fetch_raw $source $lines $since $grep $level $public_key $secret_key] ncode body
        if {$ncode != 200} {
            set_error "API error ($ncode): $body"
            error "API error ($ncode): $body"
        }
        return [::json::json2dict $body]
    }

    proc print_log_row {src line} {
        if {$src ne ""} {
            puts [format "\033\[36m%-12s\033\[0m %s" $src $line]
        } else {
            puts $line
        }
        flush stdout
    }

    proc log_cursor_path {} { return [file join $::env(HOME) ".unsandbox" "paas_log_cursor"] }

    proc read_log_cursor {} {
        if {[catch {set fh [open [log_cursor_path] r]}]} { return "" }
        set ts [string trim [gets $fh]]
        close $fh
        if {![string is digit -strict $ts] || $ts <= 0} { return "" }
        set ago [expr {[clock seconds] - $ts}]
        if {$ago < 5} { set ago 5 }
        return "${ago}s"
    }

    proc write_log_cursor {} {
        set dir [file join $::env(HOME) ".unsandbox"]
        catch {
            if {![file isdirectory $dir]} { file mkdir $dir; file attributes $dir -permissions 0700 }
            set fh [open [log_cursor_path] w]
            puts $fh [clock seconds]
            close $fh
        }
    }

    # SSE body handler: reads what arrived, emits complete events.
    proc stream_handler {cb sock token} {
        variable stream_buf
        variable stream_last
        variable stream_seen
        set data [read $sock]
        set stream_seen [clock seconds]
        if {[::http::ncode $token] != 200} { return [string length $data] }
        append stream_buf $data
        set stream_buf [string map {"\r\n" "\n"} $stream_buf]
        while {[set i [string first "\n\n" $stream_buf]] >= 0} {
            set event [string range $stream_buf 0 [expr {$i - 1}]]
            set stream_buf [string range $stream_buf [expr {$i + 2}] end]
            foreach line [split [encoding convertfrom utf-8 $event] "\n"] {
                if {![string match "data: *" $line]} continue
                set payload [string range $line 6 end]
                if {$payload eq ":keepalive"} continue
                set stream_last [clock seconds]
                if {![catch {::json::json2dict $payload} ev] && [string index [string trimleft $payload] 0] eq "\{"} {
                    if {[dict exists $ev line]} {
                        set src [expr {[dict exists $ev source] ? [dict get $ev source] : ""}]
                        {*}$cb $src [dict get $ev line]
                    }
                } else {
                    {*}$cb "" $payload
                }
            }
        }
        return [string length $data]
    }

    proc stream_watchdog {token} {
        variable stream_seen
        variable stream_watch
        if {[clock seconds] - $stream_seen > 120} {
            ::http::reset $token timeout
            return
        }
        set stream_watch [after 5000 [list ::Un::stream_watchdog $token]]
    }

    # Library + CLI: stream /logs/{source}/stream (SSE), reconnecting on drops
    # (2 s backoff doubling to 30 s, since=<gap>s). Returns 0 on stop, 1 on error.
    proc logs_stream {{source "all"} {grep ""} {level ""} {public_key ""} {secret_key ""} {callback ::Un::print_log_row}} {
        variable PORTAL_BASE
        variable stream_buf
        variable stream_last 0
        variable stream_seen
        variable stream_watch
        set path "/logs/$source/stream"
        puts stderr "Streaming $source logs... (Ctrl+C to stop)"
        set delay 2; set reconnect 0
        while {1} {
            set since ""
            if {$reconnect && $stream_last > 0} {
                set gap [expr {[clock seconds] - $stream_last + 5}]
                if {$gap < 10} { set gap 10 }
                set since "${gap}s"
            }
            set url "${PORTAL_BASE}${path}[logs_query grep $grep level $level since $since]"
            set headers [portal_headers $path $public_key $secret_key]
            lappend headers Accept text/event-stream
            set stream_buf ""
            set stream_seen [clock seconds]
            set started [clock seconds]
            # HTTP/1.0 so the stream arrives unchunked, closed by the server.
            if {[catch {::http::geturl $url -headers $headers -protocol 1.0 -binary 1 \
                    -handler [list ::Un::stream_handler $callback] -command ::Un::stream_done} token]} {
                puts stderr "Error: Stream error: $token"
                return 1
            }
            set stream_watch [after 5000 [list ::Un::stream_watchdog $token]]
            ::http::wait $token
            after cancel $stream_watch
            set status [::http::status $token]
            set ncode [::http::ncode $token]
            ::http::cleanup $token
            if {[clock seconds] - $started > 30} { set delay 2 }
            if {$ncode == 401 || $ncode == 403} {
                puts stderr "Error: HTTP $ncode (auth failed)"
                return 1
            }
            if {$ncode eq "" && $status eq "error"} {
                puts stderr "Error: Stream error: connection failed"
                return 1
            }
            if {$ncode == 200 || ($ncode ne "" && $ncode >= 500) || $status in {eof timeout error}} {
                after [expr {$delay * 1000}]
                set delay [expr {min($delay * 2, 30)}]
                set reconnect 1
                continue
            }
            break
        }
        write_log_cursor
        puts stderr "\nStream stopped."
        return 0
    }

    proc stream_done {token} {}

    proc paas_logs_help {} {
        set p "tclsh un.tcl"
        puts stderr "Usage: $p paas logs \[options\]

Fetch or stream PaaS production logs.

Sources:
  --all              All sources (default)
  --api              API server logs
  --portal           Portal server logs
  --pool NODE        Pool node logs (e.g., cammy, ai)
  --source SOURCE    Source string (api, portal, pool/cammy, all)

Options:
  --lines N, -n N    Number of lines (default: 100, max: 10000)
  --since TIME       Time window: 1m, 5m, 15m, 1h, 6h, 1d (default: 5m)
  --grep PATTERN     Filter log lines by pattern
  --level LVL, -l    Min log level: debug, info, notice, warning, err, crit
  --follow, -f       Follow logs in real-time (Ctrl+C to stop)
  --json             Output raw JSON response
  -p KEY             Public key
  -k KEY             Secret key
  -h                 Show this help

Examples:
  $p paas logs                             # last 100 lines from all sources
  $p paas logs --api --lines 500           # last 500 API log lines
  $p paas logs --portal --grep error       # portal logs matching 'error'
  $p paas logs --pool cammy --since 1h     # cammy pool logs from last hour
  $p paas logs -l warning                  # warnings and above from all sources
  $p paas logs --follow                    # follow all logs in real-time
  $p paas logs --follow -l warning         # follow warnings+ in real-time

Requires a partner API key with log access enabled."
    }

    proc cmd_paas {args} {
        variable ACCOUNT_INDEX
        if {[llength $args] == 0} {
            puts stderr "Usage: tclsh un.tcl paas <command> \[options\]\n\nCommands:\n  logs    Fetch or stream production logs"
            puts stderr "\nRun 'tclsh un.tcl paas <command> -h' for help on a specific command."
            return 1
        }
        set sub [lindex $args 0]
        if {$sub ne "logs"} {
            puts stderr "Error: Unknown paas command '$sub'\nRun 'tclsh un.tcl paas' for available commands."
            return 1
        }
        set source all; set lines 100; set since ""; set since_explicit 0
        set grep ""; set level ""; set follow 0; set json 0; set help 0
        set pk ""; set sk ""
        set n [llength $args]
        for {set i 1} {$i < $n} {incr i} {
            set a [lindex $args $i]
            set has [expr {$i + 1 < $n}]
            set next [lindex $args [expr {$i + 1}]]
            if {$a eq "-p" && $has} { set pk $next; incr i
            } elseif {$a eq "-k" && $has} { set sk $next; incr i
            } elseif {$a eq "--account" && $has} { set ACCOUNT_INDEX $next; incr i
            } elseif {$a eq "--source" && $has} { set source $next; incr i
            } elseif {$a eq "--all"} { set source all
            } elseif {$a eq "--api"} { set source api
            } elseif {$a eq "--portal"} { set source portal
            } elseif {$a eq "--pool" && $has} { set source "pool/$next"; incr i
            } elseif {($a eq "--lines" || $a eq "-n") && $has} {
                incr i
                if {![regexp {^\s*([-+]?[0-9]+)} $next -> v]} { set v 0 }
                scan $v %d v
                set lines [expr {max(1, min(10000, $v))}]
            } elseif {$a eq "--since" && $has} { set since $next; set since_explicit 1; incr i
            } elseif {$a eq "--grep" && $has} { set grep $next; incr i
            } elseif {($a eq "--level" || $a eq "-l") && $has} { set level $next; incr i
            } elseif {$a eq "--follow" || $a eq "-f"} { set follow 1
            } elseif {$a eq "--json"} { set json 1
            } elseif {$a eq "-h" || $a eq "--help"} { set help 1
            }
        }
        if {$help} { paas_logs_help; return 0 }
        if {[catch {get_credentials $pk $sk} creds] || [lindex $creds 0] eq ""} {
            puts stderr "Error: API credentials required."
            puts stderr "  Set UNSANDBOX_PUBLIC_KEY + UNSANDBOX_SECRET_KEY env vars, or"
            puts stderr "  Use -p PUBLIC_KEY -k SECRET_KEY flags, or"
            puts stderr "  Create ~/.unsandbox/accounts.csv with: public_key,secret_key"
            return 1
        }
        if {!$since_explicit} {
            set since [read_log_cursor]
            if {$since eq ""} { set since 5m }
        }
        if {$follow} { return [logs_stream $source $grep $level $pk $sk] }

        if {[catch {logs_fetch_raw $source $lines $since $grep $level $pk $sk} r]} {
            puts stderr "Error: Network error fetching logs: $r"
            return 1
        }
        lassign $r ncode body
        if {$ncode != 200} {
            set err ""
            catch { set err [dict get [::json::json2dict $body] error] }
            puts stderr "Error: HTTP $ncode[expr {$err ne "" ? " — $err" : ""}]"
            return 1
        }
        write_log_cursor
        if {$json} { puts $body; return 0 }
        if {[catch {::json::json2dict $body} data] || ![dict exists $data lines]} {
            puts stderr "Error: Invalid response format"
            return 1
        }
        set is_all [dict exists $data sources]
        foreach l [dict get $data lines] {
            if {$is_all} {
                set src [expr {[dict exists $l source] ? [dict get $l source] : ""}]
                set ln [expr {[dict exists $l line] ? [dict get $l line] : ""}]
                print_log_row $src $ln
            } else {
                puts $l
            }
        }
        return 0
    }

    # ============================================================================
    # Key Validation
    # ============================================================================

    proc validate_keys {{public_key ""} {secret_key ""}} {
        lassign [validate_keys_raw $public_key $secret_key] ncode response
        if {$ncode == 200} {
            return [::json::json2dict $response]
        }
        set_error "Key validation failed ($ncode)"
        error "Key validation failed"
    }

    # POST /keys/validate; returns {http_code body} whatever the status.
    proc validate_keys_raw {{public_key ""} {secret_key ""}} {
        variable API_BASE

        lassign [get_credentials $public_key $secret_key] pk sk

        set headers [list Authorization "Bearer $pk" Content-Type "application/json"]

        if {$sk ne ""} {
            set timestamp [clock seconds]
            set sig_input "${timestamp}:POST:/keys/validate:"
            set signature [hmac_sign $sk $sig_input]
            lappend headers X-Timestamp $timestamp
            lappend headers X-Signature $signature
        }

        set token [::http::geturl "$API_BASE/keys/validate" -method POST -headers $headers -timeout 30000]
        set ncode [::http::ncode $token]
        set response [::http::data $token]
        ::http::cleanup $token
        return [list $ncode $response]
    }

    # ============================================================================
    # CLI Commands
    # ============================================================================

    proc cmd_languages {args} {
        variable BLUE; variable RESET
        set json_output 0
        foreach arg $args {
            if {$arg eq "--json"} { set json_output 1 }
        }

        set langs [get_languages]

        if {$json_output} {
            puts $langs
        } else {
            foreach lang $langs {
                puts $lang
            }
        }
    }

    proc cmd_key {args} {
        variable GREEN; variable RED; variable YELLOW; variable PORTAL_BASE; variable RESET

        set extend 0
        set rotate 0
        set otp_given 0
        set otp ""
        for {set i 0} {$i < [llength $args]} {incr i} {
            set arg [lindex $args $i]
            if {$arg eq "--extend"} { set extend 1 }
            if {$arg eq "--rotate"} { set rotate 1 }
            if {$arg eq "--otp" && $i + 1 < [llength $args]} {
                set otp_given 1
                set otp [lindex $args [incr i]]
            }
        }

        if {$rotate} {
            cmd_key_rotate $otp_given $otp
            return
        }

        if {[catch {get_credentials} creds]} {
            puts stderr "${RED}Error: no credentials found${RESET}"
            exit 3
        }
        set pk [lindex $creds 0]

        if {$extend} {
            set url "$PORTAL_BASE/keys/extend?pk=$pk"
            puts "${YELLOW}Opening browser to extend key...${RESET}"
            catch {exec xdg-open $url &}
            return
        }

        if {[catch {validate_keys_raw} raw]} {
            puts stderr "${RED}Error: $raw${RESET}"
            exit 1
        }
        lassign $raw ncode response
        set result {}
        catch {set result [::json::json2dict $response]}

        if {$ncode == 200} {
            if {[dict exists $result status]} {
                set state [dict get $result status]
            } elseif {[dict exists $result valid] && [dict get $result valid]} {
                set state active
            } else {
                set state invalid
            }
            set reason "status $state"
        } elseif {$ncode == 401} {
            set reason unauthorized
            if {[dict exists $result error]} { set reason [dict get $result error] }
            switch -exact -- $reason {
                :expired - expired { set state expired }
                :suspended - suspended { set state suspended }
                default { set state invalid }
            }
        } else {
            set msg ""
            if {[dict exists $result error]} { set msg " [dict get $result error]" } elseif {[dict exists $result message]} { set msg " [dict get $result message]" }
            puts stderr "${RED}Error: HTTP $ncode$msg${RESET}"
            exit 4
        }

        switch -exact -- $state {
            active {}
            expired {
                puts "${RED}Expired${RESET}"
                puts "Public Key: $pk"
                puts "${YELLOW}To renew: Visit $PORTAL_BASE/keys/extend${RESET}"
                exit 3
            }
            suspended {
                puts "${RED}Suspended${RESET}"
                puts "Public Key: $pk"
                exit 3
            }
            default {
                puts "${RED}Invalid${RESET}"
                puts "Public Key: $pk"
                puts "Reason: $reason"
                exit 3
            }
        }

        set tier "N/A"; set status "N/A"; set expires "N/A"; set remaining "N/A"
        if {[dict exists $result tier]} { set tier [dict get $result tier] }
        if {[dict exists $result status]} { set status [dict get $result status] }
        if {[dict exists $result valid_through] && [string is double -strict [dict get $result valid_through]]} {
            set secs [expr {wide([dict get $result valid_through]) / 1000}]
            set expires [clock format $secs -format "%Y-%m-%dT%H:%M:%SZ" -gmt 1]
            set left [expr {$secs - [clock seconds]}]
            if {$left <= 0} {
                set remaining "expired"
            } else {
                set remaining "[expr {$left / 86400}]d [expr {$left % 86400 / 3600}]h [expr {$left % 3600 / 60}]m"
            }
        }

        puts "${GREEN}Valid${RESET}"
        puts "Public Key: $pk"
        puts "Tier: $tier"
        puts "Status: $status"
        puts "Expires: $expires"
        puts "Time Remaining: $remaining"
    }

    proc cmd_session {args} {
        variable GREEN; variable RESET
        set action ""
        set target ""

        for {set i 0} {$i < [llength $args]} {incr i} {
            set arg [lindex $args $i]
            switch -exact -- $arg {
                --list - -l { set action "list" }
                --info { set action "info"; incr i; set target [lindex $args $i] }
                --kill { set action "kill"; incr i; set target [lindex $args $i] }
                --freeze { set action "freeze"; incr i; set target [lindex $args $i] }
                --unfreeze { set action "unfreeze"; incr i; set target [lindex $args $i] }
            }
        }

        switch -exact -- $action {
            list {
                set result [session_list]
                if {[dict exists $result sessions]} {
                    foreach s [dict get $result sessions] {
                        puts "[dict get $s id]\t[dict get $s shell]\t[dict get $s status]\t[dict get $s created_at]"
                    }
                }
            }
            info { puts [session_get $target] }
            kill {
                session_destroy $target
                puts "${GREEN}Session terminated: $target${RESET}"
            }
            freeze {
                session_freeze $target
                puts "${GREEN}Session frozen: $target${RESET}"
            }
            unfreeze {
                session_unfreeze $target
                puts "${GREEN}Session unfreezing: $target${RESET}"
            }
            default {
                puts stderr "Usage: un.tcl session --list|--info ID|--kill ID|--freeze ID|--unfreeze ID"
                exit 1
            }
        }
    }

    proc cmd_service {args} {
        variable GREEN; variable RESET
        set action ""
        set target ""
        set name ""
        set ports ""

        for {set i 0} {$i < [llength $args]} {incr i} {
            set arg [lindex $args $i]
            switch -exact -- $arg {
                --list - -l { set action "list" }
                --info { set action "info"; incr i; set target [lindex $args $i] }
                --destroy { set action "destroy"; incr i; set target [lindex $args $i] }
                --freeze { set action "freeze"; incr i; set target [lindex $args $i] }
                --unfreeze { set action "unfreeze"; incr i; set target [lindex $args $i] }
                --lock { set action "lock"; incr i; set target [lindex $args $i] }
                --unlock { set action "unlock"; incr i; set target [lindex $args $i] }
                --logs { set action "logs"; incr i; set target [lindex $args $i] }
                --name { incr i; set name [lindex $args $i] }
                --ports { incr i; set ports [lindex $args $i] }
            }
        }

        switch -exact -- $action {
            list {
                set result [service_list]
                if {[dict exists $result services]} {
                    foreach s [dict get $result services] {
                        puts "[dict get $s id]\t[dict get $s name]\t[dict get $s status]"
                    }
                }
            }
            info { puts [service_get $target] }
            destroy {
                service_destroy $target
                puts "${GREEN}Service destroyed: $target${RESET}"
            }
            freeze {
                service_freeze $target
                puts "${GREEN}Service frozen: $target${RESET}"
            }
            unfreeze {
                service_unfreeze $target
                puts "${GREEN}Service unfreezing: $target${RESET}"
            }
            lock {
                service_lock $target
                puts "${GREEN}Service locked: $target${RESET}"
            }
            unlock {
                service_unlock $target
                puts "${GREEN}Service unlocked: $target${RESET}"
            }
            logs {
                set result [service_logs $target]
                if {[dict exists $result logs]} {
                    puts [dict get $result logs]
                }
            }
            default {
                if {$name ne ""} {
                    set result [service_create $name $ports ""]
                    puts "${GREEN}Service created${RESET}"
                    puts $result
                } else {
                    puts stderr "Usage: un.tcl service --list|--info ID|--destroy ID|--name NAME"
                    exit 1
                }
            }
        }
    }

    proc cmd_snapshot {args} {
        variable GREEN; variable RESET
        set action ""
        set target ""

        for {set i 0} {$i < [llength $args]} {incr i} {
            set arg [lindex $args $i]
            switch -exact -- $arg {
                --list - -l { set action "list" }
                --info { set action "info"; incr i; set target [lindex $args $i] }
                --delete { set action "delete"; incr i; set target [lindex $args $i] }
                --restore { set action "restore"; incr i; set target [lindex $args $i] }
                --lock { set action "lock"; incr i; set target [lindex $args $i] }
                --unlock { set action "unlock"; incr i; set target [lindex $args $i] }
            }
        }

        switch -exact -- $action {
            list {
                set result [snapshot_list]
                if {[dict exists $result snapshots]} {
                    foreach s [dict get $result snapshots] {
                        puts "[dict get $s id]\t[dict get $s name]\t[dict get $s type]"
                    }
                }
            }
            info { puts [snapshot_get $target] }
            delete {
                snapshot_delete $target
                puts "${GREEN}Snapshot deleted: $target${RESET}"
            }
            restore {
                snapshot_restore $target
                puts "${GREEN}Snapshot restored${RESET}"
            }
            lock {
                snapshot_lock $target
                puts "${GREEN}Snapshot locked: $target${RESET}"
            }
            unlock {
                snapshot_unlock $target
                puts "${GREEN}Snapshot unlocked: $target${RESET}"
            }
            default {
                puts stderr "Usage: un.tcl snapshot --list|--info ID|--delete ID|--restore ID"
                exit 1
            }
        }
    }

    proc cmd_image {args} {
        variable GREEN; variable RESET
        set action ""
        set target ""
        set source_type ""
        set visibility_mode ""
        set name ""
        set ports ""

        for {set i 0} {$i < [llength $args]} {incr i} {
            set arg [lindex $args $i]
            switch -exact -- $arg {
                --list - -l { set action "list" }
                --info { set action "info"; incr i; set target [lindex $args $i] }
                --delete { set action "delete"; incr i; set target [lindex $args $i] }
                --lock { set action "lock"; incr i; set target [lindex $args $i] }
                --unlock { set action "unlock"; incr i; set target [lindex $args $i] }
                --publish { set action "publish"; incr i; set target [lindex $args $i] }
                --source-type { incr i; set source_type [lindex $args $i] }
                --visibility {
                    set action "visibility"
                    incr i; set target [lindex $args $i]
                    incr i; set visibility_mode [lindex $args $i]
                }
                --spawn { set action "spawn"; incr i; set target [lindex $args $i] }
                --clone { set action "clone"; incr i; set target [lindex $args $i] }
                --name { incr i; set name [lindex $args $i] }
                --ports { incr i; set ports [lindex $args $i] }
            }
        }

        switch -exact -- $action {
            list {
                set result [image_list]
                if {[dict exists $result images]} {
                    foreach img [dict get $result images] {
                        set img_name "-"
                        if {[dict exists $img name]} { set img_name [dict get $img name] }
                        puts "[dict get $img id]\t$img_name\t[dict get $img visibility]"
                    }
                }
            }
            info { puts [image_get $target] }
            delete {
                image_delete $target
                puts "${GREEN}Image deleted: $target${RESET}"
            }
            lock {
                image_lock $target
                puts "${GREEN}Image locked: $target${RESET}"
            }
            unlock {
                image_unlock $target
                puts "${GREEN}Image unlocked: $target${RESET}"
            }
            publish {
                if {$source_type eq ""} {
                    puts stderr "Error: --source-type required"
                    exit 1
                }
                set result [image_publish $source_type $target $name]
                puts "${GREEN}Image published${RESET}"
                puts $result
            }
            visibility {
                image_set_visibility $target $visibility_mode
                puts "${GREEN}Visibility set to $visibility_mode${RESET}"
            }
            spawn {
                set result [image_spawn $target $name $ports]
                puts "${GREEN}Service spawned from image${RESET}"
                puts $result
            }
            clone {
                set result [image_clone $target $name]
                puts "${GREEN}Image cloned${RESET}"
                puts $result
            }
            default {
                puts stderr "Usage: un.tcl image --list|--info ID|--delete ID|--publish ID|--spawn ID|--clone ID"
                exit 1
            }
        }
    }

    proc run_file {file} {
        variable BLUE; variable RED; variable RESET

        if {![file exists $file]} {
            puts stderr "${RED}Error: File not found: $file${RESET}"
            exit 1
        }

        set fp [open $file r]
        set code [read $fp]
        close $fp

        set lang [detect_language $file]
        if {$lang eq ""} {
            puts stderr "${RED}Error: Cannot detect language${RESET}"
            exit 1
        }

        set result [execute $lang $code]

        if {[dict exists $result stdout]} {
            puts -nonewline "${BLUE}[dict get $result stdout]${RESET}"
        }
        if {[dict exists $result stderr]} {
            puts -nonewline stderr "${RED}[dict get $result stderr]${RESET}"
        }

        set exit_code 0
        if {[dict exists $result exit_code]} {
            set exit_code [dict get $result exit_code]
        }
        exit $exit_code
    }

    proc show_help {} {
        puts {Unsandbox CLI - Execute code in secure sandboxes

Usage:
  tclsh un.tcl [options] <source_file>
  tclsh un.tcl -s <language> '<code>'
  tclsh un.tcl session [options]
  tclsh un.tcl service [options]
  tclsh un.tcl snapshot [options]
  tclsh un.tcl image [options]
  tclsh un.tcl languages [--json]
  tclsh un.tcl key [--extend]
  tclsh un.tcl key --rotate [--otp CODE]

Global options:
  --os OS       Target OS: linux (default) or freebsd (execute, session, service)
  --account N   Use account N from accounts.csv

Commands:
  languages     List available programming languages
  key           Validate API key
  session       Manage interactive sessions
  service       Manage persistent services
  snapshot      Manage snapshots
  image         Manage images

Environment:
  UNSANDBOX_PUBLIC_KEY  API public key
  UNSANDBOX_SECRET_KEY  API secret key}
    }

    proc main {argv} {
        variable BLUE; variable RED; variable RESET; variable ACCOUNT_INDEX; variable TARGET_OS

        # Pre-scan for --account N and --os OS; strip them from argv before dispatch
        set filtered {}
        for {set i 0} {$i < [llength $argv]} {incr i} {
            set a [lindex $argv $i]
            if {$a eq "--account"} {
                incr i
                set ACCOUNT_INDEX [lindex $argv $i]
            } elseif {$a eq "--os" || [string match "--os=*" $a]} {
                if {$a eq "--os"} {
                    if {$i + 1 >= [llength $argv]} {
                        puts stderr "${RED}Error: --os requires a value: linux or freebsd${RESET}"
                        exit 1
                    }
                    incr i
                    set os_value [lindex $argv $i]
                } else {
                    set os_value [string range $a 5 end]
                }
                if {[catch {set TARGET_OS [normalize_os $os_value]}]} {
                    puts stderr "${RED}Error: invalid --os '$os_value' (expected linux or freebsd)${RESET}"
                    exit 1
                }
            } else {
                lappend filtered $a
            }
        }
        set argv $filtered

        if {[llength $argv] == 0} {
            show_help
            exit 1
        }

        set first_arg [lindex $argv 0]

        switch -exact -- $first_arg {
            languages { cmd_languages {*}[lrange $argv 1 end] }
            key { cmd_key {*}[lrange $argv 1 end] }
            session { cmd_session {*}[lrange $argv 1 end] }
            service { cmd_service {*}[lrange $argv 1 end] }
            snapshot { cmd_snapshot {*}[lrange $argv 1 end] }
            image { cmd_image {*}[lrange $argv 1 end] }
            paas { exit [cmd_paas {*}[lrange $argv 1 end]] }
            --help - -h { show_help }
            -s {
                set lang [lindex $argv 1]
                set code [lindex $argv 2]
                if {$lang eq "" || $code eq ""} {
                    puts stderr "${RED}Error: -s requires language and code${RESET}"
                    exit 1
                }
                set result [execute $lang $code]
                if {[dict exists $result stdout]} {
                    puts -nonewline [dict get $result stdout]
                }
                if {[dict exists $result stderr]} {
                    puts -nonewline stderr [dict get $result stderr]
                }
                set exit_code 0
                if {[dict exists $result exit_code]} {
                    set exit_code [dict get $result exit_code]
                }
                exit $exit_code
            }
            default {
                run_file $first_arg
            }
        }
    }
}

# CLI entry point
if {[info script] eq $argv0} {
    Un::main $argv
}

Documentation clarifications

Dependencies

C Binary (un1) — requires libcurl and libwebsockets:

sudo apt install build-essential libcurl4-openssl-dev libwebsockets-dev
wget unsandbox.com/downloads/un.c && gcc -O2 -o un un.c -lcurl -lwebsockets

SDK Implementations — most use stdlib only (Ruby, JS, Go, etc). Some require minimal deps:

pip install requests  # Python

Execute Code

Run a Script

./un hello.py
./un app.js
./un main.rs

With Environment Variables

./un -e DEBUG=1 -e NAME=World script.py

With Input Files (teleport files into sandbox)

./un -f data.csv -f config.json process.py

Get Compiled Binary (teleport artifacts out)

./un -a -o ./bin main.c

Interactive Sessions

Start a Shell Session

# Default bash shell
./un session

# Choose your shell
./un session --shell zsh
./un session --shell fish

# Jump into a REPL
./un session --shell python3
./un session --shell node
./un session --shell julia

Session with Network Access

./un session -n semitrusted

Session Auditing (full terminal recording)

# Record everything (including vim, interactive programs)
./un session --audit -o ./logs

# Replay session later
zcat session.log*.gz | less -R

Collect Artifacts from Session

# Files in /tmp/artifacts/ are collected on exit
./un session -a -o ./outputs

Session Persistence (tmux/screen)

# Default: session terminates on disconnect (clean exit)
./un session

# With tmux: session persists, can reconnect later
./un session --tmux
# Press Ctrl+b then d to detach

# With screen: alternative multiplexer
./un session --screen
# Press Ctrl+a then d to detach

List Active Sessions

./un session --list

# Output:
# Active sessions: 2
#
# SESSION ID                               CONTAINER            SHELL      TTL      STATUS
# abc123...                                unsb-vm-12345        python3    45m30s   active
# def456...                                unsb-vm-67890        bash       1h2m     active

Reconnect to Existing Session

# Reconnect by container name (requires --tmux or --screen)
./un session --attach unsb-vm-12345

# Use exit to terminate session, or detach to keep it running

Terminate a Session

./un session --kill unsb-vm-12345

Available Shells & REPLs

Shells: bash, dash, sh, zsh, fish, ksh, tcsh, csh, elvish, xonsh, ash

REPLs:  python3, bpython, ipython    # Python
        node                          # JavaScript
        ruby, irb                     # Ruby
        lua                           # Lua
        php                           # PHP
        perl                          # Perl
        guile, scheme                 # Scheme
        ghci                          # Haskell
        erl, iex                      # Erlang/Elixir
        sbcl, clisp                   # Common Lisp
        r                             # R
        julia                         # Julia
        clojure                       # Clojure

API Key Management

Check Key Status

# Check if your API key is valid
./un key

# Output:
# Valid: key expires in 30 days

Extend Expired Key

# Open the portal to extend an expired key
./un key --extend

# This opens the unsandbox.com portal where you can
# add more credits to extend your key's expiration

Authentication

Credentials are loaded in priority order (highest first):

# 1. CLI flags (highest priority)
./un -p unsb-pk-xxxx -k unsb-sk-xxxxx script.py

# 2. Environment variables
export UNSANDBOX_PUBLIC_KEY=unsb-pk-xxxx-xxxx-xxxx-xxxx
export UNSANDBOX_SECRET_KEY=unsb-sk-xxxxx-xxxxx-xxxxx-xxxxx
./un script.py

# 3. Config file (lowest priority)
# ~/.unsandbox/accounts.csv format: public_key,secret_key
mkdir -p ~/.unsandbox
echo "unsb-pk-xxxx-xxxx-xxxx-xxxx,unsb-sk-xxxxx-xxxxx-xxxxx-xxxxx" > ~/.unsandbox/accounts.csv
./un script.py

Requests are signed with HMAC-SHA256. The bearer token contains only the public key; the secret key computes the signature (never transmitted).

Resource Scaling

Set vCPU Count

# Default: 1 vCPU, 2GB RAM
./un script.py

# Scale up: 4 vCPUs, 8GB RAM
./un -v 4 script.py

# Maximum: 8 vCPUs, 16GB RAM
./un --vcpu 8 heavy_compute.py

Live Session Boosting

# Boost a running session to 2 vCPU, 4GB RAM
./un session --boost sandbox-abc

# Boost to specific vCPU count (4 vCPU, 8GB RAM)
./un session --boost sandbox-abc --boost-vcpu 4

# Return to base resources (1 vCPU, 2GB RAM)
./un session --unboost sandbox-abc

Session Freeze/Unfreeze

Freeze and Unfreeze Sessions

# Freeze a session (stop billing, preserve state)
./un session --freeze sandbox-abc

# Unfreeze a frozen session
./un session --unfreeze sandbox-abc

# Note: Requires --tmux or --screen for persistence

Persistent Services

Create a Service

# Web server with ports
./un service --name web --ports 80,443 --bootstrap "python -m http.server 80"

# With custom domains
./un service --name blog --ports 8000 --domains blog.example.com

# Game server with SRV records
./un service --name mc --type minecraft --bootstrap ./setup.sh

# Deploy app tarball with bootstrap script
./un service --name app --ports 8000 -f app.tar.gz --bootstrap-file ./setup.sh
# setup.sh: cd /tmp && tar xzf app.tar.gz && ./app/start.sh

Manage Services

# List all services
./un service --list

# Get service details
./un service --info abc123

# View bootstrap logs
./un service --logs abc123
./un service --tail abc123  # last 9000 lines

# Execute command in running service
./un service --execute abc123 'journalctl -u myapp -n 50'

# Dump bootstrap script (for migrations)
./un service --dump-bootstrap abc123
./un service --dump-bootstrap abc123 backup.sh

# Freeze/unfreeze service
./un service --freeze abc123
./un service --unfreeze abc123

# Service settings (auto-wake, freeze page display)
./un service --auto-unfreeze abc123      # enable auto-wake on HTTP
./un service --no-auto-unfreeze abc123   # disable auto-wake
./un service --show-freeze-page abc123   # show HTML payment page (default)
./un service --no-show-freeze-page abc123  # return JSON error instead

# Redeploy with new bootstrap
./un service --redeploy abc123 --bootstrap ./new-setup.sh

# Destroy service
./un service --destroy abc123

Snapshots

List Snapshots

./un snapshot --list

# Output:
# Snapshots: 3
#
# SNAPSHOT ID                              NAME             SOURCE     SIZE     CREATED
# unsb-snapshot-a1b2-c3d4-e5f6-g7h8        before-upgrade   session    512 MB   2h ago
# unsb-snapshot-i9j0-k1l2-m3n4-o5p6        stable-v1.0      service    1.2 GB   1d ago

Create Session Snapshot

# Snapshot with name
./un session --snapshot unsb-vm-12345 --name "before upgrade"

# Quick snapshot (auto-generated name)
./un session --snapshot unsb-vm-12345

Create Service Snapshot

# Standard snapshot (pauses container briefly)
./un service --snapshot unsb-service-abc123 --name "stable v1.0"

# Hot snapshot (no pause, may be inconsistent)
./un service --snapshot unsb-service-abc123 --hot

Restore from Snapshot

# Restore session from snapshot
./un session --restore unsb-snapshot-a1b2-c3d4-e5f6-g7h8

# Restore service from snapshot
./un service --restore unsb-snapshot-i9j0-k1l2-m3n4-o5p6

Delete Snapshot

./un snapshot --delete unsb-snapshot-a1b2-c3d4-e5f6-g7h8

Images

Images are independent, transferable container images that survive container deletion. Unlike snapshots (which live with their container), images can be shared with other users, transferred between API keys, or made public in the marketplace.

List Images

# List all images (owned + shared + public)
./un image --list

# List only your images
./un image --list owned

# List images shared with you
./un image --list shared

# List public marketplace images
./un image --list public

# Get image details
./un image --info unsb-image-xxxx-xxxx-xxxx-xxxx

Publish Images

# Publish from a stopped or frozen service
./un image --publish-service unsb-service-abc123 \
   --name "My App v1.0" --description "Production snapshot"

# Publish from a snapshot
./un image --publish-snapshot unsb-snapshot-xxxx-xxxx-xxxx-xxxx \
   --name "Stable Release"

# Note: Cannot publish from running containers - stop or freeze first

Create Services from Images

# Spawn a new service from an image
./un image --spawn unsb-image-xxxx-xxxx-xxxx-xxxx \
   --name new-service --ports 80,443

# Clone an image (creates a copy you own)
./un image --clone unsb-image-xxxx-xxxx-xxxx-xxxx

Image Protection

# Lock image to prevent accidental deletion
./un image --lock unsb-image-xxxx-xxxx-xxxx-xxxx

# Unlock image to allow deletion
./un image --unlock unsb-image-xxxx-xxxx-xxxx-xxxx

# Delete image (must be unlocked)
./un image --delete unsb-image-xxxx-xxxx-xxxx-xxxx

Visibility & Sharing

# Set visibility level
./un image --visibility unsb-image-xxxx-xxxx-xxxx-xxxx private   # owner only (default)
./un image --visibility unsb-image-xxxx-xxxx-xxxx-xxxx unlisted  # can be shared
./un image --visibility unsb-image-xxxx-xxxx-xxxx-xxxx public    # marketplace

# Share with specific user
./un image --grant unsb-image-xxxx-xxxx-xxxx-xxxx \
   --key unsb-pk-friend-friend-friend-friend

# Revoke access
./un image --revoke unsb-image-xxxx-xxxx-xxxx-xxxx \
   --key unsb-pk-friend-friend-friend-friend

# List who has access
./un image --trusted unsb-image-xxxx-xxxx-xxxx-xxxx

Transfer Ownership

# Transfer image to another API key
./un image --transfer unsb-image-xxxx-xxxx-xxxx-xxxx \
   --to unsb-pk-newowner-newowner-newowner-newowner

Usage Reference

Usage: ./un [options] <source_file>
       ./un session [options]
       ./un service [options]
       ./un snapshot [options]
       ./un image [options]
       ./un key

Commands:
  (default)        Execute source file in sandbox
  session          Open interactive shell/REPL session
  service          Manage persistent services
  snapshot         Manage container snapshots
  image            Manage container images (publish, share, transfer)
  key              Check API key validity and expiration

Options:
  -e KEY=VALUE     Set environment variable (can use multiple times)
  -f FILE          Add input file (can use multiple times)
  -a               Return and save artifacts from /tmp/artifacts/
  -o DIR           Output directory for artifacts (default: current dir)
  -p KEY           Public key (or set UNSANDBOX_PUBLIC_KEY env var)
  -k KEY           Secret key (or set UNSANDBOX_SECRET_KEY env var)
  -n MODE          Network mode: zerotrust (default) or semitrusted
  -v N, --vcpu N   vCPU count 1-8, each vCPU gets 2GB RAM (default: 1)
  -y               Skip confirmation for large uploads (>1GB)
  -h               Show this help

Authentication (priority order):
  1. -p and -k flags (public and secret key)
  2. UNSANDBOX_PUBLIC_KEY + UNSANDBOX_SECRET_KEY env vars
  3. ~/.unsandbox/accounts.csv (format: public_key,secret_key per line)

Session options:
  -s, --shell SHELL  Shell/REPL to use (default: bash)
  -l, --list         List active sessions
  --attach ID        Reconnect to existing session (ID or container name)
  --kill ID          Terminate a session (ID or container name)
  --freeze ID        Freeze a session (requires --tmux/--screen)
  --unfreeze ID      Unfreeze a frozen session
  --boost ID         Boost session resources (2 vCPU, 4GB RAM)
  --boost-vcpu N     Specify vCPU count for boost (1-8)
  --unboost ID       Return to base resources
  --audit            Record full session for auditing
  --tmux             Enable session persistence with tmux (allows reconnect)
  --screen           Enable session persistence with screen (allows reconnect)

Service options:
  --name NAME        Service name (creates new service)
  --ports PORTS      Comma-separated ports (e.g., 80,443)
  --domains DOMAINS  Custom domains (e.g., example.com,www.example.com)
  --type TYPE        Service type: minecraft, mumble, teamspeak, source, tcp, udp
  --bootstrap CMD    Bootstrap command/file/URL to run on startup
  -f FILE            Upload file to /tmp/ (can use multiple times)
  -l, --list         List all services
  --info ID          Get service details
  --tail ID          Get last 9000 lines of bootstrap logs
  --logs ID          Get all bootstrap logs
  --freeze ID        Freeze a service
  --unfreeze ID      Unfreeze a service
  --auto-unfreeze ID       Enable auto-wake on HTTP request
  --no-auto-unfreeze ID    Disable auto-wake on HTTP request
  --show-freeze-page ID    Show HTML payment page when frozen (default)
  --no-show-freeze-page ID Return JSON error when frozen
  --destroy ID       Destroy a service
  --redeploy ID      Re-run bootstrap script (requires --bootstrap)
  --execute ID CMD   Run a command in a running service
  --dump-bootstrap ID [FILE]  Dump bootstrap script (for migrations)
  --snapshot ID    Create snapshot of session or service
  --snapshot-name  User-friendly name for snapshot
  --hot            Create snapshot without pausing (may be inconsistent)
  --restore ID     Restore session/service from snapshot ID

Snapshot options:
  -l, --list       List all snapshots
  --info ID        Get snapshot details
  --delete ID      Delete a snapshot permanently

Image options:
  -l, --list [owned|shared|public]  List images (all, owned, shared, or public)
  --info ID        Get image details
  --publish-service ID   Publish image from stopped/frozen service
  --publish-snapshot ID  Publish image from snapshot
  --name NAME      Name for published image
  --description DESC  Description for published image
  --delete ID      Delete image (must be unlocked)
  --clone ID       Clone image (creates copy you own)
  --spawn ID       Create service from image (requires --name)
  --lock ID        Lock image to prevent deletion
  --unlock ID      Unlock image to allow deletion
  --visibility ID LEVEL  Set visibility (private|unlisted|public)
  --grant ID --key KEY   Grant access to another API key
  --revoke ID --key KEY  Revoke access from API key
  --transfer ID --to KEY Transfer ownership to API key
  --trusted ID     List API keys with access

Key options:
  (no options)       Check API key validity
  --extend           Open portal to extend an expired key

Examples:
  ./un script.py                       # execute Python script
  ./un -e DEBUG=1 script.py            # with environment variable
  ./un -f data.csv process.py          # with input file
  ./un -a -o ./bin main.c              # save compiled artifacts
  ./un -v 4 heavy.py                   # with 4 vCPUs, 8GB RAM
  ./un session                         # interactive bash session
  ./un session --tmux                  # bash with reconnect support
  ./un session --list                  # list active sessions
  ./un session --attach unsb-vm-12345  # reconnect to session
  ./un session --kill unsb-vm-12345    # terminate a session
  ./un session --freeze unsb-vm-12345  # freeze session
  ./un session --unfreeze unsb-vm-12345  # unfreeze session
  ./un session --boost unsb-vm-12345   # boost resources
  ./un session --unboost unsb-vm-12345 # return to base
  ./un session --shell python3         # Python REPL
  ./un session --shell node            # Node.js REPL
  ./un session -n semitrusted          # session with network access
  ./un session --audit -o ./logs       # record session for auditing
  ./un service --name web --ports 80   # create web service
  ./un service --list                  # list all services
  ./un service --logs abc123           # view bootstrap logs
  ./un key                             # check API key
  ./un key --extend                    # extend expired key
  ./un snapshot --list                 # list all snapshots
  ./un session --snapshot unsb-vm-123  # snapshot a session
  ./un service --snapshot abc123       # snapshot a service
  ./un session --restore unsb-snapshot-xxxx  # restore from snapshot
  ./un image --list                  # list all images
  ./un image --list owned            # list your images
  ./un image --publish-service abc   # publish image from service
  ./un image --spawn img123 --name x # create service from image
  ./un image --grant img --key pk    # share image with user

CLI Inception

The UN CLI has been implemented in 42 programming languages, demonstrating that the unsandbox API can be accessed from virtually any environment.

View All 42 Implementations →

License

PUBLIC DOMAIN - NO LICENSE, NO WARRANTY

This is free public domain software for the public good of a permacomputer hosted
at permacomputer.com - an always-on computer by the people, for the people. One
that is durable, easy to repair, and distributed like tap water for machine
learning intelligence.

The permacomputer is community-owned infrastructure optimized around four values:

  TRUTH    - First principles, math & science, open source code freely distributed
  FREEDOM  - Voluntary partnerships, freedom from tyranny & corporate control
  HARMONY  - Minimal waste, self-renewing systems with diverse thriving connections
  LOVE     - Be yourself without hurting others, cooperation through natural law

This software contributes to that vision by enabling code execution across all 42
programming languages through a unified interface, accessible to everyone. Code is
seeds to sprout on any abandoned technology.

Learn more: https://www.permacomputer.com

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.

NO WARRANTY. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.

That said, our permacomputer's digital membrane stratum continuously runs unit,
integration, and functional tests on all its own software - with our permacomputer
monitoring itself, repairing itself, with minimal human guidance in the loop.
Our agents do their best.

Copyright 2025 TimeHexOn & foxhop & russell@unturf
https://www.timehexon.com
https://www.foxhop.net
https://www.unturf.com/software

Export Vault

Enter a password to encrypt your exported vault. You'll need this password to import the vault on another device.

Import Vault

Select an exported vault file and enter the export password to decrypt it.