SDK Reference
Go SDK
Introduction

Go SDK

This is the Hatchet Go SDK reference. On this page, we'll get you up and running with a Go worker. This guide assumes that you already have a Hatchet engine instance running. If you don't, you can:

🪓

If you run into any issues, please file an issue on the Hatchet GitHub repository (opens in a new tab).

Installation

Run the following to install the Hatchet Go SDK:

go get github.com/hatchet-dev/hatchet/pkg

Generate a Token

Navigate to your Hatchet dashboard and navigate to your settings tab. You should see a section called "API Keys". Click "Create API Key", input a name for the key and copy the key. Then set the following environment variables:

HATCHET_CLIENT_TOKEN="<your-api-key>"
🪓

You may need to set additional environment variables depending on your self-hosted configuration. The Hatchet clients default to SSL by default, but to disable this you can set:

HATCHET_CLIENT_TLS_STRATEGY=none

Run your first worker

Create a main.go file with the following contents:

main.go
package main
 
import (
	"fmt"
 
	"github.com/hatchet-dev/hatchet/pkg/client"
	"github.com/hatchet-dev/hatchet/pkg/cmdutils"
	"github.com/hatchet-dev/hatchet/pkg/worker"
)
 
type stepOutput struct{}
 
func main() {
	c, err := client.New()
 
	if err != nil {
		panic(fmt.Sprintf("error creating client: %v", err))
	}
 
	w, err := worker.NewWorker(
		worker.WithClient(
			c,
		),
		worker.WithMaxRuns(1),
	)
 
	if err != nil {
		panic(fmt.Sprintf("error creating worker: %v", err))
	}
 
	err = w.RegisterWorkflow(
		&worker.WorkflowJob{
			Name:        "simple-workflow",
			Description: "Simple one-step workflow.",
			On:          worker.Events("simple"),
			Steps: []*worker.WorkflowStep{
				worker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {
					fmt.Println("executed step 1")
 
					return &stepOutput{}, nil
				},
				).SetName("step-one"),
			},
		},
	)
 
	if err != nil {
		panic(fmt.Sprintf("error registering workflow: %v", err))
	}
 
	interruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())
	defer cancel()
 
	cleanup, err := w.Start()
	if err != nil {
		panic(fmt.Sprintf("error starting worker: %v", err))
	}
 
	<-interruptCtx.Done()
	if err := cleanup(); err != nil {
		panic(err)
	}
}

Next, run the following command to start the worker:

go run main.go

Run your first workflow

The worker is now running and listening for steps to execute. You should see your first worker registered in the Workers tab of the Hatchet dashboard:

Quickstart 1

You can now trigger your first workflow by navigating to the Workflows tab, selecting your workflow, and clicking the top right "Trigger workflow" button:

Quickstart 2

That's it! You've successfully deployed Hatchet and run your first workflow.

Next Steps

Congratulations on running your first workflow!

To test out more complex workflows, check out the Hatchet Go Quickstart (opens in a new tab).