> For the complete documentation index, see [llms.txt](https://docs.pritunl.com/kb/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.pritunl.com/kb/vpn/servers/adding-routes-with-api.md).

# Adding Routes with API

Add server routes with Pritunl API

When managing servers with hundreds of routes the API can be used to create the routes. Below is an example of a common use case of adding public AWS ranges. The API requires an enterprise subscription and the key can be found in the administrators tab of the web console.

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

BASE_URL = 'https://server.domain'
API_TOKEN = 'Hv2FxEMoa3moTVuRahMsMK3VUCwdmjmt'
API_SECRET = 'zihea5hTIpIgxsPFboby4hctopxWQSKd'
SERVER_ID = '57e9e364fd632c233e86f827'
REGIONS = {
    # 'GLOBAL',
    #
    # 'us-east-1',
    # 'us-east-2',
    # 'us-west-1',
    # 'us-west-2',
    # 'us-gov-west-1',
    #
    # 'eu-central-1',
    # 'eu-west-1',
    #
    # 'ap-south-1',
    # 'ap-northeast-1',
    # 'ap-northeast-2',
    # 'ap-southeast-1',
    # 'ap-southeast-2',
    #
    # 'cn-north-1',
    #
    # 'sa-east-1',
    #
    # 'us-gov-west-1',
}

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://ip-ranges.amazonaws.com/ip-ranges.json')
ranges = response.json()
routes = []

for range in ranges['prefixes']:
    if range['region'] not in REGIONS:
        continue

    routes.append({
        'network': range['ip_prefix'],
        'nat': True,
    })

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.pritunl.com/kb/vpn/servers/adding-routes-with-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
