Snyk API TypeScript Client
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
-
Error: Request failed with status code 401
cause Incorrect or missing Snyk API token, or an expired token.fixVerify that your `SNYK_API_TOKEN` environment variable or hardcoded token is correct, has the necessary permissions, and has not expired. -
TypeError: Cannot read properties of undefined (reading 'someV3Endpoint')
cause Attempting to call a Snyk API v3 endpoint or property using this v1-only client.fixConfirm that the API endpoint or resource you are trying to access is part of Snyk API v1. This client does not support Snyk API v3. -
TS2339: Property 'someNewV1Property' does not exist on type 'V1ResourceType'
cause The client's generated types are outdated relative to recent Snyk API v1 changes, or you're trying to access a property that doesn't exist.fixEnsure you are on the latest version of `snyk-api-ts-client`. If the issue persists, the client might not yet reflect the API change due to its maintenance mode, requiring a workaround or manual type assertion.
Warnings
- deprecated The `/issues` endpoint was decommissioned as of version 1.7.3. Developers should migrate to the `/aggregated-issues` endpoint or the `/paths` endpoint for vulnerable path information, which now includes `fixVersion` details.
- gotcha This client strictly supports Snyk API v1 endpoints. Snyk API v3 endpoints are explicitly not supported, and attempts to use v3-specific functionalities or data structures through this client will fail or result in unexpected behavior.
- gotcha The `snyk-api-ts-client` package is in 'maintenance mode'. This means no new features will be developed, and future releases will focus exclusively on bug fixes and security patches. Major API changes or new Snyk functionalities will not be integrated.
- gotcha This package requires Node.js version 14 or above to function correctly. Using older Node.js versions (e.g., Node.js 12) may lead to compatibility issues or runtime errors.
Install
-
npm install snyk-api-ts-client -
yarn add snyk-api-ts-client -
pnpm add snyk-api-ts-client
Imports
- Orgs
const Orgs = require('snyk-api-ts-client')import { Orgs } from 'snyk-api-ts-client' - Org
import Org from 'snyk-api-ts-client'
import { Org } from 'snyk-api-ts-client' - ProjectsPostBodyType
import { ProjectsPostBodyType } from 'snyk-api-ts-client'import { ProjectsPostBodyType } from 'snyk-api-ts-client/dist/client/generated/org'
Quickstart
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();