less than a minute read • Updated 12 minutes ago
Get Live Shipping Rates with Shippo
Connect your Shippo account to your Foxy store with the Custom Shipping Code feature to fetch live rates from Shippo's supported carriers at checkout.
Overview
Shippo connects to more than 40 carriers through a single API. If you need rates from carriers that Foxy does not natively support (USPS, UPS, FedEx), you can fetch them from your Shippo account using Foxy's Custom Shipping Code feature. This article shows you the code to connect to Shippo and how to install it on your store.
Before You Start
You will need:
A Shippo account with a live API key (Shippo dashboard, under Settings > API).
Custom Shipping Code enabled on your store. See Add Custom Shipping Code for how to turn it on.
At least one product category set to a delivery type of "Shipped using live shipping rates", so the custom code runs for it. See [Shipping and Categories].
The Shipping Code
This code reads the customer's shipment details from the cart payload, asks Shippo to create a shipment, then adds every rate Shippo returns to the cart.
let myshippo = require('shippo')('YOUR_SHIPPO_LIVE_API_KEY');
let shipment = cart['_embedded']['fx:shipment'];
let addressFrom = {
'state': shipment['origin_region'],
'zip': shipment['origin_postal_code'],
'country': shipment['origin_country']
};
let addressTo = {
'name': shipment['first_name'] + " " + shipment['last_name'],
'company': shipment['company'],
'street1': shipment['address1'],
'city': shipment['city'],
'state': shipment['region'],
'zip': shipment['postal_code'],
'country': shipment['country'],
'is_residential': shipment['is_residential']
};
let parcel = {
'length': '5',
'width': '5',
'height': '5',
'distance_unit': 'in',
'weight': shipment['total_weight'],
'mass_unit': 'lb',
};
try {
let myshipment = await myshippo.shipment.create({
"address_from": addressFrom,
"address_to": addressTo,
"parcels": [parcel],
"async": false
});
let rate_options = await myshippo.shipment.rates(myshipment.object_id);
for (var i = 0; i < rate_options.results.length; i++) {
rates.add((10000+i), rate_options.results[i]['amount'], rate_options.results[i]['provider'], rate_options.results[i]['servicelevel']['name']);
}
} catch (e) {
console.log(e);
rates.error("Sorry, we're unable to get shipping rates at this time. Please try again, or contact us to finalize your order");
}Install the Integration
Customize the Integration
This is a basic setup. It returns every rate from Shippo and uses default values for the parcel (a 5 by 5 by 5 inch box). Shippo provides optional extras in their API that you can use to extend the request. See the Shippo documentation on rate requests, and the Custom Shipping Code API Reference to filter, rename, or reprice the returned rates.
Note that rates returned from custom shipping code are not currency converted by Foxy. If your store uses a currency other than USD, set or convert the rate values to the cart's currency yourself. See Shipping Currency Conversion.