By default the client will connect to the public Snyk service. If you are using a local installation then you can provide the API url as the second argument.
By default the User-Agent string for the API requests will be pysnyk/<version>. If you would like to send a custom user agent you can do so as follows:
This method currently only supports importing projects from GitHub and Docker Hub. For other integrations you will need to grab the lower-level snyk.models.Integration object from the snyk.models.Organization.integrations manager noted above. Other services will be added to this API soon.
Testing for vulnerabilities
The API also exposes methods to discover vulnerability information about individual packages. These methods are found on the Organization object.
test_maven(<package_group_id>, <package_artifact_id>, <version>) - returns an IssueSet containing vulnerability information for a Maven artifact
test_rubygem(<name>, <version>) - returns an IssueSet containing vulnerability information for a Ruby Gem
test_python(<name>, <version>) - returns an IssueSet containing vulnerability information for Python package from PyPi
test_npm(<name>, <version>) - returns an IssueSet containing vulnerability information for an NPM package
Here's an example of checking a particular Python package.
>>> org = client.organizations.first()
>>> result = org.test_python("flask", "0.12.2")
>>> assert result.ok
False
# You can access details of the vulnerabilities too, for example
>>> result.issues.vulnerabilities[0].title
'Improper Input Validation'
>>> result.issues.vulnerabilities[0].identifiers
{'CVE': ['CVE-2018-1000656'], 'CWE': ['CWE-20']
As well as testing individual packages you can also test all packages found in various dependency management manifests. The client currently supports the following methods:
test_pipfile(<file-handle-or-string>) - returns an IssueSet for all Python dependencies in a Pipfile
test_gemfilelock(<file-handle-or-string>) - returns an IssueSet for all Ruby dependencies in a Gemfile
test_packagejson(<file-handle-or-string>, (<lock-file-handle-or-string>)) - returns an IssueSet for all Javascript dependencies in a package.json file. Optionally takes a package.lock file
test_gradlefile(<file-handle-or-string>) - returns an IssueSet for all dependencies in a Gradlefile
test_sbt(<file-handle-or-string>) - returns an IssueSet for all dependencies defined in a .sbt file
test_pom(<file-handle-or-string>) - returns an IssueSet for all dependencies in a Maven pom.xml file
test_yarn(<file-handle-or-string>, <lock-file-handle-or-string>) - returns an IssueSet for all dependencies in Yarn package.json and yarn.lock files
test_composer(<file-handle-or-string>, <lock-file-handle-or-string>) - returns an IssueSet for all dependencies in Composer composer.json and composer.lock files
For example, here we are testing a Python Pipfile.
As well as the high-level API of the Snyk client you can use the HTTP methods directly. For these you simply need to pass the path, and optionally a data payload. The full domain, and the authentication details, are already provided by the client.
Most of the time you shouldn't need to use these. They are mainly useful if new methods are added to the API which are not yet supported in the client. This can also be useful if you want to pass very specific parameters, or to parse the raw JSON output from the API.
Experimental rest low-level client
pysnyk >= 0.9.0 now includes support for basic rest (formerly referred to as v3) compatibility. To switch to use a rest client, pass the rest API url and version when initializing a client. Right now it supports the GET method. Refer to the rest API docs for more information and examples.
Getting the rest information of an organization:
# To get this value, get it from a Snyk organizations settings page
snyk_org = "df734bed-d75c-4f11-bb47-1d119913bcc7"
# to use the rest endpoint you MUST include a version value and the url of the v3 api endpoint as shown below
rest_client = SnykClient(snyk_token, version="2022-02-16~experimental", url="https://api.snyk.io/rest")
print(rest_client.get(f"/orgs/{snyk_org}").json())
# this supports overriding rest versions for a specific GET requests:
user = rest_client.get(f"orgs/{snyk_org}/users/{snyk_user}", version="2022-02-01~experimental").json()
# pass parameters such as how many results per page
params = {"limit": 10}
targets = rest_client.get(f"orgs/{snyk_org}/targets", params=params)
V1 and rest can work at the same time by instantiating two clients:
The rest API introduces consistent pagination across all endpoints. The v3 client includes a helper method .get_rest_pages which collects the paginated responses and returns a single list combining the contents of the "data" key from all pages. It takes the same values as the get method.
rest_client = SnykClient(snyk_token, version="2022-02-16~experimental", url="https://api.snyk.io/rest")
params = {"limit": 10}
targets = rest_client.get(f"orgs/{snyk_org}/targets", params=params).json()
print(len(targets["data"]))
# returns 10 targets
all_targets = rest_client.get_rest_pages(f"orgs/{snyk_org}/targets", params=params)
print(len(all_targets))
# returns 33 targets, note we don't have to add .json() to the call or access the "data" key, get_rest_pages does that for us
# Override Content-Type header (or others) as needed for
response = rest_client.post(f"orgs/{snyk_org}/invites", body={"email": "some.body@snyk.io", "role": "f8223479-01a9-4774-8e29-06ce9a0513d6", headers={"Content-Type": "application/vnd.api+json"}})
For backwards compatibility the get_rest_pages method has an alternative name of get_v3_pages to not break code already rewritten replatformed to the 0.9.0 pysnyk module.
Works well with
Use together with pysnyk: extensions, drivers, servers and companion tools.
requestsPythie’s Index: 79Python HTTP for Humans.1.5B downloads/mo