> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Go

# Go SDK

Official **Go** client for the Mailofly REST API.

**Module:** [`github.com/teamredevs/mailofly-go`](https://pkg.go.dev/github.com/teamredevs/mailofly-go) · **Requires:** Go 1.22+ · **Quickstart:** [Go](/docs/getting-started/go.md)

***

## Install

```bash theme={null}
go get github.com/teamredevs/mailofly-go@latest
```

***

## Initialize

```go theme={null}
package main

import (
	"os"

	"github.com/teamredevs/mailofly-go"
)

func main() {
	client, err := mailofly.New(mailofly.Options{
		APIKey: os.Getenv("MAILOFLY_API_KEY"),
	})
	if err != nil {
		panic(err)
	}
	_ = client
}
```

Create an API key under **[API keys](https://www.mailofly.com/user/api-keys)** first.

***

## Send email

```go theme={null}
result, err := client.Emails.Send(map[string]any{
	"from":    "Acme <onboarding@example.com>",
	"to":      []string{"alex@example.com"},
	"subject": "Hello",
	"html":    "<p>Thanks for signing up.</p>",
})
if err != nil {
	if apiErr, ok := err.(*mailofly.Error); ok {
		fmt.Println(apiErr.Status, apiErr.Err, apiErr.DetailMessage)
	}
	panic(err)
}
_ = result
```

### With a saved template

```go theme={null}
client.Emails.Send(map[string]any{
	"from": "Acme <onboarding@example.com>",
	"to":   []string{"alex@example.com"},
	"template": map[string]any{
		"id":        "uuid-of-template",
		"variables": map[string]any{"first_name": "Alex"},
	},
})
```

`client.Compose.Send` is deprecated and forwards to `/v1/emails`.

### Batch send (up to 100 emails)

```go theme={null}
result, err := client.Batch.Send([]map[string]any{
	{
		"from":    "Acme <onboarding@example.com>",
		"to":      []string{"foo@example.com"},
		"subject": "hello world",
		"html":    "<h1>it works!</h1>",
	},
	{
		"from":    "Acme <onboarding@example.com>",
		"to":      []string{"bar@example.com"},
		"subject": "world hello",
		"html":    "<p>it works!</p>",
	},
})
```

***

## Resources

| Namespace | Methods |
| - | - |
| `client.Identities` | `List`, `Get`, `Update`, `Delete` *(creation is dashboard-only)* |
| `client.Accounts` | *(deprecated alias for `client.Identities`)* |
| `client.Contacts` | `List`, `Create`, `Get`, `Update`, `Delete` |
| `client.Templates` | `List`, `Create`, `Get`, `Update`, `Delete` |
| `client.Segments` | `List`, `Create`, `Get`, `Update`, `Delete`, `Contacts.List/Add/Remove` |
| `client.Campaigns` | `List`, `Create`, `Get`, `Update`, `Delete`, `Runs`, `Send` |
| `client.Emails` | `List`, `Get`, `Send` |
| `client.Batch` | `Send` |
| `client.Compose` | `Send` (deprecated) |
| `client.MailLogs` | `List` |

### Examples

```go theme={null}
identities, _ := client.Identities.List()

client.Contacts.Create(map[string]any{
	"email":          "alex@example.com",
	"first_name":     "Alex",
	"custom_fields":  map[string]any{"plan": "pro"},
})

client.Campaigns.Send("campaign-uuid", map[string]any{"send_now": true})

logs, _ := client.MailLogs.List(&mailofly.MailLogsListOptions{
	Status:   "failed",
	Page:     1,
	PageSize: 50,
})
_ = identities
_ = logs
```

***

## Discovery (no API key)

```go theme={null}
meta, err := mailofly.Discovery("")
if err != nil {
	panic(err)
}
fmt.Println(meta)
```

***

## Error handling

```go theme={null}
if apiErr, ok := err.(*mailofly.Error); ok {
	fmt.Println(apiErr.Status, apiErr.Err, apiErr.DetailMessage)
}
```

***

## Related

* [Go quickstart](/docs/getting-started/go.md)
* [API: Emails](https://docs.mailofly.com/api/emails)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.