A package to interact with StackPath

stackpath, cdn, waf, proxy, kubernetes, library, python
pip install pystackpath==0.5.0


PyStackpath - StackPath Python Library

Actions Status Python Version Pypi license GitHub stars

Python library interact with StackPath API (

It uses a custom OAuth2 requests.session


pip install pystackpath


Set up a new Stackpath instance

from pystackpath import Stackpath

sp = Stackpath(

Search for stacks

stacks = sp.stacks().index(filter="name='my-awesome-stack' and status='ACTIVE'")

Get one stack

stackid = "afcdaf14-47cb-40dd-9c13-3b20e6caf74a
onestack = sp.stacks().get(stackid)

Create a new stack

accountid = "081af5ee-38f8-44e9-b08a-881ea5de66f9"
newstack = sp.stacks().create(accountid, "my-second-stack")

Create a cdn site

stackid = "afcdaf14-47cb-40dd-9c13-3b20e6caf74a
cdnsite = sp.stacks().get(stackid).cdnsites().create(
    origin={"hostname": ""},

Search for cdn sites

cdnsite = sp.stacks().get(stackid).cdnsites().index(
    filter="label='' and status='ACTIVE'"

Delete a cdn site

cdnsiteid = "19e1a7b2-068f-491c-a95f-b64eae66dd34"
cdnsite = sp.stacks().get(stackid).cdnsites().get(cdnsiteid).delete()

Disable and enable a cdn site

cdnsiteid = "19e1a7b2-068f-491c-a95f-b64eae66dd34"
cdnsite = sp.stacks().get(stackid).cdnsites().get(cdnsiteid).enable()
cdnsite = cdnsite.disable()

Purge a cdn resource and check the purge status.

purge_request_id = sp.stacks().get(stackid).purge(
            "url": "", # required
            "recursive": True,
            "invalidateOnly": False,
            "purgeAllDynamic": False,
            "headers": [],
            "purgeSelector": [],

## purge_status can be used to check the status of the requested purge.
## Progress is represented as a decimal between 0 and 1, correlating to a
## percentage.

progress = sp.stacks().get(stackid).purge_status(purge_request_id)
##>> 1

Get metrics for a stack:

metrics_response1 = sp.stacks().get(stackid).metrics().get()

## Python datetime objects can be used to specify a date range, and the call
## allows a granularity to be specified. If no values are provided, the search range
## defaults to the last 24 hours, with 1 day granularity. See API doc for options.
## There is also a "platforms" argument which takes an array of "platform" codes.
## However the dev guide linked below only lists "CDE" which tracks egress traffic, which is
## how Stackpath bills for CDN usage, and "CDO" which is ingest traffic from the
## origin host, but is not billed. Since "CDE" tracks billable usage, it is the
## default.
from datetime import datetime, timedelta
end =
start = end - timedelta(days=7)
metrics_response2 = sp.stacks().get(stackid).metrics().get(granularity="PT1H",\
  platforms = ["CDO", "CDE"], start_datetime_object = start, end_datetime_object = end)

Retrieve all certificates from a stack:

certificate_response = sp.stacks().get(stackid).certificates()

 ##The object returned will have a 'results' attribute containing
 ##an array of the available certificates:
 first_certificate = certificate_response['results'][0]
 second_certificate = certificate_response['results'][1]

Add, update and delete a certificate for a stack:

##The cert and key are required, CA bundle is optional.
new_cert_response = sp.stacks().get(stackid).certificates().add(\
##The new cert ID can be retrieved from the returned object:
cert_id =

##To update a cert:
sp.stacks().get(stackid).certificates().update(cert_id, UPDATED_CERT_STRING, UPDATED_KEY_STRING)

##To delete a cert:
delete_cert_response = sp.stacks().get(stackid).certificates().delete(cert_id)