Api question to port-group


here is my little script to send the set commands in a bunch to my vyos to reach the api.

import requests
import json

# Your VyOS API URL and key
url = "https://vyos/configure"
key = '123456789'

# Read commands from the file
with open("setcommand.txt", "r") as file:
    commands = file.readlines()

# Parse commands and prepare data for the API
data_list = []
for command in commands:
    if command.startswith("set"):
        path = command.split()[1:]
        # Remove quotes from the command values
        path = [p.strip("'\"") for p in path]
        data_list.append({"op": "set", "path": path})

# Convert the list of commands to JSON
payload_data = json.dumps(data_list)

# Prepare the payload
payload = {
    'data': payload_data,
    'key': key

# Send commands to the API
response = requests.post(url, data=payload, verify=False)

# Print the response

{"success": false, "error": "Configuration path: [firewall group port-group ad-ports-tcp description TCP Ports] is not valid\n\nSet failed\n", "data": null}

My question how must be formated to send this set command to the vyos api
I have not found a example in the docu

set firewall group port-group ad-ports-tcp description ‘TCP Ports’

There is an example of python3 script

#!/usr/bin/env python3

import json
import re

import requests
import urllib3

def convert_to_json_commands(input_str):
    commands = input_str.strip().split('\n')
    result = []

    for command in commands:
        # Use regular expressions to extract parts based on desired pattern
        parts = re.findall(r"[^\s']+|'[^']+'", command)
        path = [part.replace("'", "") for part in parts[1:]]
        operation = {"op": "set", "path": path}

    return json.dumps(result, indent=4)

def configure_vyos(address, key, data):
    headers = {}
    url = f'https://{address}/configure'
    payload = {'data': data, 'key': key}

    response = requests.post(url, headers=headers, data=payload, verify=False)

    if response.status_code == 200:
        print("Configuration successful.")
        print(f"Address '{address}' configured.")
        print("Configuration failed. Status code:", response.status_code)
        print("Response:", response.text)

if __name__ == '__main__':

    commands = '''
set interfaces dummy dum1 address ''
set protocols static route next-hop
set firewall group port-group ad-ports-tcp description 'TCP Ports'
    config_json = convert_to_json_commands(commands)

    configure_vyos('', 'foo', config_json)


$ ./configure_via_api.py 
Configuration successful.
Address '' configured.

vyos@r14# show firewall 
 group {
     port-group ad-ports-tcp {
         description "TCP Ports"

So the format

curl -k --location --request POST 'https://vyos/configure' \
--form data='[{"op": "set","path":["interfaces","vxlan","vxlan1","remote",""]}, {"op": "set","path":["interfaces","vxlan","vxlan1","vni","1"]}]' \
1 Like