Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Query the Cisco ACI APIC API using Python & requests
import requests, json
apic_url = ''
apic_username = 'admin'
apic_password = 'ciscopsdt'
def apic_login(apic: str, username: str, password: str) -> dict:
""" APIC login and return session cookie """
apic_cookie = {}
credentials = {'aaaUser': {'attributes': {'name': apic_username, 'pwd': apic_password }}}
json_credentials = json.dumps(credentials)
base_url = 'https://' + apic + '/api/aaaLogin.json'
login_response =, data=json_credentials)
login_response_json = json.loads(login_response.text)
token = login_response_json['imdata'][0]['aaaLogin']['attributes']['token']
apic_cookie['APIC-Cookie'] = token
return apic_cookie
def apic_query(apic: str, path: str, cookie: dict) -> dict:
""" APIC 'GET' query and return response """
base_url = 'https://' + apic + path
get_response = requests.get(base_url, cookies=cookie)
return get_response
def apic_logout(apic: str, cookie:dict) -> dict:
""" APIC logout and return response """
base_url = 'https://' + apic + '/api/aaaLogout.json'
post_response =, cookies=cookie)
return post_response
apic_cookie = apic_login(apic=apic_url, username=apic_username, password=apic_password)
response = apic_query(apic=apic_url, path='/api/class/fabricHealthTotal.json', cookie=apic_cookie)
logout_response = apic_logout(apic=apic_url, cookie=apic_cookie)
response_json = json.loads(response.text)
fab_health_total = response_json['imdata'][0]['fabricHealthTotal']['attributes']['cur']

This comment has been minimized.

Copy link

@illumine illumine commented Feb 27, 2021

Very good job. Simple and instructional.


This comment has been minimized.

Copy link
Owner Author

@NWMichl NWMichl commented Mar 1, 2021

Very good job. Simple and instructional.

Thank you! I could've added more comments, but was in a hurry ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment