Hey there, fellow developer! Ready to dive into the world of ShipStation API integration? We're going to use the awesome kield01/shipstation-php-sdk
package to make our lives easier. Buckle up, because we're about to streamline your shipping process like never before!
Before we jump in, make sure you've got:
Let's kick things off by installing our trusty SDK. Fire up your terminal and run:
composer require kield01/shipstation-php-sdk
Easy peasy, right?
Now, let's get those API credentials working for us:
use Kield01\ShipStation\ShipStationApi; $apiKey = 'your_api_key'; $apiSecret = 'your_api_secret'; $shipstation = new ShipStationApi($apiKey, $apiSecret);
Just like that, we're ready to rock and roll with ShipStation!
Want to fetch some orders? It's as simple as:
$orders = $shipstation->orders->getOrders(['orderStatus' => 'awaiting_shipment']);
Time to create a shipment:
$shipment = $shipstation->shipments->createShipment([ 'orderId' => 123456, 'carrierCode' => 'fedex', 'serviceCode' => 'fedex_2day', // ... other shipment details ]);
Labels are just a line of code away:
$label = $shipstation->shipments->createLabelForShipment($shipmentId);
ShipStation's got your back with webhooks. Here's a quick setup:
$webhook = $shipstation->webhooks->subscribeToWebhook('ORDER_NOTIFY', 'https://your-webhook-url.com');
Need to map some custom fields? We've got you covered:
$customField = $shipstation->customFields->createCustomField([ 'name' => 'Special Instructions', 'type' => 'text' ]);
Always be prepared! Wrap your API calls in try-catch blocks:
try { $orders = $shipstation->orders->getOrders(); } catch (\Exception $e) { // Handle the error like a boss error_log($e->getMessage()); }
Unit testing is your friend. Mock those API responses:
use PHPUnit\Framework\TestCase; use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Psr7\Response; class ShipStationTest extends TestCase { public function testGetOrders() { $mock = new MockHandler([ new Response(200, [], json_encode(['orders' => [/* mock data */]])) ]); $client = new Client(['handler' => $mock]); $shipstation = new ShipStationApi('fake_key', 'fake_secret', ['client' => $client]); $orders = $shipstation->orders->getOrders(); $this->assertNotEmpty($orders); } }
When you're ready to go live, remember:
And there you have it! You're now equipped to build a robust ShipStation integration. Remember, the kield01/shipstation-php-sdk
package is your new best friend, so don't be afraid to dive into its documentation for even more features.
Happy shipping, and may your packages always arrive on time! 📦🚚💨