// The minimal valid body: one vehicle, one order, every required field
// filled in. See the POST /jobs reference for the full field list.
const body = {
external_reference: "your-own-id",
optimization_options: {
delivery_mode: "1",
kind_of_plan: "1",
tour_mode: "1;0;0;0;0;0",
},
vehicles: [
{
external_vehicle_id: "veh-01",
resource_id: 1,
from_address: "118 Tay Son, Dong Da, Ha Noi",
from_lat: 21.0122,
from_lon: 105.8252,
from_working_time: "2026-09-04T07:00:00+07:00",
to_working_time: "2026-09-04T18:00:00+07:00",
handling_unit_ids: [1],
height_of_vehicle: 180,
length_of_vehicle: 420,
width_of_vehicle: 190,
vehicle_tonnage: 1500,
},
],
orders: [
{
external_order_id: "ord-01",
transport_mode: "lastmile",
handling_unit_id: 1,
service_id: 1,
pickup_address: "118 Tay Son, Dong Da, Ha Noi",
pickup_lat: 21.0122,
pickup_lon: 105.8252,
pickup_date_time: "2026-09-04T08:00:00+07:00",
pickup_date_time_to: "2026-09-04T09:00:00+07:00",
drop_address: "72 Nguyen Trai, Thanh Xuan, Ha Noi",
drop_lat: 20.9971,
drop_lon: 105.8032,
drop_date_time: "2026-09-04T09:30:00+07:00",
drop_date_time_to: "2026-09-04T11:00:00+07:00",
rlength: 40,
rwidth: 30,
rheight: 30,
rweight: 5,
count_of_parcels: 1,
},
],
};
const idempotencyKey = crypto.randomUUID();
async function submit(body: unknown) {
const response = await fetch(
"https://<your-host>/api/v1/partners/addons/route-optimization/jobs",
{
method: "POST",
headers: {
"api-key": process.env.ROUTE4GREEN_API_KEY!,
"Idempotency-Key": idempotencyKey,
"content-type": "application/json",
},
body: JSON.stringify(body),
},
);
// 202 Accepted. The job is recorded, not finished.
const envelope = await response.json();
if (!envelope.success) {
throw new Error(`${envelope.error_code}: ${envelope.message}`);
}
return envelope.data;
}