Connect EasyPost API - Picture №1
01

Connect EasyPost API

Content:
Rate this article
Thank you for feedback!
0.0
215 Views

EasyPost is a logistics platform that provides unique tools and a convenient API for automating and optimizing the shipping and tracking processes of shipments. Its history began in 2012, and today EasyPost holds a leading position among providers of efficient logistics solutions, serving numerous business industries.
One of the key advantages of the EasyPost API is the ability for companies to access a diverse range of carriers and choose the best delivery options for their shipments. EasyPost offers comprehensive functionality with a multitude of features, which we will describe below.



The main advantages of using EasyPost are as follows:

  • EasyPost provides a convenient API that allows you to get accurate shipping cost calculations right on your website. Now your customers or managers can immediately see the shipping cost and choose the most suitable option.
  • With EasyPost, you can easily create and print labels for your shipments. This will save you time and simplify the process of preparing your packages for shipping.
  • EasyPost integrates with a variety of popular carriers, including FedEx, DHL, USPS, UPS, APC, and others. You'll have access to various delivery options and can choose the best one for your customers.
  • Using EasyPost's tracking API, you and your customers can receive real-time updates on the delivery status. This ensures transparency and builds trust in your business.
  • EasyPost provides the option to insure your shipments, protecting you from losses or damages during delivery. This way, you can have confidence in the safety of your shipments.

Example of EasyPost API Integration

For the most widely used programming languages, EasyPost provides SDKs. So, if you are using PHP, you can install the corresponding package with the following command:


composer require EasyPost/EasyPost-php


EasyPost uses only one parameter - the API key - to sign requests. Before starting to work with the client code, you need to first import EasyPost:


use EasyPost;


All classes are located within the name space EasyPost, so to set up the API key for signing requests, you need to do the following:


EasyPost\EasyPost::setApiKey($apiKey);


Now you can perform various requests, for example, to create an address. Some systems, like ShipStation, may dynamically accept only the recipient's address, while the sender's address is taken from the account settings. In the case of EasyPost, you need to provide both addresses. Let's assume that your system already has an entity called "order" with fields responsible for the delivery address. In this case, the code for saving the address in EasyPost will look like this:


$receiverAddress = EasyPost\Address::create([
   'verify' => ['delivery'],
   'company' => $order->getCompany(),
   'street1' => $order->getStreet(),
   'street2' => $order->getHouseNumber(),
   'city' => $order->getCity(),
    'state' => $order->getUserState(),
   'zip' => $order->getZip(),
    'phone' => $order->getUserPhone(),
    'email' => $order->getEmail(),
    'country' => $order->getCountryCode(),
]);


As a result, in the response, we will receive the same array with additional fields, including "id" - the address identifier in the EasyPost system, "verifications" - an array of address verification results, and others. Similarly, you need to specify and save the sender's address in the variable $senderAddress.

Now, let's take a look at how to create the shipment itself:


$parcel = EasyPost\Parcel::create([
    'weight' => $order->getWeight(),
    'height' => $order->getHeight(),
   'width' => $order->getWidth(),
   'length' => $order->getLength(),
]);


We have three variables: $receiverAddress, $senderAddress, and $parcel. They represent instances of the EasyPost\Address and EasyPost\Parcel classes, respectively. The EasyPost package automatically encodes and decodes data into JSON format, so objects can be passed in arrays. For example, when we are ready to create a shipment, we need to pass all the retrieved objects to the method for its creation:


$shipment = EasyPost\Shipment::create([
    "to_address" => $fromAddress,
    "from_address" => $toAddress,
    "parcel" => $parcel,
    "options" => [
    "label_format" => 'PDF'
    ],
]);


If everything is fine, then in the "rates" field of the $shipment object, there will be an array of objects containing information about various delivery services. Let's assume that one of the carriers suits us, for example, USPS with the lowest shipping cost, and we want to purchase a label for this service and obtain a PDF file for printing. In this case, we need to pass the following value to the "buy" method of the $shipment object:


$shipment->buy($shipment->lowest_rate(['USPS']));


The "lowest_rate" method will find the cheapest delivery method among all the available ones. By default, the system provides a list of delivery methods for USPS until you connect the corresponding service. However, to create labels, you need to activate this service. Even if you are trying to get a USPS label, you must connect it beforehand.

Since all our objects have unique identifiers in the EasyPost system, we can save these identifiers and later retrieve information about the objects directly from EasyPost using the "retrieve" method for each of the mentioned classes: EasyPost\Shipment, EasyPost\Address, and EasyPost\Parcel. Additionally, EasyPost provides the option to create all four entities in one request by passing a multidimensional array to the create method of the EasyPost\Shipment class. For example, the "to_address" key will contain an array of recipient address data, and so on. However, the method described above allows for more flexible handling of validation errors that may arise.

Regarding validation, the EasyPost SDK handles errors automatically and generates exceptions that the client code developer will need to handle. However, error messages returned by the EasyPost server may not always be clear and may only contain information about invalid data for the entity being saved.

If you need to configure the connection to this API, please leave a request on the page "Integration and API Connection".

Next article Connecting to the Amazon API (MWS)
Previous article How to speed up website performance?
Let's discuss your project
By clicking the "Send" button, you consent to the processing of personal data. Learn more.
Blog
#0000

Articles You May Also Be Interested In

YII or Laravel — which is better?
02 Jan, 2024
Thanks to technological development, it is no longer necessary to write PHP code from scratch: there are many frameworks — ready-made models, "templates" for software platforms, among which Yii2 and Laravel became the most popular. In what their difference and which of the frameworks to choose for creating a product? We tell and compare the structures in this material.
VIEW ARTICLE
Connect EasyPost API - Picture №5
Adaptive website: Relevant and effective
24 Nov, 2023
Today, the pace of life is several times faster than a couple of decades ago, making time one of the most valuable resources. The existence of mobile gadgets gives us the opportunity to make the most of our time.
VIEW ARTICLE
Connect EasyPost API - Picture №6
How much does it cost to create an online store?
19 Oct, 2023
Creating an online store requires a serious approach, knowledge in web development, UI/UX design, server configuration, and database management.
VIEW ARTICLE
Our experience: catering and food delivery
22 Feb, 2022
In the field of public catering, especially when a company is popular, the systematization of orders and proposals for customers plays a crucial role.
VIEW ARTICLE
 
Contact us
#0000

Ready to start?
Let us know!

Address:

Ukraine, Zhytomyr
Vitruka Street, 9V

Shedule:

Mon-Fri, 9 am-7 pm

Address:

Poland, Warsaw, 00-842
Łucka street 15/204

Shedule:

Mon-Fri, 9 am-7 pm

 
 
Contact us
#0000

Have a question?

Please fill out the form below, and our specialists will contact you as soon as possible!
By clicking the "Send" button, you consent to the processing of personal data. Learn more.