Skip to main content

Go SDK

Installation

go get github.com/flexops/go-sdk

Quick Start

package main

import (
"context"
"fmt"
"github.com/flexops/go-sdk"
)

func main() {
client := flexops.NewClient("your-api-key")

rates, err := client.Rates.Get(context.Background(), &flexops.RateRequest{
From: &flexops.Address{Zip: "80202", Country: "US"},
To: &flexops.Address{Zip: "10001", Country: "US"},
Parcel: &flexops.Parcel{Weight: 2.5, Length: 10, Width: 8, Height: 4},
})
if err != nil {
panic(err)
}

for _, rate := range rates.Rates {
fmt.Printf("%s %s: $%.2f\n", rate.Carrier, rate.Service, rate.TotalPrice)
}
}

Configuration

client := flexops.NewClient("your-api-key",
flexops.WithBaseURL("https://api.flexops.io/v1"),
flexops.WithTimeout(30 * time.Second),
flexops.WithMaxRetries(3),
)

Shipping Operations

Create a Label

label, err := client.Labels.Create(ctx, &flexops.CreateLabelRequest{
Carrier: "usps",
Service: "priority",
From: &flexops.Address{
Name: "Warehouse",
Street1: "123 Main St",
City: "Denver",
State: "CO",
Zip: "80202",
Country: "US",
},
To: &flexops.Address{
Name: "Customer",
Street1: "456 Broadway",
City: "New York",
State: "NY",
Zip: "10001",
Country: "US",
},
Parcel: &flexops.Parcel{Weight: 2.5, Length: 10, Width: 8, Height: 4},
}, flexops.WithIdempotencyKey("order-12345"))

fmt.Println(label.TrackingNumber)
fmt.Println(label.LabelURL)

Track a Shipment

tracking, err := client.Tracking.Get(ctx, "9400111899223456789012")

fmt.Println(tracking.Status)
fmt.Println(tracking.EstimatedDelivery)

for _, event := range tracking.Events {
fmt.Printf("%s: %s\n", event.Timestamp, event.Description)
}

Validate an Address

result, err := client.Addresses.Validate(ctx, &flexops.Address{
Street1: "123 Main St",
City: "Denver",
State: "CO",
Zip: "80202",
Country: "US",
})

if result.IsValid {
fmt.Printf("Validated: %+v\n", result.Normalized)
}

Webhook Verification

func webhookHandler(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
signature := r.Header.Get("X-FlexOps-Signature")

if !flexops.VerifyWebhookSignature(body, signature, "whsec_your_secret") {
http.Error(w, "Invalid signature", http.StatusUnauthorized)
return
}

var event flexops.WebhookEvent
json.Unmarshal(body, &event)

switch event.Type {
case "tracking.delivered":
handleDelivery(event.Data)
}

w.WriteHeader(http.StatusOK)
}

Error Handling

label, err := client.Labels.Create(ctx, request)
if err != nil {
var rateLimitErr *flexops.RateLimitError
if errors.As(err, &rateLimitErr) {
fmt.Printf("Retry after %d seconds\n", rateLimitErr.RetryAfter)
return
}

var apiErr *flexops.Error
if errors.As(err, &apiErr) {
fmt.Printf("%s: %s\n", apiErr.Code, apiErr.Message)
return
}

panic(err)
}