FedEx REST API client for OAuth tokens and Rates and Transit Times.
$ npm install @stores.com/fedex
const FedEx = require('@stores.com/fedex');
const fedex = new FedEx({
api_key: 'your_api_key',
secret_key: 'your_secret_key'
});By default the client targets the production endpoint (https://apis.fedex.com). To point at the sandbox, pass url explicitly:
const fedex = new FedEx({
api_key: 'your_api_key',
secret_key: 'your_secret_key',
url: 'https://apis-sandbox.fedex.com'
});FedEx APIs use the OAuth 2.0 protocol for authentication and authorization using the client_credentials grant type. Tokens are cached in-process per API key until shortly before they expire.
See: https://developer.fedex.com/api/en-us/catalog/authorization.html
const accessToken = await fedex.getAccessToken();
console.log(accessToken);
// {
// access_token: '...',
// expires_in: 3600,
// scope: 'CXS',
// token_type: 'bearer'
// }Request rate quotes and transit times from FedEx. The caller supplies the full request body — accountNumber, requestedShipment, and any of rateRequestControlParameters, carrierCodes, processingOptions, version — and the package forwards it verbatim.
See: https://developer.fedex.com/api/en-us/catalog/rate/v1/docs.html
const json = await fedex.rateAndTransitTimes({
accountNumber: { value: 'your_account_number' },
rateRequestControlParameters: {
rateSortOrder: 'SERVICENAMETRADITIONAL',
returnTransitTime: true,
servicesNeededOnRateFailure: true
},
requestedShipment: {
packagingType: 'YOUR_PACKAGING',
pickupType: 'USE_SCHEDULED_PICKUP',
recipient: {
address: { countryCode: 'US', postalCode: '10001' }
},
requestedPackageLineItems: [{ weight: { units: 'LB', value: 5 } }],
serviceType: 'FEDEX_GROUND',
shipper: {
address: { countryCode: 'US', postalCode: '38116' }
}
}
});
const detail = json.output.rateReplyDetails[0].ratedShipmentDetails[0];
console.log(detail.totalNetCharge);
// 24.17Non-2xx responses reject with HttpError. If FedEx returns a 200 response carrying a non-empty errors[] envelope, the call rejects with an HttpError whose message is every message joined by ; and whose .json is the full response body (with the errors[] array, codes, and any other fields).