Categorygithub.com/EasyPost/easypost-go
modulepackage
1.4.0
Repository: https://github.com/easypost/easypost-go.git
Documentation: pkg.go.dev

# README

EasyPost Go Client Library

CI GoDoc

EasyPost is the simple shipping API. You can sign up for an account at https://easypost.com.

This work is licensed under the ISC License, a copy of which can be found at LICENSE

Requirements

The easypost Go package should work with any recent version of Go that supports modules (typically, 1.12+).

Installation

go get -u github.com/EasyPost/easypost-go

Documentation

See below example, or pkg.go.dev.

Example

package main

import (
	"fmt"
	"os"

	"github.com/EasyPost/easypost-go"
)

func main() {
	apiKey := os.Getenv("EASYPOST_API_KEY")
	if apiKey == "" {
		fmt.Fprintln(os.Stderr, "missing API key")
		os.Exit(1)
		return
	}
	client := easypost.New(apiKey)

	// create and verify addresses
	toAddr, err := client.CreateAddress(
		&easypost.Address{
			Name:    "Bugs Bunny",
			Street1: "4000 Warner Blvd",
			City:    "Burbank",
			State:   "CA",
			Zip:     "91522",
		},
		&easypost.CreateAddressOptions{Verify: []string{"delivery"}},
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error creating to address:", err)
		os.Exit(1)
		return
	}

	fromAddr, err := client.CreateAddress(
		&easypost.Address{
			Company: "EasyPost",
			Street1: "One Montgomery St",
			Street2: "Ste 400",
			City:    "San Francisco",
			State:   "CA",
			Zip:     "94104",
			Phone:   "415-555-1212",
		},
		&easypost.CreateAddressOptions{Verify: []string{"delivery"}},
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error creating from address:", err)
		os.Exit(1)
		return
	}

	// create parcel
	parcel, err := client.CreateParcel(
		&easypost.Parcel{
			Length: 10.2,
			Width:  7.8,
			Height: 4.3,
			Weight: 21.2,
		},
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error creating parcel:", err)
		os.Exit(1)
		return
	}

	// create customs_info form for intl shipping
	customsItem, err := client.CreateCustomsItem(
		&easypost.CustomsItem{
			Description:    "EasyPost t-shirts",
			HSTariffNumber: "123456",
			OriginCountry:  "US",
			Quantity:       2,
			Value:          96.27,
			Weight:         21.1,
		},
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error creating customs item:", err)
		os.Exit(1)
		return
	}

	customsInfo, err := client.CreateCustomsInfo(
		&easypost.CustomsInfo{
			CustomsCertify:    true,
			CustomsSigner:     "Wile E. Coyote",
			ContentsType:      "gift",
			EELPFC:            "NOEEI 30.37(a)",
			NonDeliveryOption: "return",
			RestrictionType:   "none",
			CustomsItems:      []*easypost.CustomsItem{customsItem},
		},
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error creating customs info:", err)
		os.Exit(1)
		return
	}

	// create shipment
	shipment, err := client.CreateShipment(
		&easypost.Shipment{
			ToAddress:   toAddr,
			FromAddress: fromAddr,
			Parcel:      parcel,
			CustomsInfo: customsInfo,
		},
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error creating shipment:", err)
		os.Exit(1)
		return
	}

	// buy postage label with one of the rate objects
	shipment, err = client.BuyShipment(shipment.ID, &easypost.Rate{ID: shipment.Rates[0].ID}, "")
	if err != nil {
		fmt.Fprintln(os.Stderr, "error buying shipment:", err)
		os.Exit(1)
		return
	}

	fmt.Println("tracking code:", shipment.TrackingCode)
	fmt.Println("label URL:", shipment.PostageLabel.LabelURL)

	// Insure the shipment for the value
	shipment, err = client.InsureShipment(shipment.ID, "100")
	if err != nil {
		fmt.Fprintln(os.Stderr, "error insuring shipment:", err)
		os.Exit(1)
		return
	}
}

Development

Releasing

  1. Update Version constant in version.go.
  2. Update CHANGELOG.
  3. Create a git tag with proper Go version semantics (e.g., v1.0.0).

Tests

Run unit tests by running go test from the tests directory:

Updating Recorded Responses

The go-vcr package is used to provide recorded responses to calls to the EasyPost HTTP API. If an API endpoint has changed, or a new endpoint is used, the recordings will need to be updated. To do this:

  1. If an existing recording is being changed, remove the corresponding file from the tests/testdata/TestClient directory. Each YAML file in this directory is named after the test it is used for, so it should be easy to identify the relevant file(s).
  2. Re-run the test with the appropriate production or test API keys specified. Most tests only require a test API key. E.g., go test -test-api-key [apikey]. Note that there is code in the tests that attempts to filter out API keys and Authorization headers, but it is recommended to check the generated files before pushing a branch to a shared repo or opening a pull request.

# Packages

No description provided by the author
No description provided by the author

# Functions

BoolPtr returns a pointer to a bool with the given value.
New returns a new Client with the given API key.
StringPtr returns a pointer to a string with the given value.
UnmarshalJSONObject attempts to unmarshal an easypost object from JSON data.

# Constants

No description provided by the author

# Structs

Address objects are used to represent people, places, and organizations in a number of contexts.
AddressVerification holds data relating to addres verification status.
AddressVerificationDetails contains extra information related to address verification.
AddressVerificationFieldError provides additional information on address validation errors.
AddressVerifications contains the result of the requested address verifications.
APIError provides data on why an API request failed.
APIKey represents a single API key.
APIKeys contains information about a list of API keys for the given user and any child users.
Batch represents a batch of shipments.
BatchStatus contains counts of statuses for the shipments in a batch.
CarrierAccount encapsulates credentials and other information related to a carrier account.
CarrierField provides data for a single field in a carrier account.
CarrierFields contains the data for carrier account fields for production and/or test credentials.
CarrierMessage contains additional status data that is provided by some carriers for certain operations.
CarrierType contains information on a supported carrier.
A Client provides an HTTP client for EasyPost API operations.
CreateAddressOptions is used to specify verification options for address creation.
CreateTrackerOptions specifies options for creating a new tracker.
CustomsInfo objects contain CustomsItem objects and all necessary information for the generation of customs forms required for international shipping.
A CustomsItem object describes goods for international shipment.
Event objects contain details about changes to EasyPost objects.
EventPayload represents the result of a webhook call.
Fee objects are used to represent the breakdown of charges made when purchasing an item on EasyPost.
A Form represents a form associated with a Shipment.
An Insurance object represents insurance for packages purchased both via the EasyPost API as well as shipments purchased through third parties and later registered with EasyPost.
ListAddressResult holds the results from the list insurances API.
ListBatchesResult holds the results from the list insurances API.
ListEventsResult holds the results from the list events API.
ListInsurancesResult holds the results from the list insurances API.
ListOptions is used to specify query parameters for listing EasyPost objects.
ListReportsOptions is used to specify query parameters for listing Report objects.
ListReportsResult holds the results from the list reports API.
ListScanFormsResult holds the results from the list scan forms API.
ListShipmentsOptions is used to specify query parameters for listing Shipment objects.
ListShipmentsResult holds the results from the list shipments API.
ListTrackersOptions is used to specify query parameters for listing Tracker objects.
ListTrackersResult holds the results from the list trackers API.
ListTrackersUpdatedOptions specifies options for the list trackers updated API.
An Order object represents a collection of packages and can be used for multi-piece Shipments.
A Parcel objects represent a physical container being shipped.
Payment provides information on how a shipment is billed.
No description provided by the author
A Pickup object represents a pickup from a carrier at a customer's residence or place of business.
PickupRate contains data about the cost of a pickup.
PostageLabel provides details of a shipping label for a purchased shipment.
A Rate contains information on shipping cost and delivery time.
No description provided by the author
Report represents a CSV-formatted file that is a log of all the objects created within a certain time frame.
A ScanForm object represents a document that can be scanned to mark all included tracking codes as "Accepted for Shipment" by the carrier.
A Shipment represents its namesake, and is made up of a "to" and "from" addresses, the Parcel being shipped, and any customs forms required for international deliveries.
ShipmentOptions represents the various options that can be applied to a shipment at creation.
TaxIdentifier objects contain tax information used by carriers.
No description provided by the author
A Tracker object contains all of the tracking information for a package.
TrackingCarrierDetail provides additional tracking information from the carrier, when available.
TrackingDetail provides information about a tracking event.
TrackingLocation provides additional information about the location of a tracking event.
A User contains data about an EasyPost account and child accounts.
UserOptions specifies options for creating or updating a user.
A Webhook represents an EasyPost webhook callback URL.