Integration Examples
Learn how to integrate delivery-cli into your applications and scripts.
Bash Script Example
#!/bin/bash
# find-and-reserve.sh - Find nearest locker and create reservation
set -e
# Configuration
LAT=40.7128
LON=-74.0060
ORDER_ID="ORD-$(date +%s)"
# Find nearest available locker
echo "Finding nearest available locker..."
MACHINE_ID=$(delivery-cli search-machines \
--latitude $LAT \
--longitude $LON \
--radius 5000 \
--only-available-lockers \
--limit 1 | jq -r '.machines[0].machine.machine_id')
if [ -z "$MACHINE_ID" ]; then
echo "No available lockers found"
exit 1
fi
echo "Found locker: $MACHINE_ID"
# Create reservation
echo "Creating reservation..."
RESERVATION=$(delivery-cli create-reservation \
--machine-id "$MACHINE_ID" \
--platform-order-id "$ORDER_ID" \
--packaging '{"width":300,"height":300,"depth":300}')
RESERVATION_ID=$(echo "$RESERVATION" | jq -r '.reservation_id')
DROPOFF_CODE=$(echo "$RESERVATION" | jq -r '.drop_off_code')
echo "Reservation created!"
echo "Reservation ID: $RESERVATION_ID"
echo "Drop-off code: $DROPOFF_CODE"
Python Integration
#!/usr/bin/env python3
import subprocess
import json
def run_delivery_cli(command, **kwargs):
"""Run delivery-cli command and return JSON output."""
cmd = ['delivery-cli', command]
for key, value in kwargs.items():
cmd.extend([f'--{key.replace("_", "-")}', str(value)])
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return json.loads(result.stdout)
# Search for machines
machines = run_delivery_cli('search-machines',
latitude=40.7128,
longitude=-74.0060,
radius=5000,
only_available_lockers=True
)
# Get first available machine
if machines['machines']:
machine_id = machines['machines'][0]['machine']['machine_id']
# Create reservation
reservation = run_delivery_cli('create-reservation',
machine_id=machine_id,
platform_order_id='PYTHON-001',
packaging='{"width":300,"height":300,"depth":300}'
)
print(f"Created reservation: {reservation['reservation_id']}")
Node.js Integration
const { execSync } = require('child_process');
function deliveryCli(command, options = {}) {
const args = [command];
Object.entries(options).forEach(([key, value]) => {
const flag = key.replace(/_/g, '-');
args.push(`--${flag}`, value);
});
const output = execSync(`delivery-cli ${args.join(' ')}`, { encoding: 'utf8' });
return JSON.parse(output);
}
// Search for machines
const machines = deliveryCli('search-machines', {
latitude: 40.7128,
longitude: -74.0060,
radius: 5000,
'only-available-lockers': true
});
// Create reservation
if (machines.machines.length > 0) {
const machineId = machines.machines[0].machine.machine_id;
const reservation = deliveryCli('create-reservation', {
'machine-id': machineId,
'platform-order-id': 'NODE-001',
packaging: JSON.stringify({ width: 300, height: 300, depth: 300 })
});
console.log(`Reservation created: ${reservation.reservation_id}`);
}