Go SDK Quickstart
Back to SDK Quickstart
This guide covers the Go SDK implementation. For other languages or general information, see the SDK Quickstart page.
Prerequisites
- Go 1.21 or later
- Your OpenTDF platform running locally (from Getting Started guide)
Platform Must Be Running
Before you begin, make sure your OpenTDF platform is running!
Verify it's running:
curl -k https://platform.opentdf.local:8443/healthz
Should return: {"status":"SERVING"}
If not running, start it:
cd ~/.opentdf/platform && docker compose up -d
See the Managing the Platform guide for details.
Step 1: Create a New Project
Create a new directory and initialize a Go module:
mkdir opentdf-quickstart
cd opentdf-quickstart
go mod init opentdf-quickstart
Step 2: Install the SDK
go get github.com/opentdf/platform/sdk@latest
Expected output:
go: downloading github.com/opentdf/platform/sdk v0.x.x
go: added github.com/opentdf/platform/sdk v0.x.x
Step 3: Create Your Application
Go Implementation Code
Create a file named main.go:
main.go
package main
import (
"bytes"
"log"
"strings"
"github.com/opentdf/platform/sdk"
)
func main() {
log.Println("🚀 Starting OpenTDF SDK Quickstart...")
platformEndpoint := "https://platform.opentdf.local:8443"
log.Printf("📡 Connecting to platform: %s", platformEndpoint)
// Create a new SDK client with client credentials
log.Println("🔐 Initializing SDK client with client credentials...")
client, err := sdk.New(
platformEndpoint,
sdk.WithClientCredentials("opentdf", "secret", nil),
// WithInsecureSkipVerifyConn() disables TLS certificate verification
// This allows connections to the platform's self-signed certificate
// Only use this for local development - never in production!
sdk.WithInsecureSkipVerifyConn(),
)
if err != nil {
log.Fatalf("❌ Client initialization failed: %v", err)
}
log.Println("✅ SDK client initialized successfully")
// Encrypt data
log.Println("\n📝 Encrypting sensitive data...")
sensitiveData := "Hello from the OpenTDF Go SDK! This data is encrypted."
dataReader := strings.NewReader(sensitiveData)
encryptedBuffer := &bytes.Buffer{}
log.Println("🔒 Creating TDF...")
_, err = client.CreateTDF(
encryptedBuffer,
dataReader,
// KASInfo specifies the Key Access Service (KAS) endpoint
// KAS manages encryption keys and enforces access policies
sdk.WithKasInformation(
sdk.KASInfo{
URL: platformEndpoint,
},
),
)
if err != nil {
log.Fatalf("❌ Encryption failed: %v", err)
}
log.Println("✅ Data successfully encrypted")
log.Printf("📊 Encrypted TDF size: %d bytes", encryptedBuffer.Len())
// Decrypt data
log.Println("\n🔓 Decrypting TDF...")
tdfReader, err := client.LoadTDF(bytes.NewReader(encryptedBuffer.Bytes()))
if err != nil {
log.Fatalf("❌ Decryption failed: %v", err)
}
var decryptedBuffer bytes.Buffer
if _, err = tdfReader.WriteTo(&decryptedBuffer); err != nil {
log.Fatalf("❌ Failed to read decrypted data: %v", err)
}
log.Println("✅ Data successfully decrypted")
log.Printf("📤 Decrypted content:\n\n%s\n", decryptedBuffer.String())
log.Println("\n🎉 Quickstart complete!")
}