yu-dev / nova-poshta-api
PHP library for the Nova Poshta API: cities, branches and parcel lockers, streets, waybills, label printing, tracking, counterparties
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
- laminas/laminas-stdlib: ^3.11
- psr/log: ^1.1 || ^2.0 || ^3.0
Requires (Dev)
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^1.11 || ^2.0
- phpunit/phpunit: ^10.5
- psr/http-client: ^1.0
Suggests
- psr/http-client: To send requests through any PSR-18 client via Psr18Transport
- psr/http-factory: Required by Psr18Transport
Provides
None
Conflicts
None
Replaces
None
README
PHP library for the Nova Poshta API v2.0: cities, warehouses and postomats, streets, waybills (ТТН), print forms, tracking, sender counterparties.
- PHP 8.1+,
ext-curl,ext-json,laminas/laminas-stdlib,psr/log. No Guzzle, no HTTP discovery. - Models keep Nova Poshta field names:
$warehouse->CityRefor$warehouse['CityRef']; a missing field isnull. - Responses are cleaned: strings trimmed,
""and the zero GUID becomenull, zero weight/size/cost limits becomenull("no limit"), coordinates becomefloat.
Install
composer require yu-dev/nova-poshta-api
Usage
use Yu\NovaPoshtaApi\NovaPoshta; $np = NovaPoshta::create('your-api-key'); // Directory: one page, or everything page by page $page = $np->address()->getCities(['FindByString' => 'Київ'], 1, 20); foreach ($np->address()->iterateWarehouses(['CityRef' => $page->items[0]->Ref]) as $warehouse) { if ($warehouse->isSelectable()) { echo $warehouse->Description, ' ', $warehouse->getPostalCode(), PHP_EOL; } } // Streets (queries shorter than 2 characters return an empty page without a request) $streets = $np->address()->getStreet($cityRef, 'Хре');
Price and waybill
use Yu\NovaPoshtaApi\Enum\CargoType; use Yu\NovaPoshtaApi\Enum\PayerType; use Yu\NovaPoshtaApi\Enum\PaymentMethod; use Yu\NovaPoshtaApi\Enum\ServiceType; use Yu\NovaPoshtaApi\Model\InternetDocument; use Yu\NovaPoshtaApi\Model\Parameters; $price = $np->internetDocument()->getDocumentPrice(new Parameters([ 'CitySender' => $senderCityRef, 'CityRecipient' => $recipientCityRef, 'ServiceType' => ServiceType::WarehouseWarehouse, 'CargoType' => CargoType::Parcel, 'Weight' => 1.5, 'Cost' => 500, 'SeatsAmount' => 1, ])); echo $price->Cost; $document = new InternetDocument([ 'PayerType' => PayerType::Recipient, 'PaymentMethod' => PaymentMethod::Cash, 'DateTime' => new DateTimeImmutable('tomorrow'), // sent as d.m.Y 'CargoType' => CargoType::Parcel, 'ServiceType' => ServiceType::WarehouseWarehouse, // ... sender, recipient, weight, cost, description ]); $created = $np->internetDocument()->save($document); // never retried echo $created->IntDocNumber; // Print forms are downloaded on the server: the print URL contains the API key $pdf = $np->internetDocument()->printDocuments([$created->Ref]); file_put_contents('/path/to/' . $created->IntDocNumber . '.pdf', $pdf);
Tracking
// number => phone (optional); split into requests of 100 automatically $statuses = $np->tracking()->getStatusDocuments(['20450000000001' => '380500000000']); foreach ($statuses as $number => $status) { echo $number, ': ', $status->Status, $status->isFinal() ? ' (final)' : '', PHP_EOL; }
Any other method
$response = $np->client()->call('Common', 'getCargoTypes', [], retryable: true); $rows = $response->data;
Errors
All exceptions extend Yu\NovaPoshtaApi\Exception\NovaPoshtaException:
TransportException— network error, timeout, non-2xx HTTP status;InvalidResponseException— invalid JSON or missing data (e.g. a price request that returns no price);ApiException—success: falseor errors;getErrors(),getErrorCodes(),getModelName(),getCalledMethod().
Read calls are retried twice on transport errors (after 1 s and 2 s). Warnings are logged, not thrown. Logs carry the model, method, duration, success and error codes — never the API key or request data.
Transport and configuration
use Yu\NovaPoshtaApi\Client; use Yu\NovaPoshtaApi\Transport\CurlTransport; $transport = new CurlTransport(timeout: 5, connectTimeout: 2, proxy: null, caFile: null); $client = new Client('your-api-key', $transport, $psrLogger);
CurlTransport— default, ext-curl.Psr18Transport— any PSR-18 client (installpsr/http-clientandpsr/http-factory).FakeTransport— queued responses and recorded requests, for your own tests.
Tracking status codes: docs/tracking-statuses.md.
Detailed reference (API, models, enums, errors, recipes): AGENTS.md and docs/agents/.
Tests
composer install vendor/bin/phpunit vendor/bin/phpstan analyse
Author
Yuriy Akishin:
- 📧 Email: yuriy.akishin@gmail.com
- 💼 LinkedIn: https://www.linkedin.com/in/yuriyakishin/
- 💻 GitHub: https://github.com/yuriyakishin
- 🌐 Website: https://yu.net.ua/
License
MIT