img
scroll
#

Connect EasyPost API

Uk Ru En
Article
#0002

There is the Eaypost SDK for widespread programming languages.

So, if you use PHP you can install the SDK using the following composer command:

composer require EasyPost/EasyPost-php

Connect EasyPost API

The EasyPost service uses the only one parameter to sign requests - the API-key.
In client code at first you have to import the EasyPost SDK

use EasyPost;


All the SDK classes are in EasyPost namespace, so one have to do the following to set the API-key:

EasyPost\EasyPost::setApiKey($apiKey);


Now it is possible to make requests, for exaple to create an address. Some servises, such as ShipStation, you can only pass a receiver address to, but sender address the system will take right from your account, but sometimes it is not so clear even how to pass a reciever address ;). But EasyPost requires both receiver and sender addresses.


Let's suppose, that we already have an order entity and it has all the necessary fields the system need for delivery purposes. So that to store an address to the EasyPost service we can do the following

$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 we will recieve the same array as a response but with additional fields, including "id" - identifier of an address in the EasyPost database, "verifications" - verification results array and other.

In the same way one should define and store a sender address to the $senderAddress variable

Now we can create a parcel entity, that corresponds to the real parcel we will send

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


Here we have 3 parameters: $receiverAddress, $senderAddress and $parcel, which are the instances of classes EasyPost\Address and EasyPost\Parcel accordingly.

The EasyPost SDK package already encode an decode the data to json, so we can pass the entities to an array as they are For example now, when we are ready to create a shipment, we should pass all the recieved objects to the shipment creation method:

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


And, if everything is OK, the Shipment object will have the "rates" array as a field with instances of EasyPost\Rate class. If one of the rates matches our needs, for example the cheapest 'USPS' rate, and we want to buy a label for it and get a PDF file to print, then we should pass such kind $shipment->lowest_rate(['USPS']) value as a parameter to the EasyPost\Shipment::buy method:

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


where "lowest_rate" method will find the cheapest 'USPS' delivery rate, as wel as delivery method.


By default the EasyPost service provide USPS delivery methods, until your connect other delivery services accounts, but the creation of labels will work only for connected services. So if you want to create USPS label you have to connect your USPS service account to the EasyPost service.

All our entities now have Ids in the EasyPost database, so we can store these Ids in our database and later get the info about the entities right from the EasyPost service, using EasyPost\Shipment::retrieve, EasyPost\Address::retrieve and EasyPost\Parcel::retrieve methods (despite they share the common parent class, the parent has no retrieve method).

On the other hand you one can create all four entities in only one request, passing all the necessary data when call EasyPost\Shipment::create static method. For example the "to_address" key's value will contain the receiver address. But this method is not as convenient at validation error handling as previous one.

By the way, the EasyPost SDK handles errors itself and throws Exceptions, a client code programmer has to handle, but errror messages returned by the EasyPost service can be meaningless and can do not contain a proper description of a problem, but only say that entity is not valid, but do not say why.

Blog
#0002

Articles You May Also Be Interested In

Asabix Wins Clutch Award for Top Developer in Ukraine
Today, there’s about 200 million active websites and 1.8 billion web applications. These numbers continue to grow each day, adding more to the clutter. The only way to stand out from the crowd these days is through innovation and creativity—and that’s where we come in!
Paypal API Connection Features
Paypal API Connection Features
In this article we will consider the REST API, which allows to conveniently interact with all the Paypal entities.
Admitad API Connection Experience
Admitad API Connection Experience
Admitad is a global affiliate network with a large selection of affiliate programs, high rates, express payments, and many tools.
Connect EasyPost API
There is the Eaypost SDK for widespread programming languages.
Contact Us
#0013

Ready to Start? Let Us Know!

Address:

Ukraine, Zhytomyr
Vitruka Street, 9V

M-F, 9am — 7pm

Address:

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

M - F, 9am - 7pm

Contact Us
#0000

Have a Question?

Describe your problem, fill the form below, and our staff will help you!
Required field
Required field
Required field
Required field