Skip to content

stores-com/fedex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fedex

Test Coverage Status npm version License: MIT

FedEx REST API client for OAuth tokens and rate quotes.

Installation

$ npm install @stores.com/fedex

Usage

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

Documentation

Methods

getAccessToken()

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'
// }

rates(requestedShipment, options)

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.17

If 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.

About

FedEx REST API client (OAuth, rates)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors