Integration of Delivery Cost Calculation via API
A customer adds a product to the cart, proceeds to checkout — and the delivery cost turns out unexpectedly high. They abandon the cart. Sound familiar? We solve this by automatically calculating delivery cost via API, showing real rates at the selection stage. Our experience: years of expertise and 15+ successful integrations for e-commerce stores of all sizes.
Imagine: the customer has already chosen a product, filled in the details, but at the delivery selection step they see a message "calculate delivery separately." That makes them leave. Our solution shows accurate cost and delivery time from multiple providers right in the cart, in real time. We integrate APIs of CDEK, Boxberry, Russian Post, and other services, unify their responses, and cache results for fast loading. With parallel asynchronous requests, the user waits no more than 2–3 seconds. This reduces cart abandonment by 20–30%.
Problems We Solve
Implementing delivery calculation via API is not just "calling a provider endpoint." Here are typical challenges:
- N+1 queries: if you poll each provider sequentially, waiting time can exceed 10 seconds. We use parallel asynchronous requests, reducing delay to the response time of the slowest provider.
- Timeouts and errors: APIs may be unavailable. We set a 2-second timeout per request. If a provider is silent, its option is simply not shown.
- Non-unified formats: each service has its own response structure. We convert them to a single
DeliveryOptionobject, convenient for both backend and frontend. - Caching: repeating the same request should not hammer external APIs. We cache the result for 15 minutes, speeding up returning to the checkout page.
How Parallel API Polling Works
At the core is a DeliveryCalculator component that gets a list of providers and runs calculations in parallel. For async work we use Guzzle Pool or ReactPHP. Here's an example implementation:
class DeliveryCalculator { private array $providers; public function calculate(Cart $cart, Address $destination): Collection { $requests = collect($this->providers)->map(function ($provider) use ($cart, $destination) { return $provider->calculateAsync($cart, $destination); // returns Promise }); return collect(async_all($requests)) // parallel execution ->flatten() ->sortBy('price') ->filter(fn($option) => $option->isAvailable()); } } Each provider returns a DeliveryOption object with a unified structure:
class DeliveryOption { public string $providerId; // 'cdek', 'boxberry', 'pochta' public string $serviceCode; // 'cdek_express', 'cdek_pvz' public string $name; // 'CDEK: Express' public string $type; // 'courier' | 'pvz' | 'postamat' public int $price; // in cents public ?int $priceWithDiscount; public int $minDays; public int $maxDays; public ?string $pvzCode; // if a pickup point needs to be selected public array $meta; // additional provider data } Why Caching Calculation Results Matters
Caching reduces load on provider APIs and speeds up repeat requests. We use a key {cart_hash}:{destination_hash} with a TTL of 10–15 minutes. When the cart contents or address change, the cache is invalidated:
$cacheKey = "delivery:{$cart->hash()}:{$destination->hash()}"; return Cache::remember($cacheKey, 900, fn() => $this->fetchFromProviders($cart, $destination)); What the Work Includes
| Stage | Content | Time (work days) |
|---|---|---|
| Audit | Analysis of current cart and available providers | 0.5 |
| API connection | Integration of 2–3 providers (CDEK, Boxberry, Russian Post) | 2–3 |
| Parallel requests | Implementing async polling, timeouts, error handling | 1–2 |
| Caching | Setting up cache and invalidation on cart events | 0.5 |
| UI | Displaying options with pickup point selection on a map, delivery time | 1–2 |
| Testing | Verification with real orders, performance testing | 1 |
Total: from 3 business days for basic integration.
Comparison of Approaches
| Criterion | Manual entry | API calculation (our solution) |
|---|---|---|
| Rate accuracy | Low (outdated) | High (real-time) |
| Number of providers | No more than 1–2 | 5 or more |
| Dimension handling | No | Automatic (from product card) |
| Errors | High probability | Minimized (timeouts, caching) |
| Conversion | Low | 20–30% higher |
Typical Integration Mistakes
- Not handling product dimensions. If dimensions are missing, we use defaults — otherwise the provider may reject or overcharge.
- Ignoring the production calendar. A delivery time of "1–2 days" without accounting for holidays misleads the customer. We tie to business days.
- Not testing with real carts. Often an error appears only with a large number of items or non-standard addresses.
According to CDEK documentation, the request timeout should not exceed 5 seconds.
What You Get After Integration
- Working cost calculation for 2–3 providers
- Parallel requests with timeouts
- Caching with automatic invalidation
- Display of options on the site with pickup point selection and delivery times
- Code and access documentation
- 30-day warranty on integration after delivery
Contact us to assess your project. We'll provide a detailed estimate of timeline and cost, and advise on provider selection. Request a consultation on delivery calculation integration — we'll help find the optimal solution.







