Go Examples
Go integration examples using the Metigan SDK. Learn how to send emails, manage contacts, and integrate Metigan into your Go applications.
Installation
install.shTerminal
1
go get github.com/metigan/metigan-goBasic Setup
main.goGo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"log"
"os"
"github.com/metigan/metigan-go"
)
func main() {
// Initialize the client
apiKey := os.Getenv("METIGAN_API_KEY")
if apiKey == "" {
log.Fatal("METIGAN_API_KEY environment variable is required")
}
client := metigan.NewClient(metigan.Config{
APIKey: apiKey,
})
// Send an email
result, err := client.Email().SendEmail(metigan.EmailOptions{
From: "Sender <sender@example.com>",
Recipients: []string{"recipient@example.com"},
Subject: "Hello from Go!",
Content: "<h1>Hello!</h1><p>This email was sent from a Go application.</p>",
})
if err != nil {
log.Fatalf("Failed to send email: %v", err)
}
if result.Success {
fmt.Println("Email sent successfully!")
fmt.Printf("Message: %s\n", result.Message)
fmt.Printf("Recipients: %d\n", result.RecipientCount)
fmt.Printf("Emails remaining: %d\n", result.EmailsRemaining)
} else {
fmt.Printf("Failed to send email: %s\n", result.Message)
}
}Sending Emails
Basic Email
send-email.goGo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
result, err := client.Email().SendEmail(metigan.EmailOptions{
From: "sender@example.com",
Recipients: []string{"recipient@example.com"},
Subject: "Email Subject",
Content: "<h1>HTML Content</h1><p>This is the email body.</p>",
})
if err != nil {
log.Fatalf("Error: %v", err)
}
if result.Success {
fmt.Println("Email sent successfully!")
}Email with CC and BCC
send-email-cc.goGo
1
2
3
4
5
6
7
8
9
result, err := client.Email().SendEmail(metigan.EmailOptions{
From: "company@email.com",
Recipients: []string{"main@email.com"},
Subject: "Meeting",
Content: "Email content",
CC: []string{"copy@email.com"},
BCC: []string{"hidden-copy@email.com"},
ReplyTo: "reply-here@email.com",
})Email with Attachments
send-email-attachment.goGo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import (
"os"
"github.com/metigan/metigan-go"
)
// Read file
fileData, err := os.ReadFile("document.pdf")
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
result, err := client.Email().SendEmail(metigan.EmailOptions{
From: "company@email.com",
Recipients: []string{"customer@email.com"},
Subject: "Important Document",
Content: "Please find the document attached.",
Attachments: []metigan.Attachment{
{
Content: fileData,
Filename: "document.pdf",
ContentType: "application/pdf",
},
},
})Email with Template
send-email-template.goGo
1
2
3
4
5
6
7
8
9
10
11
12
13
variables := map[string]interface{}{
"name": "John Doe",
"company": "Acme Inc",
}
result, err := client.Email().SendEmailWithTemplate(
"template-123",
variables,
metigan.EmailOptions{
From: "sender@example.com",
Recipients: []string{"recipient@example.com"},
},
)Contact Management
Create Contact
create-contact.goGo
1
2
3
4
5
6
7
8
9
10
11
12
13
contact, err := client.Contacts().Create(metigan.CreateContactOptions{
Email: "new@email.com",
FirstName: "Jane",
LastName: "Doe",
AudienceID: "audience-123",
Tags: []string{"customer", "newsletter"},
})
if err != nil {
log.Fatalf("Error: %v", err)
}
fmt.Printf("Contact created: %s\n", contact.ID)Get Contact
get-contact.goGo
1
2
3
4
5
// By ID
contact, err := client.Contacts().Get("contact-456")
// By email
contact, err := client.Contacts().GetByEmail("jane@email.com", "audience-123")List Contacts
list-contacts.goGo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
result, err := client.Contacts().List(metigan.ContactListFilters{
AudienceID: "audience-123",
Status: metigan.ContactStatusSubscribed,
Page: 1,
Limit: 50,
})
if err != nil {
log.Fatalf("Error: %v", err)
}
for _, contact := range result.Contacts {
fmt.Printf("%s: %s\n", contact.Email, contact.FirstName)
}Update Contact
update-contact.goGo
1
2
3
4
updated, err := client.Contacts().Update("contact-456", metigan.UpdateContactOptions{
FirstName: "Jane Marie",
Tags: []string{"customer", "vip"},
})Manage Subscription
manage-subscription.goGo
1
2
3
4
5
// Unsubscribe
err := client.Contacts().Unsubscribe("contact-456")
// Resubscribe
err := client.Contacts().Subscribe("contact-456")Audience Management
Create Audience
create-audience.goGo
1
2
3
4
audience, err := client.Audiences().Create(metigan.CreateAudienceOptions{
Name: "Main Newsletter",
Description: "Main subscriber list",
})List Audiences
list-audiences.goGo
1
2
3
4
5
6
7
8
9
10
11
12
result, err := client.Audiences().List(metigan.PaginationOptions{
Page: 1,
Limit: 10,
})
if err != nil {
log.Fatalf("Error: %v", err)
}
for _, audience := range result.Audiences {
fmt.Printf("%s: %d contacts\n", audience.Name, audience.Count)
}Get Audience Statistics
audience-stats.goGo
1
2
3
4
5
6
7
8
9
stats, err := client.Audiences().GetStats("audience-123")
if err != nil {
log.Fatalf("Error: %v", err)
}
fmt.Printf("Total: %d\n", stats.Total)
fmt.Printf("Subscribed: %d\n", stats.Subscribed)
fmt.Printf("Unsubscribed: %d\n", stats.Unsubscribed)Error Handling
error-handling.goGo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
result, err := client.Email().SendEmail(options)
if err != nil {
switch e := err.(type) {
case *metigan.APIError:
fmt.Printf("API Error: %d - %s\n", e.StatusCode, e.Message)
case *metigan.ValidationError:
fmt.Printf("Validation Error: %s\n", e.Message)
if e.Field != "" {
fmt.Printf("Field: %s\n", e.Field)
}
default:
fmt.Printf("Unknown error: %v\n", err)
}
return
}Advanced Configuration
advanced-config.goGo
1
2
3
4
5
6
7
8
9
import "time"
client := metigan.NewClient(metigan.Config{
APIKey: "your-api-key",
Timeout: 30 * time.Second, // Optional, defaults to 30s
RetryCount: 3, // Optional, defaults to 3
RetryDelay: 2 * time.Second, // Optional, defaults to 2s
Debug: false, // Optional, defaults to false
})HTTP Server Example
http-server.goGo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/metigan/metigan-go"
)
func main() {
apiKey := os.Getenv("METIGAN_API_KEY")
client := metigan.NewClient(metigan.Config{
APIKey: apiKey,
})
http.HandleFunc("/send-email", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req struct {
To string `json:"to"`
Subject string `json:"subject"`
Content string `json:"content"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result, err := client.Email().SendEmail(metigan.EmailOptions{
From: "noreply@example.com",
Recipients: []string{req.To},
Subject: req.Subject,
Content: req.Content,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
})
fmt.Println("Server listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}Environment Variables
Use environment variables to store your API key securely. Set METIGAN_API_KEY before running your application.