Skip to main content

Initializing the client

Use hatchet.NewClient() to create a client instance. It reads HATCHET_CLIENT_TOKEN from the environment automatically:
hatchet-client.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
}
You can pass ClientOpt functions to override specific settings:
import (
    hatchet "github.com/hatchet-dev/hatchet/sdks/go"
    v0Client "github.com/hatchet-dev/hatchet/pkg/client"
)

client, err := hatchet.NewClient(
    v0Client.WithToken("ey..."),
    v0Client.WithHostPort("localhost", 7070),
    v0Client.WithNamespace("my-app"),
)

Signature

func NewClient(opts ...v0Client.ClientOpt) (*Client, error)

ClientOpt functions

WithToken(token string)
ClientOpt
Sets the API token directly, bypassing the HATCHET_CLIENT_TOKEN environment variable.
WithHostPort(host string, port int)
ClientOpt
Sets the gRPC server address, e.g. WithHostPort("localhost", 7070). Overrides the address embedded in the token.
WithNamespace(namespace string)
ClientOpt
Applies a namespace prefix to all workflow names and event keys. Can also be set via HATCHET_CLIENT_NAMESPACE.
WithLogLevel(lvl string)
ClientOpt
Sets the log level for the SDK’s internal logger. Accepts any level string recognized by zerolog.
WithTenantId(tenantId string)
ClientOpt
Overrides the tenant ID extracted from the JWT.
WithSharedMeta(meta map[string]string)
ClientOpt
Attaches key-value metadata to every workflow run triggered by this client.
WithGRPCHeaders(headers map[string]string)
ClientOpt
Adds custom headers to every outgoing gRPC request.

Feature clients

All feature clients are lazy-initialized properties on *Client. Each wraps a specific area of the Hatchet API.
MethodTypeDescription
client.Metrics()*MetricsClientWorkflow and queue metrics.
client.RateLimits()*RateLimitsClientManage rate limit configurations.
client.Crons()*CronsClientCreate and manage cron workflow triggers.
client.Schedules()*SchedulesClientCreate and manage one-off scheduled runs.
client.Filters()*FiltersClientCreate and manage event filters.
client.Runs()*RunsClientGet, list, cancel, replay, and stream workflow runs.
client.Workers()*WorkersClientList and inspect registered workers.
client.Workflows()*WorkflowsClientList and get workflow definitions.
client.Logs()*LogsClientAccess task run log lines.
client.Webhooks()*WebhooksClientCreate and manage webhook endpoints.
client.CEL()*CELClientEvaluate CEL expressions.
client.Events()EventClientPush events that trigger event-based workflows.

RunsClient

// Get a workflow run by ID
run, err := client.Runs().Get(ctx, runId)

// Get just the status
status, err := client.Runs().GetStatus(ctx, runId)

// List runs with filters
list, err := client.Runs().List(ctx, rest.V1WorkflowRunListParams{...})

// Cancel a task in a run
cancelled, err := client.Runs().Cancel(ctx, rest.V1CancelTaskRequest{...})

// Replay a task in a run
replayed, err := client.Runs().Replay(ctx, rest.V1ReplayTaskRequest{...})

// Subscribe to a streaming run
ch := client.Runs().SubscribeToStream(ctx, workflowRunId)
for msg := range ch {
    fmt.Println(msg)
}

CronsClient

// Create a cron trigger
cron, err := client.Crons().Create(ctx, "my-workflow", features.CreateCronTrigger{
    Name:       "daily-report",
    Expression: "0 9 * * *",
    Input:      map[string]interface{}{"report": "full"},
})

// List cron triggers
list, err := client.Crons().List(ctx, rest.CronWorkflowListParams{})

// Get a specific cron trigger
cron, err := client.Crons().Get(ctx, cronId)

// Delete a cron trigger
err = client.Crons().Delete(ctx, cronId)

SchedulesClient

// Schedule a one-off run
scheduled, err := client.Schedules().Create(ctx, "my-workflow", features.CreateScheduledRunTrigger{
    TriggerAt: time.Now().Add(24 * time.Hour),
    Input:     map[string]interface{}{"key": "value"},
})

// List scheduled runs
list, err := client.Schedules().List(ctx, rest.WorkflowScheduledListParams{})

// Delete a scheduled run
err = client.Schedules().Delete(ctx, scheduledRunId)

MetricsClient

// Workflow-level metrics
metrics, err := client.Metrics().GetWorkflowMetrics(ctx, "my-workflow", nil)

// Tenant queue metrics
queueMetrics, err := client.Metrics().GetQueueMetrics(ctx, nil)

// Step run queue metrics
taskMetrics, err := client.Metrics().GetTaskQueueMetrics(ctx)

Events

err := client.Events().Push(ctx, "user:created", map[string]interface{}{
    "userId": "123",
})

Error handling

NewClient and all feature client methods return a standard Go error as the second return value. Check it before using the result:
client, err := hatchet.NewClient()
if err != nil {
    log.Fatalf("failed to create client: %v", err)
}

run, err := client.Runs().Get(ctx, runId)
if err != nil {
    return fmt.Errorf("could not get run: %w", err)
}
NewClient returns an error if HATCHET_CLIENT_TOKEN is not set or if the token is not a valid JWT.

Build docs developers (and LLMs) love