> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mailofly.com/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](../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"),
		// BaseURL: "https://www.mailofly.com", // optional
	})
	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.Accounts`  | `List`, `Create`, `Get`, `Update`, `Delete`                             |
| `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}
accounts, _ := client.Accounts.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,
})
_ = accounts
_ = 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](../getting-started/go.md)
* [API: Emails](https://docs.mailofly.com/api/emails)


## Related topics

- [SDKs](/sdks/index.md)
- [Go](/getting-started/go.md)
- [Http](/getting-started/http.md)
- [Manage Emails](/sending/manage-emails.md)
- [Introduction](/sending/introduction.md)
