Dynamically Route New E-Commerce Orders

See files for this Use Case on GitHub here

With Route4Me, you can dynamically route new e-commerce orders on the fly.

The main difference between an address and an order is the fact that an order isn't attached to a permanent customer in your Route4Me account, while an address is.

Description

If you have many new orders per day, you can upload them directly into our planner by browsing for a CSV file - which is simply a type of file (.csv) you can choose after you press "Save As" on Microsoft Excel (you can see a sample of a csv file here)

Of course, you can use other input formats too, including JSON file, Access DB, SQLite, MySql, Sql, etc. For simplicity's sake, we'll only be reviewing JSON and CSV options.

Solution

Create an Optimization

The cURL example below shows you how to create new optimized routes for this Use Case:

ParameterTypeDescription
api_keystringAPI KEY of the user
input dataPOST dataValid JSON object string. Click here to see the JSON Schema
1 @ECHO OFF
2 
3 SET url=http://www.route4me.com/api.v4/optimization_problem.php
4 SET apikey=11111111111111111111111111111111
5 
6 ECHO ON
7 curl -o file1.txt -g -k -X POST -d "@create_new_optimization_data.json" "%url%?api_key=%apikey%"
8 
9 timeout /t 30

You can expect an Optimization Problem (see link), which has generated some routes already. On Route4Me's map, you can see this as:

Review the orders to be added

Suppose you received new orders from customers to deliver cargo (see link for input data). On the map, these orders could be located as:

Note
The sample data on our GitHub repository is not constant. They belong to a demo account, which is used by many users for testing, so data changes regularly. For accurate testing of our API examples, you'll need to enter up-to-date information yourself.

The Optimization Problem is a set of addresses with parameters for generating optimal routes. You can view them in the sample mentioned above.

As for an order's input JSON data, there is one depot (without order ID) and 3 addresses (with order IDs) and parameters for route-generating options. This data should be sent as HTTP POST data.

Add orders to an Optimization

Apart from the POST data with orders, you should also send some parameters with the HTTP GET method:

  • api_key - put the API key you obtained from Route4Me here;
  • optimization_problem_id - put the ID of the Optimization Problem to which will be added new orders here;
  • redirect - this should be 0 in our case.

HTTP Parameters

Attribute Type Description HTTP
method
api_key string API KEY of the user GET
optimization_problem_id string Optimization Problem ID GET
redirect integer If equal to 1, will be redirected, if 0 - not GET
parameters Route Parameters Valid JSON object string. Click here to see the JSON Schema POST
addresses array Valid JSON string of Address object array. lick here to see the JSON Schema POST

HTTP Request

PUT
/api.v4/optimization_problem.php?api_key=...&optimization_problem_id=...&redirect=0



1 @ECHO OFF
2 
3 SET url=http://route4me.com/api.v4/optimization_problem.php
4 SET apikey=11111111111111111111111111111111
5 SET rdirect=0
6 SET optid=8034E6A76E8518D2F9E49171E6A89896
7 
8 ECHO ON
9 
10 :: The example refers to the process of adding an order to an optimization by sending HTPP PUT data
11 
12 curl -o file1.txt -g -X PUT -H "Content-Type: application/json" -d "@add_order_to_route_data.json" "%url%?api_key=%apikey%&redirect=%rdirect%&optimization_problem_id=%optid%"
13 
14 timeout /t 30

Result

See sample RESPONSE JSON data here

Table of the Response parameters

Attribute Type Description
optimization_problem_id string Optimization Problem ID
user_errors array String array
optimization_errors array String array
state integer Optimization problem state
created_timestamp integer A creation time of an Optimization Problem
scheduled_for integer Date order was scheduled for
optimization_completed_timestamp integer Optimization completion date
parameters Route Parameters Valid JSON object string. Click here to see the JSON Schema
sent_to_background boolean Background computing
addresses array Valid JSON string of Address object array. lick here to see the JSON Schema
routes array Valid JSON string of Route object array. lick here to see the JSON Schema
links array Valid JSON string of Link object array. lick here to see the JSON Schema

You can view this result on Route4Me's map as: