Skip to main content
Module: github.com/unosend/unosend-go · Go: 1.21+

Installation

go get github.com/unosend/unosend-go

Quick Start

package main

import (
    "fmt"
    "log"
    "github.com/unosend/unosend-go"
)

func main() {
    client := unosend.New("un_your_api_key")

    email, err := client.Emails.Send(&unosend.SendEmailRequest{
        From:    "hello@yourdomain.com",
        To:      []string{"user@example.com"},
        Subject: "Hello from Unosend!",
        HTML:    "<h1>Welcome!</h1><p>Thanks for signing up.</p>",
    })
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Email sent: %s\n", email.ID)
}

Configuration

client := unosend.New("un_your_api_key",
    unosend.WithBaseURL("https://www.unosend.co/api/v1"),
    unosend.WithTimeout(30 * time.Second),
)
Never hardcode your API key. Use environment variables: os.Getenv("UNOSEND_API_KEY")

Emails

Send an Email

email, err := client.Emails.Send(&unosend.SendEmailRequest{
    From:    "John Doe <john@yourdomain.com>",
    To:      []string{"user@example.com"},
    Subject: "Order Confirmation",
    HTML:    "<h1>Order #1234</h1><p>Your order has been confirmed.</p>",
    Text:    "Order #1234 - Your order has been confirmed.",
    ReplyTo: "support@yourdomain.com",
    CC:      []string{"manager@yourdomain.com"},
    BCC:     []string{"archive@yourdomain.com"},
    Tags: []unosend.Tag{
        {Name: "category", Value: "order"},
    },
    Headers: map[string]string{
        "X-Entity-Ref-ID": "order-1234",
    },
    Tracking: &unosend.Tracking{
        Open:  true,
        Click: true,
    },
})

Send with Attachments

import "encoding/base64"

pdfBytes, _ := os.ReadFile("invoice.pdf")
pdfContent := base64.StdEncoding.EncodeToString(pdfBytes)

email, err := client.Emails.Send(&unosend.SendEmailRequest{
    From:    "billing@yourdomain.com",
    To:      []string{"customer@example.com"},
    Subject: "Your Invoice",
    HTML:    "<p>Please find your invoice attached.</p>",
    Attachments: []unosend.Attachment{
        {
            Filename:    "invoice.pdf",
            Content:     pdfContent,
            ContentType: "application/pdf",
        },
    },
})

Send Batch Emails

results, err := client.Emails.SendBatch([]unosend.SendEmailRequest{
    {
        From:    "hello@yourdomain.com",
        To:      []string{"alice@example.com"},
        Subject: "Welcome, Alice!",
        HTML:    "<h1>Welcome!</h1>",
    },
    {
        From:    "hello@yourdomain.com",
        To:      []string{"bob@example.com"},
        Subject: "Welcome, Bob!",
        HTML:    "<h1>Welcome!</h1>",
    },
})

Get Email Details

email, err := client.Emails.Get("email_abc123")

fmt.Println(email.Status)    // "delivered"
fmt.Println(email.OpenedAt)  // "2026-02-28T10:30:00Z"

Domains

// Create domain
domain, err := client.Domains.Create(&unosend.CreateDomainRequest{
    Domain: "yourdomain.com",
})
fmt.Println("DNS records:", domain.DNSRecords)

// Verify after adding DNS records
verified, err := client.Domains.Verify(domain.ID)
fmt.Println("Verified:", verified.Verified)

Contacts

// Create audience
audience, err := client.Audiences.Create(&unosend.CreateAudienceRequest{
    Name: "Newsletter Subscribers",
})

// Add contact
contact, err := client.Contacts.Create(audience.ID, &unosend.CreateContactRequest{
    Email:     "user@example.com",
    FirstName: "Alice",
    LastName:  "Smith",
    Metadata: map[string]interface{}{
        "plan": "pro",
    },
})

SMS

sms, err := client.SMS.Send(&unosend.SendSMSRequest{
    To:      "+14155551234",
    Message: "Your verification code is 384729. Expires in 10 minutes.",
})

fmt.Printf("SMS sent: %s (cost: $%.3f)\n", sms.ID, sms.Cost)

WhatsApp

msg, err := client.WhatsApp.Send(&unosend.SendWhatsAppRequest{
    To:        "+919876543210",
    Type:      "template",
    Template:  "order_update",
    Language:  "en",
    Variables: []string{"Alice", "ORD-1234"},
})

Error Handling

email, err := client.Emails.Send(&unosend.SendEmailRequest{
    From:    "hello@yourdomain.com",
    To:      []string{"user@example.com"},
    Subject: "Hello",
    HTML:    "<h1>Hello</h1>",
})
if err != nil {
    var apiErr *unosend.APIError
    if errors.As(err, &apiErr) {
        switch apiErr.StatusCode {
        case 401:
            log.Fatal("Invalid API key")
        case 422:
            log.Printf("Validation error: %s", apiErr.Message)
        case 429:
            log.Printf("Rate limited. Retry after %d seconds", apiErr.RetryAfter)
        default:
            log.Printf("API error %d: %s", apiErr.StatusCode, apiErr.Message)
        }
    }
    log.Fatal(err)
}

fmt.Printf("Sent: %s\n", email.ID)

HTTP Server Example

package main

import (
    "encoding/json"
    "net/http"
    "os"
    "github.com/unosend/unosend-go"
)

var client = unosend.New(os.Getenv("UNOSEND_API_KEY"))

func sendHandler(w http.ResponseWriter, r *http.Request) {
    var req struct {
        Email   string `json:"email"`
        Subject string `json:"subject"`
        HTML    string `json:"html"`
    }
    json.NewDecoder(r.Body).Decode(&req)

    email, err := client.Emails.Send(&unosend.SendEmailRequest{
        From:    "hello@yourdomain.com",
        To:      []string{req.Email},
        Subject: req.Subject,
        HTML:    req.HTML,
    })
    if err != nil {
        http.Error(w, err.Error(), 500)
        return
    }

    json.NewEncoder(w).Encode(map[string]string{"id": email.ID})
}

func main() {
    http.HandleFunc("/send", sendHandler)
    http.ListenAndServe(":8080", nil)
}

Environment Variables

.env
UNOSEND_API_KEY=un_your_api_key

Full API Reference

See all available endpoints and parameters