Built-in Modules > FedEx Class
FedEx Class
Important: This plugin relies on third-party resources. The resources might change or be discontinued without notice. Ensure you test the plugin extensively before production use, implement error handling with a try-catch block, have a backup shipping rate ready, and reach out to our support with detailed descriptions of any unexpected issues.
Overview
The FedEx
class is specially designed for obtaining real-time shipping rates from FedEx.
Class Definition
class FedEx {
constructor(origin, destination, currency, order_processing_days) {
// Class constructor
}
async getRates(parcels) {
// Method to fetch rates
}
}
Arguments
origin
(Object): Represents the "origin" information from the DATA JSON object used in thecalculateShippingRates
function.destination
(Object): Represents the "destination" information in the same DATA JSON object.currency
(String): The desired currency ISO code for calculating shipping rates.order_processing_days
(Number): The number of days until the parcels will be ready for pickup.
Usage Example
import { FedEx } from "./modules.js"
const fedex = new FedEx(DATA.origin, DATA.destination, DATA.currency, 3);
const parcels = [
{ length: 15, width: 10, height: 5, metricUnit: "CM", weight: 10, weightUnit: "KG", quantity: 2 },
// ... additional parcels
];
const rates = await fedex.getRates(parcels);
This example shows the instantiation of the FedEx
class with origin, destination, currency, and order processing days. The getRates
method calculates shipping rates for a series of parcels, each defined by their dimensions (in cm or inches), weight (in kg or lbs), and quantity.
Notes
- For metric system, the values for
metricUnit
andweightUnit
should be 'CM' and 'KG', respectively. - For imperial system, the values should be 'IN' and 'LB', respectively.
The format of the returned rates
is aligned with Shopify's shipping rates.
The rates are based on the information on FedEx website. Check the FedEx delivery service is available for the origin and destination region; unavailable service throws null
shipping price.
Sample AI Prompt
I'd like a shipping rate calculator that uses the FedEx rates.
The parcels will be ready for pick up in 3 days.
The dimensions and weight of the parcels are contained in the items metafield namespace object called "parcel". Get the first 15 values in the namespace.
It uses the metric unit system.
Sample AI Output
import { enrichItemDetails, FedEx } from './modules.js';
export async function calculateShippingRates(DATA, env) {
try {
// Enriching DATA with additional parcel information
DATA = await enrichItemDetails(DATA, [{ namespace: "parcel", size: 15 }]);
// Extracting parcels information from enriched DATA
const parcels = DATA.items.map(item => ({
length: item.metafields.parcel.length,
width: item.metafields.parcel.width,
height: item.metafields.parcel.height,
metricUnit: "CM", // Using metric units
weight: item.metafields.parcel.weight,
weightUnit: "KG", // Weight in kilograms
quantity: item.quantity
}));
// Setting up FedEx module with necessary arguments
const fedex = new FedEx(DATA.origin, DATA.destination, DATA.currency, 3);
// Fetching rates from FedEx
const rates = await fedex.getRates(parcels);
// Returning rates in the expected Shopify format
return { rates };
} catch (error) {
// In case of any error, return a rate with price equals to null and other fields set to empty string
return {
rates: [{
service_name: "",
service_code: "",
total_price: null,
description: "",
currency: "",
min_delivery_date: "",
max_delivery_date: ""
}]
};
}
}