Back

Step by Step Guide to Building a Shippo API Integration in Go

Sep 14, 20246 minute read

Introduction

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.

Prerequisites

Before we set sail, make sure you've got:

  • Go installed on your machine
  • A Shippo account with an API key
  • Some basic Go knowledge and familiarity with RESTful APIs

Got all that? Great! Let's get started.

Setting up the project

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

Initializing the Shippo client

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 }

Implementing core Shippo functionalities

Creating an address

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)

Creating a parcel

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)

Creating a shipment

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)

Retrieving rates

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))

Purchasing a label

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)

Error handling and best practices

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.

Testing the integration

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.

Conclusion

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!

Resources

Happy shipping, Go developer! 🚢✨