DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]

Trending

  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How to Use an npm REST API to Get npm Audit Results

How to Use an npm REST API to Get npm Audit Results

See how to use an npm REST API to get npm audit results.

By 
Gorav Singal user avatar
Gorav Singal
·
Sep. 04, 19 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
19.8K Views

Join the DZone community and get the full member experience.

Join For Free

Dog taking a REST

We know you're tired, but you can REST now with this npm REST API tutorial.

Introduction

Npm has a tool called npm audit, which reports if your packages or libraries have any known vulnerabilities on them or not. This is an excellent initiative from npm.

This is a great security threat in which your application can be hacked or vulnerable if your application is using a 3rd-party library that has a known vulnerability on it. Even if your app does not have a security issue, your whole system is vulnerable due to that 3rd-party library. It is one of the top 10 Owasp Security threats.

In this post, we will see the following:

  • How to use it via REST API
  • You don’t need to install a package before using npm audit
  • No need to run npm audit command
  • Check vulnerability information about any npm package without installing it

How npm Audit Works Internally

It requires your package.json and package-lock.json file. It reads some meta-information from these files and submits it to their web servers via REST APIs. The web server then returns the response and indicates if any library has vulnerable information in it or not.

So, when you run npm audit on the home directory of your project, it prepares some data and sends it to its web server.

npm audit uses a module, npm-registry-fetch, which exposes some methods to call those REST APIs. However, you will not find its documentation anywhere. I just found it while looking at the GitHub code of npm.

Rest API for Getting npm Audit Information

URL: /-/npm/v1/security/audits
Host: registry.npmjs.org
Port: 443
HttpMethod: POST

It has a post body that looks like:

{
    "name": "npm_audit_test",
    "version": "1.0.0",
    "requires": {
        "marked": "^0.6.3"
    },
    "dependencies": {
        "marked": {
            "version": "0.6.3",
            "integrity": "sha1-ebq614r2OLpNUiqecVzf3SQp6UY=234"
        }
    }
}

So, the good thing is that you don't need to have package.json or package-lock.json file. You can just call this API and can get a result. You can see above that it is sending some hash integrity in POST body, but you can remove that as well.

Let's look at the fully functional code.

Code to Fetch Audit Data

Here, I have used a nonexistent name — npm_audit_test — and any version of my project. It can be anything. And I’m using a dependency package: marked.

const regFetch = require('npm-registry-fetch');

const auditData = {
    "name": "npm_audit_test",
    "version": "1.0.0",
    "requires": {
        "marked": "^0.6.3"
    },
    "dependencies": {
        "marked": {
            "version": "0.6.3",
            "integrity": "sha1-ebq614r2OLpNUiqecVzf3SQp6UY=234"
        }
    }
};

let opts = {
    "color":true,
    "json":true,
    "unicode":true,
    method: 'POST',
    gzip: true,
    body: auditData
};

return regFetch('/-/npm/v1/security/audits', opts)
    .then(res => {
        return res.json();
    })
    .then(res => {
        console.log(JSON.stringify(res, "", 3));
  }).catch(err => console.error(err));

So, the solution that is presented above doesn’t require you to install your packages. You can just pass any package name and you are done.

If you have any questions, feel free to comment.

Further Reading

The License and Security Risks of Using Node.js

REST Web Protocols API Npm (software)

Published at DZone with permission of Gorav Singal. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook