FedEx REST API client for OAuth tokens and rate quotes.
$ npm install @stores.com/fedex
const FedEx = require('@stores.com/fedex');
const fedex = new FedEx({
account_number: 'your_account_number',
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({
account_number: 'your_account_number',
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 account number 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 for a shipment. The caller supplies only the requestedShipment body; the package fills the top-level accountNumber and rateRequestControlParameters envelope.
See: https://developer.fedex.com/api/en-us/catalog/rate.html
const body = await fedex.rates({
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 = body.output.rateReplyDetails[0].ratedShipmentDetails[0];
console.log(detail.totalNetCharge);
// 24.17If FedEx returns a 200 response carrying a non-empty errors[] envelope, the call rejects with Error('${code}: ${message}') from the first entry. Non-2xx responses reject with HttpError.