Sending requests using scripts
To work with the scripts, you'll need an API key. You can find your Calendar Planning API key in the service interface: go to Settings → Company.
Note
A Planning API key and a Calendar Planning API key are two different keys.
Specify the API key value in the APIKEY field.
You can send a request for running a task using the requests library.
You can also run a Calendar Planning task via the API using cURL. For more information, see API request.
Start planning with a script
To run a Calendar Planning task via the API:
-
Create
request.jsonand place the request body in it. For the JSON request structure, see the specification.Example of a planning request body
You can download the example here.
-
In the same folder, create a file named
script.pyand copy the following Python script into it:#!/usr/bin/env python3 import hashlib import hmac import json import requests import sys import time import urllib3 from urllib.parse import urlencode urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) APIKEY = '<your API key>' SOLVER_URL = 'https://courier.yandex.com' USER_AGENT = 'RouteQ Support Agent/1.0' CALENDAR_PLANNING_URI = '/vrs/api/v1/calendar_planning/tasks' def make_request(method, uri, **kwargs): body = json.dumps(kwargs['json']) if kwargs.get('json') else '' url = SOLVER_URL + uri headers = { 'User-Agent': USER_AGENT } print(f"{method} {url}") response = requests.request(method, url, verify=False, headers=headers, **kwargs) return response def add_task(task): params = {'apikey': APIKEY} uri = f"{CALENDAR_PLANNING_URI}?{urlencode(params)}" response = make_request('POST', uri, json=task) response.raise_for_status() j = response.json() return j['id'] def get_status(task_id): params = {'apikey': APIKEY} uri = f"{CALENDAR_PLANNING_URI}/{task_id}/status?{urlencode(params)}" response = make_request('GET', uri) response.raise_for_status() j = response.json() print(j) return j['status'] def get_result(task_id): params = {'apikey': APIKEY} uri = f"{CALENDAR_PLANNING_URI}/{task_id}/result?{urlencode(params)}" response = make_request('GET', uri) response.raise_for_status() j = response.json() return j def main(): with open(sys.argv[1]) as fd: task = json.load(fd) task_id = add_task(task) while get_status(task_id) != 'completed': time.sleep(2) result = get_result(task_id) print(json.dumps(result)[:100]) if __name__ == '__main__': main() -
Launch the script by running the
python3 script.py request.jsoncommand. It must be launched in the command line from the folder with your createdrequest.jsonandscript.pyfiles.If Python with the
requestslibrary is already installed on your device, the script will run and create a planning task.Screenshot

If Python and the library aren't installed, proceed to step 4.
-
Install Python with the
requestslibrary and launch the script by running thepython3 script.py request.jsoncommand.To do this:
-
Visit the official developer website and download the latest Python version compatible with your device's operating system.
-
Run the installation and select the options that allow:
- Installation with administrator rights.
- Adding
python.exeto the PATH variable.
-
Allow the app to make changes on your device.
-
Wait for the installation to complete and reboot your device.
-
Open the folder where you installed Python. Find python.exe, copy it, and name the copy
python3.exe. -
Run the
python3 --versioncommand in the command line. Make sure that the output shows the version number. -
Use the pip package manager that comes with Python to install the
requestslibrary. To do this, run thepip install requestscommand and wait for the installation to complete. -
From the folder with your
request.jsonandscript.pyfiles, launch the script by running thepython3 script.py request.jsoncommand. This will create a planning task.
-
Get a planning result with a script
To get a Calendar Planning result via the API:
-
Create a file named
script.pyand copy the following Python script into it:import requests import hashlib import hmac APIKEY = '<your API key>' TASK_ID = '<task ID>' USER_AGENT = 'RouteQ Support Agent/1.0' METHOD = 'GET' BASE_URL = 'https://courier.yandex.com' PATH = f'/vrs/api/v1/calendar_planning/tasks/{TASK_ID}/result' PARAMS = {'apikey': APIKEY} BODY = "" headers = { 'User-Agent': USER_AGENT } print("--- Debug Info ---") print(f"URL for request:{BASE_URL}{PATH}") print(f"Parameters:{PARAMS}") print(f"Headers:{headers}") print("------------------\n") try: response = requests.get( url=f"{BASE_URL}{PATH}", headers=headers, params=PARAMS ) print(f"HTTP/1.1{response.status_code} {response.reason}") for header, value in response.headers.items(): print(f"{header}: {value}") except requests.exceptions.RequestException as e: print(f"An error occurred when executing the request:{e}") -
Enter the task ID in the
TASK_IDfield. -
Launch the script by running the
python3 script.pycommand.
It must be launched in the command line from the folder with your created script.py file.
If the script doesn't launch, make sure that Python with the requests library is installed on your device. To learn more about the installation, see step 4 of the Start planning with a script section.