# Go SDK

Install github.com/dimmihq/dimmi-go to call the REST API, iterate paginated lists, sign identity tokens, and verify webhooks.

The official Go SDK requires Go 1.22 or later.

```bash
go get github.com/dimmihq/dimmi-go
```

## Create a client and call the API

```go title="main.go"
package main

	"context"
	"errors"
	"fmt"
	"log"
	"net/url"
	"os"

	dimmi "github.com/dimmihq/dimmi-go"
	"github.com/dimmihq/dimmi-go/api"
)

func main() {
	ctx := context.Background()

	client, err := dimmi.New(dimmi.WithToken(os.Getenv("DIMMI_TOKEN")))
	if err != nil {
		log.Fatal(err)
	}

	res, err := client.API.ConversationsIndexWithResponse(ctx, nil)
	if err != nil {
		var apiErr *dimmi.Error
		if errors.As(err, &apiErr) {
			log.Fatal(apiErr.Status, " ", apiErr.Code, " ", apiErr.Fields)
		}
		log.Fatal(err)
	}
	for _, conversation := range *res.JSON200.Data {
		fmt.Println(conversation.Id, conversation.Status)
	}

	open := dimmi.List[api.Conversation](client, "/conversations", url.Values{"filter[status]": {"open"}})
	for open.Next(ctx) {
		fmt.Println(open.Item().Id)
	}
	if err := open.Err(); err != nil {
		log.Fatal(err)
	}
}
```

Set `DIMMI_TOKEN` to your [API token](/developers/api/authentication/). `dimmi.New()` reads `DIMMI_TOKEN` automatically even if you omit `WithToken`.

## Ways to call endpoints

- **Typed methods:** `client.API` generates one method per operation ID (for example, `conversations.index` becomes `ConversationsIndexWithResponse`).
- **Raw requests:** call `client.Do(ctx, method, path, query, body, &out)` for any `/api/v1` path and decode JSON into `out`.
- **Pagination:** use `dimmi.List[T]` for cursor-based lists (`meta.next_cursor`) and `dimmi.Pages[T]` for page-numbered lists. Both expose `Next`, `Item`, `Err`, and `All`.

## Handle errors

API errors return `*dimmi.Error` with `Status`, `Code`, `Message`, `Fields`, and `RetryAfter`. Match status groups with `errors.Is`:

| Sentinel error | HTTP status |
| --- | --- |
| `dimmi.ErrBadRequest` | 400 |
| `dimmi.ErrUnauthorized` | 401 |
| `dimmi.ErrForbidden` | 403 |
| `dimmi.ErrNotFound` | 404 |
| `dimmi.ErrConflict` | 409 |
| `dimmi.ErrValidation` | 422 |
| `dimmi.ErrRateLimited` | 429 |
| `dimmi.ErrServer` | 500+ |

## Client options

| Option | Default | What it does |
| --- | --- | --- |
| `WithToken` | `DIMMI_TOKEN`, then `DIMMI_API_TOKEN` | Sets the API token. |
| `WithBaseURL` | `DIMMI_BASE_URL`, then `https://trydimmi.com` | Points the client at a self-hosted server. |
| `WithMaxRetries` | `3` | Retries on `429`, `503`, or dropped connections. |
| `WithMaxRetryWait` | 60 seconds | Max `Retry-After` wait before returning the error. |
| `WithTimeout` | 30 seconds | Total timeout per call, including retries. |

The Go client attaches an [`Idempotency-Key`](/developers/api/idempotency/) to every `POST` and reuses it across retries. Pass `dimmi.IdempotencyKey(key)` as the last argument on a generated method to supply your own key.

## Webhooks and identity tokens

Use `dimmi.NewWebhook(secret)` to verify webhooks (see [Verify webhook signatures](/developers/webhooks/verify-signatures/)) and `dimmi.SignIdentityToken` to mint JWTs for [identity verification](/developers/messenger/identity-verification/).

---

Index of all dimmi docs: https://trydimmi.com/llms.txt (full text: https://trydimmi.com/llms-full.txt)
