Cisco Firepower Management Center (FMC) API Client Code Examples¶
This guide shows how to use the
CiscoFMC client to interact with Cisco Firepower Management Center APIs.
Environment Variables
You can set the environment variables in your shell to avoid hardcoding sensitive information in your code. Check out the FAQ section on Environment Variables for more details.
from wingpy import CiscoFMC
fmc = CiscoFMC(
base_url="https://fmc.example.com", # (1)!
username="...", # (2)!
password="...", # (3)!
verify=False,
)
-
Environment variable:
-
Environment variable:
-
Environment variable:
The
fmc.get_all() method fetches all the pages and items for you. It also uses concurrency under the hood to fetch the data in parallel, which can significantly speed up the process for large datasets.
devices = fmc.get_all("/api/fmc_config/v1/domain/{domainUUID}/devices/devicerecords")
for device in devices:
print(device["name"])
What you might also notice is that we just copied the URL from the FMC API documentation. The
CiscoFMC client automatically handles the domain UUID for you, this means you can conveniently use the API endpoints as they are documented and paste them directly into your code.
host_data = {
"name": "TestHost",
"value": "192.0.2.10",
"type": "Host",
}
response = fmc.post(
"/api/fmc_config/v1/domain/{domainUUID}/object/hosts",
data=host_data,
)
object_id = response.json()["id"]
path_params = {"objectId": object_id}
We have created a host object and stored the objectId in the path_params variable, which we will use in subsequent requests.
Again, we want to preserve the ability to use the API endpoints as they are documented, so we can use the path_params variable to pass the objectId in the URL.
Imagine if you have a large number of objects, you can easily loop through them and perform operations on each one. For this example, we will update and delete the host object we just created.
update_data = dict(host_data)
update_data.update({
"id": object_id,
"name": "TestHostUpdated",
})
response = fmc.put(
"/api/fmc_config/v1/domain/{domainUUID}/object/hosts/{objectId}",
path_params=path_params,
data=update_data,
)
print(response.status_code)