1. Go to this page and download the library: Download sashalenz/nova-poshta-api 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/ */
sashalenz / nova-poshta-api example snippets
use Sashalenz\NovaPoshtaApi\ApiModels\Address;
// Uses NOVA_POSHTA_API_KEY from env
$cities = Address::make()->getCities($request);
// Uses a different counterparty's key for this call only
$senderInvoices = InternetDocument::make($sender->np_api_key)->getDocumentList($request);
use Sashalenz\NovaPoshtaApi\ApiModels\Address;
use Sashalenz\NovaPoshtaApi\ApiModels\Address\RequestData\GetCitiesRequest;
$cities = Address::make()
->cache(60 * 60) // optional: cache the response for 1 hour
->getCities(GetCitiesRequest::from([
'limit' => 20,
'findByString' => 'Київ',
]));
foreach ($cities as $city) {
echo "{$city->ref} {$city->description}\n";
}
use Sashalenz\NovaPoshtaApi\ApiModels\Address;
use Sashalenz\NovaPoshtaApi\ApiModels\Address\RequestData\GetWarehousesRequest;
$warehouses = Address::make()
->cache(60 * 60 * 24)
->getWarehouses(GetWarehousesRequest::from([
'cityRef' => $city->ref,
'limit' => 500,
]));
use Sashalenz\NovaPoshtaApi\ApiModels\InternetDocument;
use Sashalenz\NovaPoshtaApi\ApiModels\InternetDocument\RequestData\GetDocumentPriceRequest;
use Sashalenz\NovaPoshtaApi\Enums\CargoType;
use Sashalenz\NovaPoshtaApi\Enums\ServiceType;
$price = InternetDocument::make($sender->api_key)->getDocumentPrice(
GetDocumentPriceRequest::from([
'citySender' => $sender->city_ref,
'cityRecipient' => $recipientCityRef,
'weight' => 5.5,
'serviceType' => ServiceType::WAREHOUSE_WAREHOUSE,
'cargoType' => CargoType::CARGO,
'cost' => 1000,
'seatsAmount' => 1,
])
);
echo $price->cost; // UAH
use Sashalenz\NovaPoshtaApi\ApiModels\TrackingDocument\TrackingDocument;
use Sashalenz\NovaPoshtaApi\ApiModels\TrackingDocument\RequestData\GetStatusDocumentsRequest;
use Sashalenz\NovaPoshtaApi\ApiModels\TrackingDocument\RequestData\DocumentData;
$statuses = TrackingDocument::make()->getStatusDocuments(
GetStatusDocumentsRequest::from([
'documents' => [
DocumentData::from(['documentNumber' => '20400000000001', 'phone' => '380501234567']),
DocumentData::from(['documentNumber' => '20400000000002', 'phone' => '380501234567']),
],
])
);
foreach ($statuses as $status) {
echo "{$status->number}: {$status->status} (statusCode={$status->statusCode})\n";
}
// Cache cities for 24h — the directory rarely changes
Address::make()->cache(60 * 60 * 24)->getCities($request);
// Cache forever (until you flush manually)
Common::make()->cache()->getCargoTypes();
use Sashalenz\NovaPoshtaApi\Exceptions\NovaPoshtaApiUnavailableException;
use Sashalenz\NovaPoshtaApi\Exceptions\NovaPoshtaException;
try {
$cities = Address::make()->getCities($request);
} catch (NovaPoshtaApiUnavailableException $e) {
// NP is down — fall back to cached results, suppress UI noise
return $cachedCities;
} catch (NovaPoshtaException $e) {
// Something is wrong with our request — log and surface to the user
report($e);
return collect();
}