A simple and effective API service for calculating shipping costs based on address coordinates and order parameters using Excel-based formulas for accurate pricing.
- Excel-based calculation - precise delivery cost calculation using real Excel formulas
- Automatic zone checking - single request for zone validation + cost calculation
- Minimum load guarantee - 74.3% minimum load ensures profitability for small orders
- Weight and volume support - calculates based on both weight (kg) and volume (m³)
- Express delivery option - priority delivery with 14.25% surcharge
- Round to 100 rubles - customer-friendly pricing
- Supabase integration - real-time zone validation via RPC
- Dual authentication - Basic Auth + API Key protection
- Comprehensive logging - detailed operation tracking
- TypeScript
- Express.js
- Supabase (for checking delivery zones)
- Docker
- Traefik (for proxying and SSL)
delivery-api-express/
├── src/
│ ├── models/ # Data types
│ ├── routes/ # API routes
│ ├── services/ # Business logic
│ ├── middleware/ # Middleware (authorization)
│ ├── utils/ # Utilities (logging)
│ ├── config/ # Configuration files
│ └── index.ts # Entry point
├── logs/ # Log directory
├── docs/ # Documentation
├── .env # Environment variables
├── docker-compose.yml # Docker Compose configuration
├── Dockerfile # Configuration for creating a Docker image
├── package.json # NPM dependencies
└── tsconfig.json # TypeScript configuration
The project has aliases for import paths, which makes the code more readable:
// Instead of relative paths
import { DeliveryCalculationRequest } from '../models/deliveryData'
// Using aliases
import { DeliveryCalculationRequest } from '@models/deliveryData'Available aliases:
@/*- access to any file from the src directory@models/*- access to data models@services/*- access to services@routes/*- access to API routes@middleware/*- access to middleware@utils/*- access to utilities@config/*- access to configuration files
- Clone the repository:
git clone https://github.com/Father1993/delivery-express-api.git
cd delivery-api-express
- Install dependencies:
npm install
- Run in development mode:
npm run dev
- The API will be available at: http://localhost:3000
The API service provides functionality for calculating delivery costs based on coordinates and order parameters. The service now supports automatic zone checking - you can calculate delivery costs with a single request.
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
API status |
| POST | /api/zone |
Checking the delivery zone |
| POST | /api/calculate |
Calculating the delivery cost |
- Send coordinates and order data to
/api/calculate - API automatically checks the delivery zone
- If in zone: calculates and returns delivery cost
- If out of zone: returns error with zone information
- First, check the coordinates via
/api/zone - Then use the received data to calculate the delivery via
/api/calculate
All requests to the API must include the x-api-key header with a valid API key.
x-api-key: YOUR_API_KEY
Additionally, you can configure Basic Authentication via the API_USERNAME and API_PASSWORD environment variables.
Checking the API status.
Response:
{
"status": "Ok",
"message": "The shipping calculation API service is running | Level",
"version": "1.0.0",
"timestamp": "2025-09-09T07:00:00.000Z"
}Checks if shipping is available at the specified coordinates via Supabase RPC.
Headers:
Content-Type: application/json
x-api-key: YOUR_API_KEY
Request:
{
"lat": 48.480223,
"lon": 135.071917
}Successful response:
{
"inZone": true,
"zoneName": "Central District",
"zoneData": [
{
"zone_name": "Central District",
"zone_description": "Primary Delivery Zone",
"city_id": 1
}
],
"timestamp": "2025-09-09T07:00:00.000Z"
}Response (out of zone delivery):
{
"inZone": false,
"error": "Address is outside the delivery zone",
"timestamp": "2025-09-09T07:00:00.000Z"
}Calculating delivery costs based on coordinates, order data, and delivery zone information.
Headers:
Content-Type: application/json
x-api-key: YOUR_API_KEY
Request (new way - automatic zone checking):
{
"lat": 48.480223,
"lon": 135.071917,
"order": {
"weight": 2.5,
"volume": 0.1,
"cost": 3200
}
}Request (legacy way - with zone info):
{
"lat": 48.480223,
"lon": 135.071917,
"order": {
"weight": 2.5,
"volume": 0.1,
"cost": 3200
},
"zoneInfo": {
"inZone": true,
"zoneName": "Central District"
}
}Response (successful):
{
"delivery_cost": 700,
"delivery_time": "1-2 дня",
"express_delivery_cost": 800,
"options": [
{
"name": "Экспресс доставка",
"cost": 800,
"description": "Приоритетная доставка"
}
],
"zoneInfo": {
"inZone": true,
"zoneName": "Зона доставки — Хабаровск"
}
}Response (out of zone):
{
"error": true,
"message": "Адрес находится вне зоны доставки",
"zoneInfo": {
"inZone": false,
"error": "Адрес находится вне зоны доставки"
}
}The service uses Excel-based formulas for precise delivery cost calculation:
- Vehicle capacity: 2000 kg weight, 9 m³ volume
- City speed: 20 km/h average
- Loading time: 0.5 hours maximum
- City distance: 7 km average route
- Minimum load: 74.3% (ensures profitability)
- Hourly rate: 850 rubles/hour
- Additional costs: 100 rubles
- Margin: 10%
- Express surcharge: 14.25%
- Load ratio:
MAX(weight/2000, volume/9) - Required trips:
CEIL(load_ratio) - Effective load:
MAX(load_ratio, 74.3%) - Loading time:
0.5 hours × effective_load - Delivery time:
(7 km ÷ 20 km/h) × effective_load - Base cost:
(loading_time + delivery_time) × 850 + 100 - Regular delivery:
ROUND(base_cost × 1.1 ÷ 100) × 100 - Express delivery:
ROUND(base_cost × 1.1425 × 1.1 ÷ 100) × 100
- Small order (0.1 kg, 0.1 m³): 700/800 rubles
- Medium order (1000 kg, 4 m³): 700/800 rubles
- Large order (3000 kg, 6 m³): 2500/2800 rubles
- Very large order (100000 kg): 39800/45500 rubles
# Supabase configuration
SUPABASE_URL=your_supabase_url
SUPABASE_ANON_KEY=your_supabase_anon_key
# API authentication
API_KEY_CS_CART=your_api_key
API_USERNAME=your_username
API_PASSWORD=your_password
# Logging
LOG_LEVEL=info
LOG_TO_CONSOLE=true
LOG_DIR=logsRun tests:
# Unit tests
npm test
# Tests with coverage
npm run test:coverage
# Watch mode
npm run test:watchBuild and run with Docker:
# Build image
docker build -t delivery-api .
# Run container
docker run -p 3000:3000 --env-file .env delivery-apiOr use Docker Compose:
docker-compose up -dCheck API status:
curl http://localhost:3000/Check delivery zone:
curl -X POST http://localhost:3000/api/zone \
-H "Content-Type: application/json" \
-H "x-api-key: your-api-key" \
-d '{"lat": 48.480223, "lon": 135.071917}'Calculate delivery (new way):
curl -X POST http://localhost:3000/api/calculate \
-H "Content-Type: application/json" \
-H "x-api-key: your-api-key" \
-H "Authorization: Basic $(echo -n 'username:password' | base64)" \
-d '{
"lat": 48.480223,
"lon": 135.071917,
"order": {
"weight": 2.5,
"volume": 0.1,
"cost": 3200
}
}'- 🎯 Simplified - one request instead of two
- 📊 Accurate - calculation using real Excel formulas
- 💰 Profitable - minimum 74.3% load for small orders
- ⚡ Fast - fewer network requests
- 🔄 Compatible - legacy code continues to work
- 🛡️ Secure - dual authentication system
- 📊 Monitored - comprehensive operation logging
This service provides a well-structured, scalable API for delivery cost calculation with automatic zone checking and precise Excel-based pricing.