Skip to content

Petal Flow Getting Started

This guide walks you through building your first Petal Flow workflow graph.

  • Go 1.21 or later
  • A Go module initialized (go mod init)
Terminal window
go get github.com/petal-labs/petalflow
  1. Create a graph — Graphs are containers for nodes and edges.
  2. Add nodes — Nodes perform work on the envelope data.
  3. Set the entry point — Define where execution begins.
  4. Run with the runtime — Execute the graph and get results.
package main
import (
"context"
"fmt"
"strings"
"github.com/petal-labs/petalflow"
)
func main() {
// Create a new graph with a name
g := petalflow.NewGraph("hello")
// Create a function node that transforms input
node := petalflow.NewFuncNode("greet", func(ctx context.Context, env *petalflow.Envelope) (*petalflow.Envelope, error) {
name := env.GetVarString("name")
env.SetVar("greeting", fmt.Sprintf("Hello, %s!", strings.ToUpper(name)))
return env, nil
})
// Add the node and set it as the entry point
g.AddNode(node)
g.SetEntry("greet")
// Create an envelope with initial data
env := petalflow.NewEnvelope().WithVar("name", "world")
// Run the graph
runtime := petalflow.NewRuntime()
result, err := runtime.Run(context.Background(), g, env, petalflow.DefaultRunOptions())
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println(result.GetVarString("greeting"))
// Output: Hello, WORLD!
}