Using Lamatic SDK with Go
This guide demonstrates how to integrate the Lamatic Go SDK with your Go applications to execute flows and agents.
This is a community-built Go SDK for the Lamatic AI platform. The SDK is maintained under the Lamatic organization (opens in a new tab) on GitHub.
Installation
Install the Lamatic Go SDK using Go modules:
go get github.com/Lamatic/lamatic-sdk-goOr if you're using the community version:
go get github.com/AasheeshLikePanner/lamatic-sdk-goGetting Started
To use the Lamatic Go SDK, you'll need:
- An API key or access token
- Your project ID
- The Lamatic API endpoint
Basic Setup
Import the SDK in your Go code:
import (
"context"
lamatic "github.com/Lamatic/lamatic-sdk-go/pkg/lamatic"
)Authentication
The SDK supports two authentication methods:
package main
import (
"context"
lamatic "github.com/Lamatic/lamatic-sdk-go/pkg/lamatic"
)
func main() {
apiKey := "your-api-key"
client, err := lamatic.NewClient(lamatic.Config{
Endpoint: "your-endpoint",
ProjectID: "your-project-id",
APIKey: &apiKey,
})
if err != nil {
panic(err)
}
// Use client...
}Executing Flows
Execute a workflow using the ExecuteFlow method:
package main
import (
"context"
"fmt"
"log"
lamatic "github.com/Lamatic/lamatic-sdk-go/pkg/lamatic"
)
func main() {
ctx := context.Background()
apiKey := "your-api-key"
client, err := lamatic.NewClient(lamatic.Config{
Endpoint: "your-endpoint",
ProjectID: "your-project-id",
APIKey: &apiKey,
})
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
flowId := "your-flow-id"
payload := map[string]interface{}{
"prompt": "Hello, AI!",
}
resp, err := client.ExecuteFlow(ctx, flowId, payload)
if err != nil {
log.Fatalf("Error executing flow: %v", err)
}
if resp.Status == lamatic.StatusSuccess {
fmt.Printf("Result: %v\n", resp.Result)
} else {
fmt.Printf("Failed: %s\n", resp.Message)
}
}Checking Request Status
For long-running workflows or agents, you can check the status using CheckStatus:
package main
import (
"context"
"fmt"
"log"
"time"
lamatic "github.com/Lamatic/lamatic-sdk-go/pkg/lamatic"
)
func main() {
ctx := context.Background()
apiKey := "your-api-key"
client, err := lamatic.NewClient(lamatic.Config{
Endpoint: "your-endpoint",
ProjectID: "your-project-id",
APIKey: &apiKey,
})
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
requestId := "your-request-id"
// Check status with polling interval of 2 seconds and timeout of 30 seconds
status, err := client.CheckStatus(ctx, requestId, 2*time.Second, 30*time.Second)
if err != nil {
log.Fatalf("Error checking status: %v", err)
}
if status.Status == lamatic.StatusSuccess {
fmt.Printf("Request completed: %+v\n", status.Result)
} else {
fmt.Printf("Request failed: %s\n", status.Message)
}
}Updating Access Token
If you're using access token authentication and need to update the token:
// Update the access token
newToken := "new-access-token"
err := client.UpdateAccessToken(newToken)
if err != nil {
log.Fatalf("Failed to update access token: %v", err)
}Response Structure
The ExecuteFlow and ExecuteAgent methods return a response with the following structure:
type Response struct {
Status string // "success", "error", or "failed"
Result interface{} // The result data from the workflow/agent (if successful)
Message string // Error message (if status is "error" or "failed")
}Error Handling
Always handle errors properly:
resp, err := client.ExecuteFlow(ctx, flowId, payload)
if err != nil {
// Handle network errors, configuration errors, etc.
log.Fatalf("Error: %v", err)
}
// Check response status
if resp.Status == lamatic.StatusSuccess {
// Handle success
fmt.Printf("Success: %v\n", resp.Result)
} else if resp.Status == lamatic.StatusError || resp.Status == lamatic.StatusFailed {
// Handle execution errors
fmt.Printf("Execution failed: %s\n", resp.Message)
}Environment Variables
For better security, use environment variables:
package main
import (
"context"
"log"
"os"
lamatic "github.com/Lamatic/lamatic-sdk-go/pkg/lamatic"
)
func main() {
apiKey := os.Getenv("LAMATIC_API_KEY")
projectID := os.Getenv("LAMATIC_PROJECT_ID")
endpoint := os.Getenv("LAMATIC_ENDPOINT")
client, err := lamatic.NewClient(lamatic.Config{
Endpoint: endpoint,
ProjectID: projectID,
APIKey: &apiKey,
})
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Use client...
}Complete Example
Here's a complete example that demonstrates executing a flow with proper error handling:
package main
import (
"context"
"fmt"
"log"
"os"
lamatic "github.com/Lamatic/lamatic-sdk-go/pkg/lamatic"
)
func main() {
ctx := context.Background()
// Get credentials from environment variables
apiKey := os.Getenv("LAMATIC_API_KEY")
if apiKey == "" {
log.Fatal("LAMATIC_API_KEY environment variable is not set")
}
projectID := os.Getenv("LAMATIC_PROJECT_ID")
if projectID == "" {
log.Fatal("LAMATIC_PROJECT_ID environment variable is not set")
}
endpoint := os.Getenv("LAMATIC_ENDPOINT")
if endpoint == "" {
log.Fatal("LAMATIC_ENDPOINT environment variable is not set")
}
// Create client
client, err := lamatic.NewClient(lamatic.Config{
Endpoint: endpoint,
ProjectID: projectID,
APIKey: &apiKey,
})
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Execute flow
flowId := os.Getenv("LAMATIC_FLOW_ID")
payload := map[string]interface{}{
"prompt": "Generate a creative story about AI",
}
resp, err := client.ExecuteFlow(ctx, flowId, payload)
if err != nil {
log.Fatalf("Error executing flow: %v", err)
}
// Handle response
if resp.Status == lamatic.StatusSuccess {
fmt.Printf("Flow executed successfully!\n")
fmt.Printf("Result: %+v\n", resp.Result)
} else {
fmt.Printf("Flow execution failed: %s\n", resp.Message)
}
}Project Structure
The Go SDK follows standard Go project structure:
lamatic-sdk-go/
βββ go.mod
βββ README.md
βββ LICENSE
βββ internal/
β βββ client/
β βββ client.go # Internal HTTP client
βββ pkg/
β βββ lamatic/
β βββ lamatic.go # Public SDK API
β βββ lamatic_test.go # Unit tests
βββ examples/
βββ api_key/
β βββ main.go # Example: API Key auth
βββ access_token/
β βββ main.go # Example: Access Token auth
βββ access_token_expiry/
β βββ main.go # Example: Handling token expiry
βββ token_generation/
βββ main.go # Example: Server-side JWT generationFeatures
The Go SDK provides the following features:
- β Execute Workflows (Flows)
- β Execute Agents
- β
Job Polling with
CheckStatus - β Support for API Key and Access Token (JWT) authentication
- β Proper internal/pkg separation for Go best practices
- β Context support for cancellation and timeouts
Resources
- GitHub Repository: github.com/Lamatic/lamatic-sdk-go (opens in a new tab)
- Documentation: Check the repository README for the latest updates
- Examples: See the
examples/directory in the repository for more usage examples
Support
For help and support with the Go SDK:
- Open an issue on GitHub (opens in a new tab)
- Contact [email protected]