Search by

yu-dev / nova-poshta-api

yuriyakishin

PHP library for the Nova Poshta API: cities, branches and parcel lockers, streets, waybills, label printing, tracking, counterparties

Package info

github.com/yuriyakishin/nova-poshta-api

pkg:composer/yu-dev/nova-poshta-api

Statistics

Installs: 5

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.1 2026-12-01 19:04 UTC

This package is auto-updated.

Last update: 2026-12-01 19:08:46 UTC


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->CityRef or $warehouse['CityRef']; a missing field is null.
  • Responses are cleaned: strings trimmed, "" and the zero GUID become null, zero weight/size/cost limits become null ("no limit"), coordinates become float.

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: false or 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 (install psr/http-client and psr/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:

License

MIT