# Go SDK

> Install the Go SDK, create a client, and find the package reference.

The Go SDK defines durable functions, sends events, and serves your functions from any `net/http` server. It also runs [Connect](/docs-markdown/durable-execution/deploying-functions/connect) workers, supports [checkpointing](/docs-markdown/durable-execution/guides-and-advanced/checkpointing), and turns `net/http` handlers into [Durable Endpoints](/docs-markdown/reference/go/durable-endpoints). Some features are TypeScript-only for now; see [feature support](/docs-markdown/sdks#feature-support).

The full API reference for every package is on [pkg.go.dev](https://pkg.go.dev/github.com/inngest/inngestgo).

## Install

```shell
go get github.com/inngest/inngestgo@latest
```

## Create a client and serve functions

Create one client for your app, define functions with it, and serve them over HTTP:

```go {{ title: "main.go" }}
package main

import (
	"context"
	"log"
	"net/http"

	"github.com/inngest/inngestgo"
	"github.com/inngest/inngestgo/step"
)

func main() {
	client, err := inngestgo.NewClient(inngestgo.ClientOpts{AppID: "my-app"})
	if err != nil {
		log.Fatal(err)
	}

	_, err = inngestgo.CreateFunction(
		client,
		inngestgo.FunctionOpts{ID: "process-task"},
		inngestgo.EventTrigger("app/task.created", nil),
		func(ctx context.Context, input inngestgo.Input[map[string]any]) (any, error) {
			return step.Run(ctx, "handle-task", func(ctx context.Context) (map[string]any, error) {
				return map[string]any{"processed": true}, nil
			})
		},
	)
	if err != nil {
		log.Fatal(err)
	}

	http.Handle("/api/inngest", client.Serve())
	log.Fatal(http.ListenAndServe(":8080", nil))
}
```

The [Go quick start](/docs-markdown/durable-execution/quick-start/go-quick-start) walks through a full example with the Dev Server. See [Durable Workflows in Go](/docs-markdown/reference/go/durable-workflows) for triggers, steps, errors, and function options.

## Durable Endpoints

[Durable Endpoints](/docs-markdown/durable-execution/durable-endpoints) (beta) run a `net/http` handler as an Inngest function, so the handler can use `step.Run` and the other step tools. Create one `stephttp` provider when the process starts:

```go {{ title: "steps.go" }}
import "github.com/inngest/inngestgo/stephttp"

// Create one provider when the process starts, and use it for every handler.
var steps = stephttp.Setup(stephttp.SetupOpts{})
```

Then wrap each handler that should be a function:

```go {{ title: "orders.go" }}
import (
	"context"
	"encoding/json"
	"net/http"

	"github.com/inngest/inngestgo/step"
	"github.com/inngest/inngestgo/stephttp"
)

// steps is the provider from stephttp.Setup, created once at startup.
func RegisterRoutes(mux *http.ServeMux, steps stephttp.Provider) {
	mux.HandleFunc("POST /api/orders", steps.HandleFunc(stephttp.FnOpts{}, handleOrder))
}

func handleOrder(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	var body struct {
		OrderID string `json:"orderId"`
	}
	_ = json.NewDecoder(r.Body).Decode(&body)

	if body.OrderID == "" {
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusBadRequest)
		_ = json.NewEncoder(w).Encode(map[string]string{"error": "orderId is required"})
		return
	}

	order, err := step.Run(ctx, "load-order", func(ctx context.Context) (Order, error) {
		return loadOrder(ctx, body.OrderID)
	})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	reservation, err := step.Run(ctx, "reserve-inventory", func(ctx context.Context) (Reservation, error) {
		return reserveInventory(ctx, order)
	})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	_ = json.NewEncoder(w).Encode(map[string]any{
		"orderId":     body.OrderID,
		"reservation": reservation,
	})
}
```

Durable Endpoints need `inngestgo` v0.16.5 or later. See [Durable Endpoints in Go](/docs-markdown/reference/go/durable-endpoints) to wait for runs at shutdown, add middleware to a router, wrap handlers you can't register yourself, and configure functions.

## Packages

| Package                                                                                      | Use it for                                                                                        |
| -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [`inngestgo`](https://pkg.go.dev/github.com/inngest/inngestgo)                               | The client, functions, triggers, events, `Serve()`, and `Connect()`                               |
| [`inngestgo/step`](https://pkg.go.dev/github.com/inngest/inngestgo/step)                     | Step tools such as `step.Run`, `step.Sleep`, `step.WaitForEvent`, and `step.Invoke`               |
| [`inngestgo/errors`](https://pkg.go.dev/github.com/inngest/inngestgo/errors)                 | Retry control, such as non-retriable errors                                                       |
| [`inngestgo/pkg/checkpoint`](https://pkg.go.dev/github.com/inngest/inngestgo/pkg/checkpoint) | [Checkpointing](/docs-markdown/durable-execution/guides-and-advanced/checkpointing) configuration |
| [`inngestgo/stephttp`](https://pkg.go.dev/github.com/inngest/inngestgo/stephttp)             | [Durable Endpoints](/docs-markdown/reference/go/durable-endpoints) (beta)                         |
| [`inngestgo/realtime`](https://pkg.go.dev/github.com/inngest/inngestgo/realtime)             | [Realtime](/docs-markdown/realtime/reference#go) publish and subscribe                            |

## Guides

- [Durable Workflows in Go](/docs-markdown/reference/go/durable-workflows): create functions, set triggers, run steps, and configure retries and flow control
- [Durable Endpoints in Go](/docs-markdown/reference/go/durable-endpoints): run `net/http` handlers as functions with `stephttp`
- [Development and production](/docs-markdown/reference/go/dev-and-production): use the Dev Server locally, and set signing and event keys for production
- [Serve functions](/docs-markdown/durable-execution/deploying-functions/serve) and [Connect workers](/docs-markdown/durable-execution/deploying-functions/connect)
- [Primitives and steps](/docs-markdown/durable-execution/primitives) and [flow control](/docs-markdown/durable-execution/flow-control)
- [Environment variables](/docs-markdown/sdk/environment-variables): set `INNGEST_DEV`, `INNGEST_EVENT_KEY`, and `INNGEST_SIGNING_KEY` for local and production runs

**Migrations**

- [v0.15 to v0.16](/docs-markdown/reference/go/migrations/v0.16), [v0.8 to v0.11](/docs-markdown/reference/go/migrations/v0.8-to-v0.11), and [v0.7 to v0.8](/docs-markdown/reference/go/migrations/v0.7-to-v0.8)

## Source code

The Go SDK is open source on GitHub: [ inngest/inngestgo](https://github.com/inngest/inngestgo).