Hey there, fellow Go developer! Ready to supercharge your shipping game? Let's dive into integrating the Shippo API using the awesome go-shippo
package. Shippo's API is a game-changer for automating shipping processes, and with Go, we'll make it smooth sailing.
Before we set sail, make sure you've got:
Got all that? Great! Let's get started.
First things first, let's create a new Go project and grab the go-shippo
package:
mkdir shippo-integration && cd shippo-integration go mod init shippo-integration go get github.com/OrderMyGear/go-shippo
Now, let's import the package and create a new Shippo client:
package main import ( "fmt" "github.com/OrderMyGear/go-shippo" ) func main() { client := shippo.NewClient("your-api-key-here") // We'll use this client for all our Shippo operations }
Let's start by creating a sender address:
senderAddress, err := client.CreateAddress(&shippo.AddressInput{ Name: "Shippo Sender", Street1: "215 Clayton St.", City: "San Francisco", State: "CA", Zip: "94117", Country: "US", }) if err != nil { fmt.Println("Error creating address:", err) return } fmt.Println("Sender address created:", senderAddress.ObjectID)
Next, let's define our parcel:
parcel, err := client.CreateParcel(&shippo.ParcelInput{ Length: "5", Width: "5", Height: "5", DistanceUnit: "in", Weight: "2", MassUnit: "lb", }) if err != nil { fmt.Println("Error creating parcel:", err) return } fmt.Println("Parcel created:", parcel.ObjectID)
Now, let's create a shipment using our sender address and parcel:
shipment, err := client.CreateShipment(&shippo.ShipmentInput{ AddressFrom: senderAddress.ObjectID, AddressTo: "recipient-address-object-id", Parcels: []string{parcel.ObjectID}, Async: false, }) if err != nil { fmt.Println("Error creating shipment:", err) return } fmt.Println("Shipment created:", shipment.ObjectID)
Time to get some shipping rates:
rates, err := client.GetShipmentRates(shipment.ObjectID, "") if err != nil { fmt.Println("Error retrieving rates:", err) return } fmt.Println("Available rates:", len(rates))
Finally, let's purchase a shipping label:
transaction, err := client.PurchaseShippingLabel(&shippo.TransactionInput{ Rate: rates[0].ObjectID, Async: false, }) if err != nil { fmt.Println("Error purchasing label:", err) return } fmt.Println("Label purchased:", transaction.LabelURL)
As you've seen, we're checking for errors after each API call. This is crucial for robust error handling. Also, consider implementing retries for transient errors and use environment variables for sensitive data like API keys.
Don't forget to thoroughly test your integration! Create test cases for each functionality and consider edge cases. The go-shippo
package provides a MockClient
for testing without hitting the actual API.
And there you have it! You've just built a solid Shippo API integration in Go. You've learned how to create addresses, parcels, shipments, retrieve rates, and purchase labels. But don't stop here – there's so much more you can do with Shippo and Go!
Happy shipping, Go developer! 🚢✨