Registry / devops / pysnyk

pysnyk

JSON →
library0.9.19pypypi✓ verified 87d ago

pysnyk is a Python client library for interacting with the Snyk API, enabling programmatic access to Snyk's security analysis and vulnerability management capabilities. It provides an object-oriented interface to various Snyk resources like organizations, projects, and issues. The library is currently at version 0.9.19 and maintains a frequent release cadence, often with patch versions addressing bug fixes and minor enhancements.

pip install pysnyk
INSTALL
IMPORT
SIG · PYSNYK
P
pysnyk
devopspythonv0.9.19
Install
2.8s avg
Import
980ms
Disk
23MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.19 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 1.042s · 24.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.8s · import 0.918s · 25MB
23MB installed
● package 23MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SnykClient
from snyk import SnykClient
import pysnyk
While 'import pysnyk' works, the primary class is exposed directly under the 'snyk' namespace, making 'from snyk import SnykClient' the idiomatic import.

This quickstart initializes the SnykClient using an API token, preferably from the `SNYK_TOKEN` environment variable. It then fetches and prints all accessible Snyk organizations and, for the first organization found, lists its associated projects.

import os from snyk import SnykClient # It's recommended to set SNYK_TOKEN as an environment variable # and avoid hardcoding API tokens in your code. snyk_token = os.environ.get('SNYK_TOKEN', 'YOUR_SNYK_API_TOKEN') if not snyk_token or snyk_token == 'YOUR_SNYK_API_TOKEN': raise ValueError("SNYK_TOKEN environment variable not set or is default value.") try: client = SnykClient(snyk_token) print("Successfully initialized SnykClient.") # Fetch all organizations you have access to organizations = client.organizations.all() print(f"Found {len(organizations)} organizations:") for org in organizations: print(f" - {org.name} (ID: {org.id})") if organizations: # Get the first organization and list its projects first_org = organizations[0] print(f"\nProjects in {first_org.name} (ID: {first_org.id}):") projects = first_org.projects.all() for project in projects: print(f" - {project.name} (ID: {project.id})") except Exception as e: print(f"An error occurred: {e}")
Debug
Known issues
breakingThe default page size for `.all()` methods in pysnyk changed from a smaller, implicit value to 100 in version 0.9.18. This can alter the number of API calls made and potentially impact existing pagination logic or performance expectations in client applications.
fix
Review any code that makes assumptions about the number of items per page or the total API calls for `.all()` methods. If a specific page size is required, explicitly pass the `per_page` argument (e.g., `client.organizations.all(per_page=50)`).
affects: 0.9.18+
gotchaAuthentication requires a Snyk API token. Hardcoding this token is a security risk. It's best practice to use environment variables.
fix
Set the `SNYK_TOKEN` environment variable with your Snyk API token (e.g., `export SNYK_TOKEN='your_api_token'`). The `SnykClient` constructor will automatically pick it up. Alternatively, pass it explicitly `SnykClient(os.environ.get('SNYK_TOKEN'))`.
affects: All versions
gotchaWhen fetching dependency graphs for projects, the underlying Snyk API only supports specific package managers. Attempting to get a dependency graph for an unsupported project type (e.g., Dockerfile-based projects without a recognized manifest) can lead to exceptions.
fix
Catch potential exceptions when calling `project.dependency_graph` and verify the project type beforehand if possible. Refer to Snyk API documentation for supported project types for dependency graph retrieval.
affects: All versions
Errors
Common errors & fixes
ERROR: Could not find a version that satisfies the requirement pysnyk (from versions: none)\nERROR: No matching distribution found for pysnyk
The Python version being used is incompatible with `pysnyk` (which requires Python >=3.7,<4.0), or there is a typo in the package name.
fix
Ensure you are using a Python version between 3.7 and 3.9 (inclusive for minor versions) and that the package name `pysnyk` is spelled correctly. Upgrade Python if necessary.
snyk.exceptions.SnykHTTPError: 401 Unauthorized
The provided Snyk API token is missing, expired, or invalid. This can also occur if the token lacks the necessary permissions for the requested operation.
fix
Verify that your `SNYK_TOKEN` environment variable is correctly set and contains a valid, non-expired Snyk API token with the appropriate scopes. Generate a new token if unsure.
AttributeError: 'Project' object has no attribute 'issueCountsBySeverity' or similar 'InvalidFieldValue' errors for model attributes.
The Snyk API response structure might have evolved, introducing new fields or changing existing ones that are not yet reflected in the `pysnyk` client's internal models. This commonly happens with composite objects like `IssueData` or `Project` attributes.
fix
Upgrade `pysnyk` to the latest version. If the issue persists with the latest version, consider accessing the raw data via `obj.data` or `obj.json` properties (if available for the specific object) and parsing the JSON manually, or report the issue to the `pysnyk` maintainers.
Upgrade
Version history
0.9.19latest on PyPI · released Jan 13, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
pysnyk — pip install pysnyk · libregistry