Skip to main content

Requirements

  • Go 1.21 or higher

Installation

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

Setup

1

Get an API token

Obtain your API token from the Hatchet Cloud dashboard or your self-hosted Hatchet instance.The token is a JWT that embeds your tenant ID and server addresses so the SDK can read them automatically.
2

Set environment variables

Export your token so the SDK picks it up automatically:
export HATCHET_CLIENT_TOKEN="<your-api-token>"
For self-hosted deployments, also set the host and port:
export HATCHET_CLIENT_TOKEN="<your-api-token>"
export HATCHET_CLIENT_HOST_PORT="localhost:7070"
3

Initialize the client

Import the SDK and call hatchet.NewClient(). The client reads HATCHET_CLIENT_TOKEN from the environment automatically:
main.go
package main

import (
    "log"

    hatchet "github.com/hatchet-dev/hatchet/sdks/go"
)

func main() {
    client, err := hatchet.NewClient()
    if err != nil {
        log.Fatal(err)
    }

    _ = client
}
4

Define and run your first task

main.go
package main

import (
    "context"
    "fmt"
    "log"

    hatchet "github.com/hatchet-dev/hatchet/sdks/go"
)

type GreetInput struct {
    Name string `json:"name"`
}

type GreetOutput struct {
    Message string `json:"message"`
}

func main() {
    client, err := hatchet.NewClient()
    if err != nil {
        log.Fatal(err)
    }

    greet := client.NewStandaloneTask(
        "greet",
        func(ctx hatchet.Context, input GreetInput) (GreetOutput, error) {
            return GreetOutput{Message: "Hello, " + input.Name + "!"}, nil
        },
    )

    result, err := greet.Run(context.Background(), GreetInput{Name: "World"})
    if err != nil {
        log.Fatal(err)
    }

    var output GreetOutput
    if err := result.Into(&output); err != nil {
        log.Fatal(err)
    }

    fmt.Println(output.Message) // Hello, World!
}

Environment variable reference

VariableDescriptionDefault
HATCHET_CLIENT_TOKENAPI token (required)
HATCHET_CLIENT_HOST_PORTgRPC server host and portRead from token
HATCHET_CLIENT_NAMESPACENamespace prefix for workflow and event names""
The SDK validates that HATCHET_CLIENT_TOKEN is a valid JWT. Tokens from the dashboard start with ey — if you see an error about the token format, make sure you are copying the full token value.

Build docs developers (and LLMs) love