DEV Community

Cover image for Supercharge Your PHP Shipping Integrations with the New ShipTime API Client
Igor
Igor

Posted on

Supercharge Your PHP Shipping Integrations with the New ShipTime API Client

Shipping can be a major headache for developers building e-commerce sites and business applications. Juggling multiple carrier APIs, calculating rates, printing labels, and tracking shipments often leads to complex and brittle integrations. But what if you could manage it all through a single, streamlined platform?

Enter ShipTime, an all-in-one shipping service designed to simplify logistics for businesses. ShipTime provides a unified platform to compare rates from major carriers, print labels, and manage all your shipments in one place, often at discounted prices [1].

To make integration even easier for PHP developers, I'm excited to introduce a new, modern PHP client library for the ShipTime REST API: tigusigalpa/shiptime-php [2].

This library is built from the ground up for PHP 8.1+ and offers seamless integration with Laravel, empowering you to add robust shipping capabilities to your applications with minimal effort.

Why a New Client Library?

While ShipTime offers a powerful REST API [3], integrating it directly can still involve a lot of boilerplate code for handling HTTP requests, authentication, and data mapping. This library abstracts away that complexity, providing a clean, object-oriented interface that feels right at home in any modern PHP project.

Key Features

  • πŸš€ Modern PHP 8.1+: Leverages the latest language features like enums, readonly properties, and strict typing for more reliable and maintainable code.
  • πŸ”§ Full Laravel Integration: Includes a service provider and facade for effortless integration into your Laravel applications. Configuration is a breezeβ€”just add your API keys to your .env file.
  • πŸ“¦ Complete API Coverage: Provides access to all major ShipTime API features, including Rates, Shipments, Tracking, Orders, and Pickups.
  • 🎨 Clean Architecture: Designed with a service-oriented architecture and comprehensive Data Transfer Objects (DTOs) for a clean, type-safe, and intuitive developer experience.
  • πŸ›‘οΈ Robust Error Handling: Comes with specific exception types for different API errors, making debugging and error handling straightforward.

Getting Started

Integrating the library into your project is simple. First, install it via Composer:

composer require tigusigalpa/shiptime-php
Enter fullscreen mode Exit fullscreen mode

For Laravel Users

If you're using Laravel, the service provider will be automatically registered. All you need to do is publish the configuration file and add your credentials:

php artisan vendor:publish --tag=shiptime-config
Enter fullscreen mode Exit fullscreen mode

Then, add your ShipTime API key and secret to your .env file:

SHIPTIME_API_KEY=your_api_key
SHIPTIME_API_SECRET=your_api_secret
Enter fullscreen mode Exit fullscreen mode

You can now inject the ShipTime client directly into your controllers or use the ShipTime facade:

use Tigusigalpa\ShipTime\Facades\ShipTime;

// Get shipping rates for a package
$rates = ShipTime::rates()->getRates($rateRequest);
Enter fullscreen mode Exit fullscreen mode

Standalone PHP

Not using Laravel? No problem. You can use the client in any PHP project by instantiating it directly:

use Tigusigalpa\ShipTime\ShipTime;

$shiptime = new ShipTime(
    apiKey: 'your_api_key',
    apiSecret: 'your_api_secret'
);

// Create a shipment
$shipment = $shiptime->shipments()->createShipment($shipRequest);
Enter fullscreen mode Exit fullscreen mode

Example: Fetching Shipping Rates

Let's see how easy it is to get shipping rates. The library uses DTOs to ensure all data sent to the API is correctly structured and validated.

use Tigusigalpa\ShipTime\DTOs\RateRequest;
use Tigusigalpa\ShipTime\DTOs\Address;
use Tigusigalpa\ShipTime\DTOs\LineItem;
use Tigusigalpa\ShipTime\Enums\PackageType;
use Tigusigalpa\ShipTime\Enums\UnitOfMeasurement;

$from = new Address(/* ... */);
$to = new Address(/* ... */);
$lineItem = new LineItem(length: 12.0, width: 8.0, height: 6.0, weight: 5.5);

$rateRequest = new RateRequest(
    from: $from,
    to: $to,
    packageType: PackageType::PACKAGE,
    lineItems: [$lineItem],
    unitOfMeasurement: UnitOfMeasurement::IMPERIAL,
    shipDate: '2024-02-20'
);

$rateResponse = ShipTime::rates()->getRates($rateRequest);

foreach ($rateResponse->availableRates as $quote) {
    echo "Carrier: {$quote->carrierName} - {$quote->totalCharge->amount} {$quote->totalCharge->currency->value}\n";
}
Enter fullscreen mode Exit fullscreen mode

In just a few lines of code, you can retrieve a list of available shipping options and their costs, ready to be displayed to your users.

Final Thoughts

The shiptime-php library is designed to make integrating with ShipTime a fast and enjoyable experience for PHP developers. By providing a modern, type-safe, and well-structured client, it allows you to focus on building great features for your users instead of wrestling with API integration details.

Give it a try in your next project! The library is open-source and available on GitHub. Contributions, feedback, and feature requests are always welcome.

Happy shipping!


References

[1] ShipTime | Find the Cheapest Shipping Rates
[2] GitHub - tigusigalpa/shiptime-php
[3] ShipTime API Documentation

Top comments (0)