Command Palette

Search for a command to run...

SDK

Go SDK

The official Go SDK for Unosend. Supports Go 1.21+ with full type safety.

Installation

terminal
go get github.com/unosend/unosend-go

Requirements

  • Go 1.21 or higher

Environment Variables

Never hardcode your API key. Use environment variables instead:

.env
UNOSEND_API_KEY=un_your_api_key
main.go
package main

import (
    "os"
    
    "github.com/unosend/unosend-go"
)

func main() {
    client := unosend.New(os.Getenv("UNOSEND_API_KEY"))
}

Basic Usage

main.go
package main

import (
    "fmt"
    "log"

    "github.com/unosend/unosend-go"
)

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

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

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

Response Format

All SDK methods return a result and an error. Check the error before using the result:

response.go
// Successful response
email, err := client.Emails.Send(&unosend.SendEmailRequest{...})
if err != nil {
    // Handle error
    if apiErr, ok := err.(*unosend.Error); ok {
        fmt.Printf("API Error %d: %s\n", apiErr.Code, apiErr.Message)
    }
    return
}

// Use the result
fmt.Printf("Email ID: %s\n", email.ID)
fmt.Printf("Status: %s\n", email.Status)

Send to Multiple Recipients

multiple.go
email, err := client.Emails.Send(&unosend.SendEmailRequest{
    From:    "hello@yourdomain.com",
    To:      []string{"user1@example.com", "user2@example.com"},
    Subject: "Team Update",
    HTML:    "<h1>Important Update</h1>",
    CC:      []string{"manager@example.com"},
    BCC:     []string{"archive@example.com"},
    ReplyTo: "support@yourdomain.com",
})

Send with Attachments

attachments.go
import (
    "encoding/base64"
    "os"
)

// Read file and encode to base64
fileContent, _ := os.ReadFile("invoice.pdf")
encoded := base64.StdEncoding.EncodeToString(fileContent)

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

Working with Domains

domains.go
// Add a domain
domain, err := client.Domains.Create("yourdomain.com")
fmt.Printf("Domain added: %s\n", domain.ID)
fmt.Printf("DNS Records: %+v\n", domain.Records)

// List all domains
domains, err := client.Domains.List()
for _, d := range domains {
    fmt.Printf("%s - %s\n", d.Name, d.Status)
}

// Verify domain DNS
domain, err := client.Domains.Verify(domainID)
fmt.Printf("Domain status: %s\n", domain.Status)

// Delete a domain
err := client.Domains.Delete(domainID)

Working with Audiences & Contacts

audiences.go
// Create an audience
audience, err := client.Audiences.Create("Newsletter Subscribers")
fmt.Printf("Audience created: %s\n", audience.ID)

// Add a contact to the audience
contact, err := client.Contacts.Create(&unosend.CreateContactRequest{
    AudienceID: audience.ID,
    Email:      "user@example.com",
    FirstName:  "John",
    LastName:   "Doe",
})
fmt.Printf("Contact added: %s\n", contact.ID)

// List contacts
contacts, err := client.Contacts.List(audience.ID)
for _, c := range contacts {
    fmt.Printf("%s - %s\n", c.Email, c.FirstName)
}

// Update a contact
firstName := "Jane"
contact, err := client.Contacts.Update(contactID, &unosend.UpdateContactRequest{
    FirstName: &firstName,
})

// Delete a contact
err := client.Contacts.Delete(contactID)

Error Handling

errors.go
email, err := client.Emails.Send(&unosend.SendEmailRequest{...})
if err != nil {
    // Check if it's an API error
    if apiErr, ok := err.(*unosend.Error); ok {
        fmt.Printf("API Error %d: %s\n", apiErr.Code, apiErr.Message)
        // Handle specific error codes
        switch apiErr.Code {
        case 401:
            fmt.Println("Invalid API key")
        case 429:
            fmt.Println("Rate limit exceeded")
        }
    } else {
        // Network or other error
        fmt.Printf("Error: %v\n", err)
    }
    return
}

Custom Configuration

config.go
import (
    "net/http"
    "time"
)

// Custom base URL (for self-hosted instances)
client := unosend.New("un_your_api_key",
    unosend.WithBaseURL("https://your-instance.com/api/v1"),
)

// Custom HTTP client with timeout
client := unosend.New("un_your_api_key",
    unosend.WithHTTPClient(&http.Client{
        Timeout: 60 * time.Second,
    }),
)