Beta: This SDK is in beta. APIs may change between versions.
A Go SDK for deploying models to Baseten using Truss.
go get github.com/basetenlabs/truss-gopackage main
import (
"context"
"fmt"
"log"
"github.com/basetenlabs/truss-go"
)
func main() {
// Create client (reads BASETEN_API_KEY from environment)
client := truss.NewClient()
// Push a truss
result, err := client.Push(context.Background(), "./my-truss", "my-model")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Deployed! URL: %s\n", result.PredictURL())
}The SDK supports three authentication methods:
// 1. Environment variable (recommended)
// Set BASETEN_API_KEY in your environment
client := truss.NewClient()
// 2. Explicit API key
client := truss.NewClient(truss.WithAPIKey("your-api-key"))
// 3. From ~/.trussrc config file
client, err := truss.NewClientFromConfig("baseten")The ~/.trussrc file uses INI format:
[baseten]
remote_provider = baseten
api_key = your-api-key
remote_url = https://app.baseten.coresult, err := client.Push(ctx, "./my-truss", "my-model",
// Create a production deployment (default is development)
truss.Publish(),
// Promote to production immediately
truss.Promote(),
// Deploy to a specific environment
truss.WithEnvironment("production"),
// Set a custom deployment name
truss.WithDeploymentName("v2.0-release"),
// Assign to a team
truss.WithTeam("ml-team"),
// Include git commit info in deployment metadata
truss.WithGitInfo(),
// Keep previous production deployment's autoscaling
truss.PreservePreviousProduction(),
// Use existing environment's instance type
truss.PreserveEnvInstanceType(),
// Set backend deployment timeout
truss.WithDeployTimeout(30 * time.Minute),
// Allow downloading truss from Baseten UI
truss.AllowTrussDownload(),
)result, err := client.Push(ctx, trussDir, modelName)
if err != nil {
log.Fatal(err)
}
// Get deployment info
fmt.Println("Model ID:", result.ModelID)
fmt.Println("Version ID:", result.VersionID)
fmt.Println("Predict URL:", result.PredictURL())
fmt.Println("Logs URL:", result.LogsURL(client.BaseURL()))
fmt.Println("Is Draft:", result.IsDraft)
// Wait for deployment to be ready
err = result.WaitReady(ctx, 10*time.Minute)
if err != nil {
log.Fatal(err)
}
// Or check status manually
status, err := result.Status(ctx)
// status is one of: StatusBuilding, StatusDeploying, StatusActive, StatusFailed// Get current user info
user, err := client.WhoAmI(ctx)
fmt.Println(user.Email, user.WorkspaceName)
// List all models
models, err := client.Models(ctx)
// List models for a specific team
models, err := client.Models(ctx, truss.WithTeamFilter("ml-team"))
// List teams
teams, err := client.Teams(ctx)
// Validate a truss config without deploying
result, err := client.Validate(ctx, "./my-truss")
if !result.Success {
fmt.Println("Validation errors:", result.Messages)
}The SDK provides typed errors for different failure modes:
result, err := client.Push(ctx, trussDir, modelName)
if err != nil {
switch e := err.(type) {
case *truss.AuthError:
// Invalid or missing API key
log.Fatal("Authentication failed:", e)
case *truss.ValidationError:
// Truss config validation failed
fmt.Println("Errors:", e.Errors)
fmt.Println("Warnings:", e.Warnings)
case *truss.APIError:
// Error response from Baseten API
fmt.Printf("API error (code: %s): %s\n", e.Code, e.Message)
case *truss.UploadError:
// Failed to upload truss to S3
log.Fatal("Upload failed:", e)
case *truss.ArchiveError:
// Failed to create truss archive
log.Fatal("Archive creation failed:", e)
case *truss.ConfigError:
// Problem with truss or SDK configuration
log.Fatal("Config error:", e)
default:
log.Fatal(err)
}
}The SDK respects .trussignore files in your truss directory. This file uses gitignore-style patterns to exclude files from the upload.
Default exclusions (applied even without a .trussignore file):
.git__pycache__*.pyc,*.pyo.DS_Store.env,.venv,venvnode_modules
Example .trussignore:
# Exclude test files
tests/
*_test.py
# Exclude large data files
*.csv
*.parquet
data/
# Exclude secrets
secrets/
*.key
# Run all unit tests
go test -v ./...
# Run tests with coverage
go test -cover ./...Integration tests push actual models to Baseten and require valid credentials in ~/.trussrc.
# Run integration tests (requires ~/.trussrc with valid API key)
go test -tags=integration -v ./...The integration tests will:
- Authenticate using the "baseten" remote from
~/.trussrc - Push a test truss to your workspace
- Poll until the deployment reaches
MODEL_READYstatus
- Go 1.21 or later
- A Baseten account and API key
MIT