Sites API Guide
This guide provides Python client samples for the Cisco Secure Access Sites API.
Note: Your Secure Access API key must have the permissions to read and write on the deployments.sites
key scope. For more information about the API key scopes, see Secure Access OAuth 2.0 Scopes.
First get your Secure Access API key, set up your environment, and install the Secure Access API client. For more information, see Samples Overview.
Run the Script
- Copy the script to a local file called
main.py
. Locate the script in your environment in a directory above thecisco
directory. - Run
python3 main.py
.
main.py
"""
Copyright (c) 2025 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""
import requests
from requests_toolbelt import MultipartEncoder
import json
import os
from dotenv import load_dotenv
from cisco.secure_access import API
from cisco.secure_access import deployments
from cisco.secure_access import GET
from cisco.secure_access import POST
from cisco.secure_access import PUT
from cisco.secure_access import DELETE
from cisco.secure_access import token_url
from cisco.secure_access import client_id
from cisco.secure_access import client_secret
# Sites API endpoints
sites_endpoint = "sites"
sites_details_endpoint = "sites/{}"
load_dotenv()
def get_sites(api):
''' Get Sites. '''
try:
# Get Sites in the organization
response = api.Query(deployments, sites_endpoint, GET)
# Check if the API request was successful
if response.status_code == 200:
print(f"Success. GET {sites_endpoint}, {response.json()}")
return response.json()
else:
print(f"Failed to get the Sites. Status code: {response.status_code}, Response: {response.text}.")
return None
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}.")
def create_site(api, name=None):
''' Create a Site. '''
try:
if name is None:
raise ValueError("name is required to create the Site.")
# Prepare the payload
payload = {
"name": name
}
# Create a Site
response = api.Query(deployments, sites_endpoint, POST, payload)
# Check the response status
if response.status_code == 200:
print(f"Success. POST {sites_endpoint}, {response.json()}")
return response.json()
else:
print(f"Failed to create the Site {name}. Status code: {response.status_code}, Response: {response.text}.")
return None
except Exception as e:
print(f"An error occurred: {e}.")
def get_site(api, site_id):
''' Get the properties of the Site. '''
try:
if site_id is None:
raise ValueError("site_id is required to get the Site.")
url = sites_details_endpoint.format(site_id)
# Get the properties for the Site
response = api.Query(deployments, url, GET)
# Check if the API request was successful
if response.status_code == 200:
print(f"Success. GET {url}, {response.json()}.")
return response.json()
else:
print(f"Failed to get the Site {site_id}. Status code: {response.status_code}, Response: {response.text}.")
return
except Exception as e:
print(f"An error occurred: {e}.")
def put_site(api, site_id, name=None):
''' Update the properties of the Site. '''
try:
if site_id is None or name is None:
raise ValueError("site_id and name are required to update the Site.")
# Prepare the payload
payload = {
"name": name
}
url = sites_details_endpoint.format(site_id)
# Update the properties for the Site
response = api.Query(deployments, url, PUT, payload)
# Check if the API request was successful
if response.status_code == 200:
print(f"Success. PUT {url}, {response.json()}.")
return response.json()
else:
print(f"Failed to update the Site {site_id}. Status code: {response.status_code}, Response: {response.text}.")
return
except Exception as e:
print(f"An error occurred: {e}.")
def delete_site(api, site_id):
''' Delete the Site. '''
try:
if site_id is None:
raise ValueError("site_id is required to delete the Site.")
url = sites_details_endpoint.format(site_id)
# Delete the Site
response = api.Query(deployments, url, DELETE)
# Check if the API request was successful
if response.status_code == 204:
print(f"No Content. DELETE {url}.")
else:
print(f"Failed to delete the Site {site_id}. Status code: {response.status_code}, Response: {response.text}.")
except Exception as e:
print(f"An error occurred: {e}.")
def main():
# Exit out if the required client_id or client_secret is not set
for var in ['API_KEY', 'API_SECRET', 'OUTPUT_DIR']:
if os.environ.get(var) == None:
print("Required environment variable: {} not set".format(var))
exit()
# Get an API token
api = API(token_url, client_id, client_secret)
try:
# get the sites in the organization
json_data = get_sites(api)
# create a site
name = "customer site one"
json_data = create_site(api, name)
site_id = None
# get a site
if 'siteId' in json_data:
site_id = json_data['siteId']
json_data = get_site(api, site_id)
# update a site
name = 'customer site two'
json_data = put_site(api, site_id, name)
# delete a site
json_data = delete_site(api, site_id)
except Exception as e:
print(e)
if __name__ == "__main__":
main()