Your first shipment

From zero to shipment in less than 50 lines of code

Getting Started

In this example, we will use Guzzlearrow-up-right, a popular HTTP client written in PHP.

First, start with installing Guzzle to your project using Composer. At time of writing, version 7.0 is the latest stable. If you're using Laravelarrow-up-right, a popular PHP framework, then Guzzle is already installedarrow-up-right and Laravel actually provides an expressive API on top of it.

composer require guzzlehttp/guzzle:^7.0

Once you're done, start a new php file:

my_first_shipment.php
<?php

Pre-requisites

Before you can start issuing commands and creating shipments! we need you to have the following ready:

circle-info

Your API token is a JSON Web Tokenarrow-up-right, which a standard and secure form of authentication and authorization

triangle-exclamation
  • Your Service Variables: Depending on your account type, contract, available rates and your geo location, there are one or more services available for you. Refer to eshiparrow-up-rightAPI pagearrow-up-right for more details. Every request must have at least one "Base Product" associated. The value section represent the UUID or unique identifier of the service you want to request, which you will need later

circle-info

UUIDs or Universally Unique IDentifiersarrow-up-right are unique values we use to represent a certain element in our systems. These values rarely change, and it's safe to cache and re-use them

  • Collection Address: Every address you enter to our system will be given a unique identifier, using the same UUID format explained above. Many customers have only a single collection address. The value of that address is also unique, and it doesn't change with minor modifications. However, if you're planning to change your collection address completely, please create a new one.

Other considerations

To be updated

Setting up the client

Now that we have the initial variables handy, let's create the HTTP client object:

A few observations

  • $api_base is the main API URL, you can get this value from the API Docs, for the majority of the customers, it's the one mentioned above

  • $api_token is the variable holding your API Token, you can inject it the way you want, including hardcoding it temporarily

  • base_uri allows us to tell the client about the main URL and use relative endpoints thereafter

  • headers is where we configure the client to use the default method of auth for all subsequent requests, using the Authorization header, and make sure to send JSON requests to all endpoints.

The first test

Next, we'll make the very first request, which will return back the account info and confirm that we're able to connect and authenticate:

A typical response to this request is something along these lines:

This is a standard, successful JSON response. And while some response fields were omitted for brevity, you get the idea.

If you get GuzzleHttp\Exception\ClientException or with a message starting with Cannot Authenticate: or a status code 401, then you need to double check your API Token

To inspect the response and get the results in PHP object (or array) format, use the following snippet

It is important though to check for errors before proceeding with parsing the response body:

The 1st shipment

Okay, so far we have the client ready and the first request successful, let's dive into creating an actual shipment, well, maybe a test one first.

Full example code

Follow the comments for a quick explanation, this example will ultimately create an actual shipment if you remove the test field from the request.

Happy shipping!!

Last updated