Snyk API TypeScript Client

1.11.2 · maintenance · verified Tue Apr 21

This package provides a TypeScript client for interacting with Snyk API v1 endpoints. It is generated from Snyk's API Blueprint definitions, offering type definitions for both request bodies and responses to enhance developer experience with type safety. The current stable version is 1.11.2. The project is explicitly in 'maintenance mode,' meaning new feature development has ceased, with ongoing support limited to bug and security fixes. A critical distinction is that this client exclusively supports Snyk API v1 and does not offer integration with Snyk API v3 endpoints. Releases are irregular, typically driven by necessary bug fixes or updates to the underlying Snyk API v1 definitions. It requires Node.js version 14 or above.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Snyk API client, authenticate using an API token, and list projects for a specific organization using a POST request with filters. It also highlights type usage for request bodies.

import { Org, ProjectsPostBodyType } from 'snyk-api-ts-client';

const SNYK_API_TOKEN = process.env.SNYK_API_TOKEN ?? ''; // Replace with your Snyk API Token
const ORG_ID = 'your-org-id'; // Replace with your Snyk Organization ID

const main = async () => {
  if (!SNYK_API_TOKEN || !ORG_ID) {
    console.error('SNYK_API_TOKEN and ORG_ID must be set in environment variables or hardcoded.');
    return;
  }

  try {
    // Initialize the Org client with authentication and specific orgId
    const orgClient = new Org({ orgId: ORG_ID, apiToken: SNYK_API_TOKEN });

    // Example: List projects using a POST request with filters
    const body: ProjectsPostBodyType = { filters: { type: ['npm'], isPrivate: true } };
    console.log(`Fetching projects for organization ${ORG_ID}...`);
    const result = await orgClient.projects.post(body);

    console.log('Successfully fetched projects:');
    if (result && result.projects && result.projects.length > 0) {
      result.projects.slice(0, 3).forEach((project: any) => {
        console.log(`  - Project Name: ${project.name}, ID: ${project.id}`);
      });
      if (result.projects.length > 3) {
        console.log(`  ... and ${result.projects.length - 3} more.`);
      }
    } else {
      console.log('No projects found with the specified filters.');
    }

    // You can also get a full response object (including headers, status) by passing true
    // const fullResponse = await orgClient.projects.post(body, true);
    // console.log('Full response status:', fullResponse.status);

  } catch (error: any) {
    console.error('Error interacting with Snyk API:', error.message);
    if (error.response) {
      console.error('API Error Details:', error.response.data);
    }
  }
};

main();

view raw JSON →