Documentation You are here: start » v » 2.0 » shipping » custom_code » easypost

Custom Shipping Code EasyPost Integration

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.

Requirements

  • A Foxy store on version 2.0
  • At least one category configured with a delivery type of “Shipped using live shipping rates”
  • An active EasyPost account

Installation

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");
}

Steps

  1. Log in to your store's Foxy administration
  2. Head to the “Shipping” page
  3. Scroll down until you see the “Custom Shipping Code” option, and check the option for “use custom code”.
  4. In the code editor that appears, copy and paste the code shown above. Don't save it just yet.
  5. In a new tab, log in to your EasyPost account
  6. Go to API keys section
  7. Within the “API Keys” section, click the “ADD API KEY” button and select Production or Test, then click “CREATE API KEY”.
  8. Copy the API key generated to your clipboard from either the Production or Testing (depending which type you created), and in the code you pasted into your Foxy admin above on this line:
    let api_key = 'YOUR-EASYPOST-API-KEY';


    Replace the YOUR-EASYPOST-API-KEY text with your copied API key

  9. Update this line to test instead of production if you are using a Test API key:
    let mode = 'production';
  10. Configure the remainder of the configuration options for weight/length units and package size
  11. Click the “Update” button to save your new settings
  12. Wait ~10-15 seconds and refresh the page (the custom shipping code feature is a separate system, so takes a moment to initialize, you should see your custom code again after refreshing if it was successful)
  13. You're done! Now you can test it out!

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.

Taking It Further

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.

Site Tools