PHP code example of its-pradeependra / postpin-php

1. Go to this page and download the library: Download its-pradeependra/postpin-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

its-pradeependra / postpin-php example snippets



Postpin\Client;

$postpin = new Client(getenv('POSTPIN_API_KEY'));

$rate = $postpin->rates->calculate([
    'origin'      => '400001', // Mumbai
    'destination' => '110001', // New Delhi
    'weight'      => 1200,      // grams
    'service'     => 'express', // "surface" | "express" | "same_day"
]);

echo $rate['total'] . ' ' . $rate['currency']; // 254.38 INR
print_r($rate['etaDays']);                      // [1, 3]

$postpin = new Client($apiKey, [
    'base_url'    => 'https://api.postpin.in/v1', // override the API base (incl. version)
    'timeout'     => 30.0,                         // per-request timeout in seconds
    'max_retries' => 2,                            // retries on 429/5xx/network errors
    'headers'     => ['X-Team' => 'logistics'],    // extra headers on every request
]);

// Rates
$rate = $postpin->rates->calculate([
    'origin' => '400001', 'destination' => '781001', 'weight' => 2500,
    'length' => 30, 'width' => 20, 'height' => 15,  // cm — enables volumetric weight
    'cod' => true, 'declared_value' => 4999,        // rupees
    'idempotency_key' => 'order-8f3a-rate',         // optional; auto-generated otherwise
]);

// Serviceability
$check = $postpin->serviceability->check('781001');

// Pincodes
$pin    = $postpin->pincodes->get('302001');
$states = $postpin->pincodes->states();

// Plans
$plans = $postpin->plans->list();

use Postpin\Exception\RateLimitException;
use Postpin\Exception\QuotaExceededException;
use Postpin\Exception\ValidationException;
use Postpin\Exception\AuthenticationException;

try {
    $postpin->rates->calculate([...]);
} catch (RateLimitException $e) {
    echo "Rate limited — retry after {$e->retryAfter}s";
} catch (QuotaExceededException $e) {
    echo "Monthly quota exhausted — upgrade your plan";
} catch (ValidationException $e) {
    echo "Bad request: {$e->getMessage()}";
    var_dump($e->details);
} catch (AuthenticationException $e) {
    echo "Invalid API key";
}

use Postpin\Webhooks;
use Postpin\Exception\SignatureVerificationException;

$payload = file_get_contents('php://input');
$sig     = $_SERVER['HTTP_X_POSTPIN_SIGNATURE'] ?? null;

try {
    $event = Webhooks::constructEvent($payload, $sig, getenv('POSTPIN_WEBHOOK_SECRET'));
} catch (SignatureVerificationException $e) {
    http_response_code(400);
    exit;
}

if ($event['event'] === 'rate.calculated') {
    // handle $event['data']
}
http_response_code(200);
bash
composer