Меню

Connect EasyPost API

logo
API EasyPost integration tips
Connect EasyPost API

API EasyPost integration tips

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

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.

How to create an electronic currency exchanger correctly 04.09.2019 How to create an electronic currency exchanger correctly
Tips for creating an online exchanger. What special attention should be paid to when creating an exchanger convenient for the user? Basic requirements for the code

Implementing a Connection to the Payoneer API 10.09.2019 Implementing a Connection to the Payoneer API
Connection to the Payoneer API payment system. Main connection environments. User’s registration/authorization through the provided link, list of users

Back to the list