# Problem with script adding routes via API

**URL:** <https://forum.pritunl.com/t/problem-with-script-adding-routes-via-api/2017>\
**Category:** Pritunl VPN\
**Created:** [April 26, 2024, 2:55pm UTC](https://forum.pritunl.com/t/problem-with-script-adding-routes-via-api/2017 "2024-04-26T14:55:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![infra](https://forum.pritunl.com/letter_avatar_proxy/v4/letter/i/f4b2a3/32.png) [@infra](https://forum.pritunl.com/u/infra)\
**Post date:** [April 26, 2024, 2:55pm UTC](https://forum.pritunl.com/t/problem-with-script-adding-routes-via-api/2017/1 "2024-04-26T14:55:25Z")

</div>

I have this script and fails when i wanna add ips via API. I copied and modified the script from the docs

```auto
import requests
import time
import uuid
import hmac
import hashlib
import base64
import json
import sys

BASE_URL = 'https://xxx.xxxx.com/'
API_TOKEN = 'xxxx'
API_SECRET = 'xxx'
SERVER_ID = 'xxxx'

terraform_response = requests.get('https://app.terraform.io/api/meta/ip-ranges')
terraform_ranges = terraform_response.json()

def auth_request(method, path, headers=None, data=None):
    auth_timestamp = str(int(time.time()))
    auth_nonce = uuid.uuid4().hex
    auth_string = '&'.join([API_TOKEN, auth_timestamp, auth_nonce,
        method.upper(), path])
    if sys.version_info[0] < 3:
        auth_signature = base64.b64encode(hmac.new(
            API_SECRET, auth_string, hashlib.sha256).digest())
    else:
        auth_signature = base64.b64encode(hmac.new(
            API_SECRET.encode('utf-8'), auth_string.encode('utf-8'), hashlib.sha256).digest())
    auth_headers = {
        'Auth-Token': API_TOKEN,
        'Auth-Timestamp': auth_timestamp,
        'Auth-Nonce': auth_nonce,
        'Auth-Signature': auth_signature,
    }
    if headers:
        auth_headers.update(headers)
    return getattr(requests, method.lower())(
        BASE_URL + path,
        headers=auth_headers,
        data=data,
    )

response = requests.get('https://app.terraform.io/api/meta/ip-ranges')
ranges = response.json()
routes = []

ranges_data = response.json()

for service, ips in ranges_data.items():
    for ip in ips:
        routes.append({
            'network': ip,
            'nat': True,
        })

#print(json.dumps(routes))
#exit(1)
response = auth_request(
    'POST',
    '/server/%s/routes' % SERVER_ID,
    headers={
        'Content-Type': 'application/json',
   },
     data=json.dumps(routes),
)
assert(response.status_code == 200)

```

the result of the json data is

[{“network”: “75.2.98.97/32”, “nat”: true}, {“network”: “99.83.150.238/32”, “nat”: true}, {“network”: “52.86.200.106/32”, “nat”: true}, {“network”: “52.86.201.227/32”, “nat”: true}, {“network”: “52.70.186.109/32”, “nat”: true}, {“network”: “44.236.246.186/32”, “nat”: true}, {“network”: “54.185.161.84/32”, “nat”: true}, {“network”: “44.238.78.236/32”, “nat”: true}, {“network”: “52.86.200.106/32”, “nat”: true}, {“network”: “52.86.201.227/32”, “nat”: true}, {“network”: “52.70.186.109/32”, “nat”: true}, {“network”: “44.236.246.186/32”, “nat”: true}, {“network”: “54.185.161.84/32”, “nat”: true}, {“network”: “44.238.78.236/32”, “nat”: true}, {“network”: “52.86.200.106/32”, “nat”: true}, {“network”: “52.86.201.227/32”, “nat”: true}, {“network”: “52.70.186.109/32”, “nat”: true}, {“network”: “44.236.246.186/32”, “nat”: true}, {“network”: “54.185.161.84/32”, “nat”: true}, {“network”: “44.238.78.236/32”, “nat”: true}]

---

<div class="post-metadata">

**Author:** ![zach](https://forum.pritunl.com/user_avatar/forum.pritunl.com/zach/32/1105_2.png) [@zach](https://forum.pritunl.com/u/zach)\
**Post date:** [April 30, 2024, 9:44pm UTC](https://forum.pritunl.com/t/problem-with-script-adding-routes-via-api/2017/2 "2024-04-30T21:44:45Z")

</div>

There is an example in [**pritunl/tools/add\_aws\_ranges.py**](https://github.com/pritunl/pritunl/blob/master/tools/add_aws_ranges.py). What error message is shown?
