The following page details integrating EasyPost into your Foxy store for fetching shipping rates from any of their supported providers, using the Custom Shipping Code functionality available for Foxy stores.
Below is the custom shipping code you'll use to connect to EasyPost, and below that is the steps to install it on your store.
// Set to the API key of your EasyPost account const easypostApiKey = 'YOUR-EASYPOST-API-KEY'; // use values 'test' or 'production' let mode = 'production'; let shipment = cart['_embedded']['fx:shipment']; let parcel = { 'length': '5', 'width': '5', 'height': '5', // in ounces, if shipping by the pound, multiply by 16 'weight': shipment['total_weight']*16, }; let shipmentData = 'mode=' + mode + '&shipment[to_address][name]=' + shipment['first_name'] + " " + shipment['last_name'] + '&shipment[to_address][company]=' + shipment['company'] + '&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[to_address][residential]=' + shipment['is_residential'] + '&shipment[from_address][street1]=' + '' + '&shipment[from_address][city]=' + '' + '&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]=' + parcel['length'] + '&shipment[parcel][width]=' + parcel['width'] + '&shipment[parcel][height]=' + parcel['height'] + '&shipment[parcel][weight]=' + parcel['weight']; if (shipment['origin_country'] !== shipment['country']) { shipmentData += '&shipment[customs_info][customs_certify]=true' + '&shipment[customs_info][customs_signer]=John Smith' + '&shipment[customs_info][contents_type]=merchandise' + '&shipment[customs_info][contents_explanation]=' + '&shipment[customs_info][restriction_type]=none' + '&shipment[customs_info][eel_pfc]=NOEEI 30.37(a)'; for (let p = 0; p < cart['_embedded']['fx:items'].length; p++) { const item = cart['_embedded']['fx:items'][p]; shipmentData += '&shipment[customs_info][customs_items][' + p + '][description]=' + item.name + '&shipment[customs_info][customs_items][' + p + '][quantity]=' + item.quantity + '&shipment[customs_info][customs_items][' + p + '][weight]=' + (item.weight * item.quantity) + '&shipment[customs_info][customs_items][' + p + '][value]=' + (item.price * item.quantity) + // '&shipment[customs_info][customs_items][' + p + '][hs_tariff_number]=' + item.quantity + '&shipment[customs_info][customs_items][' + p + '][origin_country]=' + shipment['origin_country']; } } var options = { url: 'https://api.easypost.com/v2/shipments', method: 'POST', body: shipmentData, auth: { 'user': easypostApiKey, } }; try { await request(options, function(error, response, body) { let payload = JSON.parse(body); let easypostRates = payload['rates']; for (var i = 0; i < easypostRates.length; i++) { // for easypostRates[i]['rate'] you can instead use easypostRates[i]['retail_rate'] for the retail rate rates.add((10000+i), easypostRates[i]['rate'], easypostRates[i]['carrier'], easypostRates[i]['service']); } }); } 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"); }
let api_key = 'YOUR-EASYPOST-API-KEY';
Replace the YOUR-EASYPOST-API-KEY
text with your copied API key
test
instead of production
if you are using a Test API key:let mode = 'production';
To test our the new rates after saving, you can simply add a product to your cart that belongs to a live rate shippable category. On the cart or checkout, after entering a shipping address, you should see your rates returning from your EasyPost account.
Note that for international shipments (based on your store country compared to the shipment country), the customs_info
object will be passed. We would recommend reviewing the values of that object in the code to confirm it matches to your requirements. Review the EasyPost documentation for more details on what values are possible there.
This integration is fairly basic - it just returns all rates from EasyPost, and passes it some default values. You can review the EasyPost documentation on shipment rate requests with their API here to customise the integration above further as needed.
You can also customise the rates returned, to only output specific rates or work with them further using our custom shipping code API documented here.