Thursday, 18 September 2014

Fedex API integration in rails

Introduction to Fedex
FedEx Corporation is an American global courier delivery services company headquartered in Memphis, Tennessee.

With FedEx Web Services, we can integrate FedEx Express®, FedEx Ground® and FedEx Freight® shipping services into our business systems, retail website or order management system. FedEx Web Services is the next generation shipping API for integrating software applications with FedEx Systems to create shipping labels, facilitate returns, track shipments, obtain rate quotes and generate reports.

Fedex API integration in rails 4

We can integrate Fedex API with gem 'fedex' in rails .

It uses Fedex API Shipment version 13. This version uses the Non-SOAP Web Services so there is no need to download the Fedex WSDL files, note however that you will need to apply for development/production credentials.


Include gem 'fedex' in Gemfile.rb .

Run $bundle install .

How to use?
Basically we use some shipping destination where to ship and the sender's destinations from where we want to ship. We need to provide package informations . The package informations consists of

Package Dimensions with parameters

  • height
  • length
  • width
These informations need to give in inches or centimeters .

Package Weight parameter
  • weight with lbs

We will send a request to the fedex API to get rate from fedex. Later we will get a request to get label from fedex. The label will give a report with bar code with valid fedex tracking number.

Using Rails gem how will acomplish this will described here .....

Defining a Test Shiper

shipper = { :name => "Test Fedex Sender",
:company => "Home",
:phone_number => "121-333-2332",
:address => "Main Street",
:city => "Littleton",
:state => "CO",
:postal_code => "80125",
:country_code => "US" }

Defining a test Recipient

recipient = { :name => "Test Fedex Recipient",
:company => "Home",
:phone_number => "555-555-5555",
:address => "Main Street",
:city => "Boulder",
:state => "CO",
:postal_code => "80304",
:country_code => "US",
:residential => "false" }

Defining the packages; multiple packages in a single shipment are also allowed (all the dimensions must be integers).

packages = []
packages << {
:weight => {:units => "LB", :value => 2},
:dimensions => {:length => 10, :width => 5, :height => 4, :units => "IN" }
packages << {
:weight => {:units => "LB", :value => 6},
:dimensions => {:length => 5, :width => 5, :height => 4, :units => "IN" }

By default the packaging type will be “YOUR PACKAGING” and the dropoff type will be “REGULAR PICKUP”

Initializing shipping options

shipping_options = {
:packaging_type => "YOUR_PACKAGING",
:drop_off_type => "REGULAR_PICKUP"

Creating a Fedex::Shipment object using your FedEx credentials; mode should be either production or development depending on what Fedex environment you want to use.

Include fedex in your ruby file
require 'fedex'

Initializing Fedex::Shipment

fedex = => 'xxx',
:password => 'xxxx',
:account_number => 'xxxx',
:meter => 'xxx',
:mode => 'development')

Getting Shipment Rates

rate = fedex.rate(:shipper=>shipper,
:recipient => recipient,
:packages => packages,
:service_type => "FEDEX_GROUND",
:shipping_options => shipping_options)

This will provide a response as
# Complete response
$ <Fedex::Rate:0x1019ba5f8
@total_billing_weight="8.0 LB",

$ rate.total_net_charge => "34.03"

Fedex provides multiple total values; total_net_charge is the final amount we are looking for.

We will then find the suitable way to pay the fedex price and continue shipping.

In the next blogs we will read about the fedex label, cancelling fedex shipment, verifying address with fedex.

No comments:

Post a Comment