less than a minute read • Updated 12 minutes ago
Get Live Shipping Rates with EasyPost
Connect your EasyPost account to your Foxy store with the Custom Shipping Code feature to fetch live multi carrier rates at checkout.
Overview
EasyPost is a multi carrier shipping API that returns rates from a wide range of carriers through one integration. If you need rates beyond Foxy's native carriers (USPS, UPS, FedEx), you can fetch them from your EasyPost account using Foxy's Custom Shipping Code feature. This article shows you the code to connect to EasyPost and how to install it on your store.
Before You Start
You will need:
An EasyPost account with an API key (EasyPost dashboard, under Account Settings > API Keys). Use your production key for live rates.
Carriers added to your EasyPost account. By default EasyPost returns USPS rates; add more carriers in the EasyPost dashboard to get more rates.
Custom Shipping Code enabled on your store. See [Add Custom Shipping Code].
At least one product category set to a delivery type of "Shipped using live shipping rates". See [Shipping and Categories].
The Shipping Code
This code builds a shipment from the cart payload, posts it to the EasyPost shipments endpoint, then adds each returned rate to the cart. EasyPost expects weight in ounces, so the code converts the cart's weight from pounds.
const easypostApiKey = 'YOUR_EASYPOST_KEY';
const shipment = cart['_embedded']['fx:shipment'];
const shipmentData =
'shipment[to_address][name]=' + shipment['first_name'] + ' ' + shipment['last_name'] +
'&shipment[to_address][street1]=' + shipment['address1'] +
'&shipment[to_address][city]=' + shipment['city'] +
'&shipment[to_address][state]=' + shipment['region'] +
'&shipment[to_address][zip]=' + shipment['postal_code'] +
'&shipment[to_address][country]=' + shipment['country'] +
'&shipment[from_address][state]=' + shipment['origin_region'] +
'&shipment[from_address][zip]=' + shipment['origin_postal_code'] +
'&shipment[from_address][country]=' + shipment['origin_country'] +
'&shipment[parcel][length]=5&shipment[parcel][width]=5&shipment[parcel][height]=5' +
'&shipment[parcel][weight]=' + (shipment['total_weight'] * 16); // lbs to oz
try {
await request({
url: 'https://api.easypost.com/v2/shipments',
method: 'POST',
body: shipmentData,
auth: { 'user': easypostApiKey }
}, function (error, response, body) {
const payload = JSON.parse(body);
for (let i = 0; i < payload.rates.length; i++) {
rates.add(
10000 + i,
payload.rates[i]['rate'],
payload.rates[i]['carrier'],
payload.rates[i]['service']
);
}
});
} catch (e) {
rates.error("Sorry, we're unable to get shipping rates. Please try again or contact us.");
}Install the Integration
Customize the Integration
This is a basic setup. It uses default parcel dimensions (a 5 by 5 by 5 inch box) and returns every rate EasyPost provides. You can adjust the parcel, restrict which carriers are rated (by passing carrier accounts in the request), or work with the returned rates further. See the EasyPost documentation on shipments and rates to extend the request, and the Custom Shipping Code API Reference to filter, rename, or reprice rates.
Rates from custom shipping code are not currency converted by Foxy. If your store uses a non USD currency, set the rate values in the cart's currency yourself. See Shipping Currency Conversion.