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 SettingsCompany.

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:

  1. Create request.json and 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.

  2. In the same folder, create a file named script.py and 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()
    
  3. Launch the script by running the python3 script.py request.json command. It must be launched in the command line from the folder with your created request.json and script.py files.

    If Python with the requests library 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.

  4. Install Python with the requests library and launch the script by running the python3 script.py request.json command.

    To do this:

    1. Visit the official developer website and download the latest Python version compatible with your device's operating system.

    2. Run the installation and select the options that allow:

      • Installation with administrator rights.
      • Adding python.exe to the PATH variable.
    3. Allow the app to make changes on your device.

    4. Wait for the installation to complete and reboot your device.

    5. Open the folder where you installed Python. Find python.exe, copy it, and name the copy python3.exe.

    6. Run the python3 --version command in the command line. Make sure that the output shows the version number.

    7. Use the pip package manager that comes with Python to install the requests library. To do this, run the pip install requests command and wait for the installation to complete.

    8. From the folder with your request.json and script.py files, launch the script by running the python3 script.py request.json command. This will create a planning task.

Get a planning result with a script

To get a Calendar Planning result via the API:

  1. Create a file named script.py and 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}")
    
  2. Enter the task ID in the TASK_ID field.

  3. Launch the script by running the python3 script.py command.

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.

Contact support