🐹

Go SDK

Official SDK for Go applications and microservices.

Installation

go get github.com/notification-system/sdk-go

Requirements

  • • Go 1.18 or later
  • • Go modules enabled

Quick Start

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    notification "github.com/notification-system/sdk-go"
)

func main() {
    // Initialize client
    client := notification.NewClient(os.Getenv("NOTIFICATION_API_KEY"))

    // Send notification
    resp, err := client.SendNotification(context.Background(), &notification.NotificationRequest{
        Type: []string{"email", "sms"},
        Content:  "Hello World!",
        Target:  []string{"user@example.com", "+1234567890"},
    })
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Notification ID: %s\n", resp.ID)
    fmt.Printf("Status: %s\n", resp.Status)
}

Configuration Options

// With custom configuration
client := notification.NewClient(
    os.Getenv("NOTIFICATION_API_KEY"),
    notification.WithBaseURL("https://custom-api.example.com"),
    notification.WithTimeout(60 * time.Second),
    notification.WithRetries(5),
)

API Methods

client.SendNotification(ctx, request)

Send a notification to one or more channels.

resp, err := client.SendNotification(ctx, &notification.NotificationRequest{
    Type: []string{"email", "sms", "slack"},
    Content:  "Your order has been shipped!",
    Target:  []string{"user@example.com", "+1234567890", "#orders"},
    Metadata: map[string]interface{}{
        "orderId": "12345",
        "subject": "Order Shipped",
    },
})
client.GetNotification(ctx, id)

Get details of a specific notification.

notification, err := client.GetNotification(ctx, "ntf_abc123")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Status: %s\n", notification.Status)
client.ListNotifications(ctx, options)

List notifications with pagination and filters.

result, err := client.ListNotifications(ctx, &notification.ListNotificationsParams{
    Page:    1,
    PerPage: 20,
    Status:  "delivered",
    Channel: "email",
})
for _, n := range result.Notifications {
    fmt.Printf("ID: %s, Status: %s\n", n.ID, n.Status)
}

Error Handling

resp, err := client.SendNotification(ctx, request)
if err != nil {
    log.Printf("Failed to send notification: %v", err)
    return
}

Context Support

All methods accept a context for cancellation and timeouts:

// With timeout
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

resp, err := client.SendNotification(ctx, request)
if err != nil {
    if errors.Is(err, context.DeadlineExceeded) {
        log.Println("Request timed out")
    }
}

// With cancellation
ctx, cancel := context.WithCancel(context.Background())
go func() {
    // Cancel after some condition
    cancel()
}()

resp, err := client.SendNotification(ctx, request)