pysnyk: Snyk API Python Client

0.9.19 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

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}")

view raw JSON →