Query npmjs registry via api
node.jsNpmasp.net Web-ApiNpmjsnode.js Problem Overview
I find I'm often unsatisfied with the ordering and expressiveness of searches on https://www.npmjs.com/. I guess there should be a way to programmatically query the server using https://api.npmjs.org/ or http://registry.npmjs.org/. But how? Where is the documentation?
(Or are there node packages to faciliate that? I know SO isn't about tool or package recommendations, so perhaps I shouldn't be asking about this as well, but if there were some tool, I could likely read the API from that as well.)
node.js Solutions
Solution 1 - node.js
There is a really good npm query site https://npms.io which I am using for years on my web project. It is an open-source project and supports advanced search with a better quality of the results for the keywords.
They have a scoring system for each package using the collected information about the project. The final score is calculated based on four different aspects of Quality, Maintenance, Popularity, and Personalities.
It also has very neat REST API.
API Doc: https://api-docs.npms.io
Solution 2 - node.js
https://skimdb.npmjs.com/registry/_all_docs
This is a current method to pull all the package names.
The URL search is http://registry.npmjs.com/-/v1/search?text=<searchstring>&size=20
Edit1: 3 years later (Mar 2021) and this endpoint still works. Glad it's still being helpful to folks :)
Edit2: Still working in April of 2022
Solution 3 - node.js
Of course there is at least one tool designed to talk to that registry, and that is the npm
command line tool itself. Its search for example starts by updating a local cached copy of the index, filtering that. In update-index one can read that the URL ending in /-/all
is apparently special. And indeed, appending that to registry.npmjs.org
(deliberately not formatted as a link) will fetch a 125M file which might be too much for your browser. Luckily the cached version is stored available in ~/.npm/registry.npmjs.org/-/all/.cache.json
. From there one can read a list of all nown packages. One could then continue to query each such package in more detail.
The fact that npm search
apparently uses no more elaborate scheme suggests that there is not much of a server-side api here.
Searching the web for pages mentioning api.npmjs.org
I found that the download counts can be retrieved from there. All other documents I found make use of that single facility.
Looking for alternatives, I also stumbled upon npmsearch.com which offers some more versatile searching facilities and has a somewhat documented API.
Solution 4 - node.js
You can always use the NPM registry client: https://github.com/npm/npm-registry-client.
If you look through this you can get the endpoints for the API. e.g to get the dist-tags for a package then you can go to /-/package/packageName/dist-tags'
So to get the babel-core dist tags you would go to http://registry.npmjs.org/-/package/babel-core/dist-tags
Solution 5 - node.js
If you can't find a package or are just hacking together a shell script the Registry API Docs in the registry's git repository include detailed information on Search API and search qualifiers.
If you're looking for the most popular insecure package in the public registry run:
wget -qO - "http://registry.npmjs.com/-/v1/search?text=is:insecure&popularity=1.0&size=1"
The above uses the is:insecure
search qualifier without any additional text criteria and grabs size=1
results where popularity=1.0
(the most popular).
Check in the docs
directory in the repo for a number of other useful things such as:
- Detailed list of response Package Metadata.
- Replicate API for scoped and unscoped packages.
- Information on getting download counts.
- Walkthrough on how to create a registry "follower".
Solution 6 - node.js
The docs are here now: https://github.com/npm/registry/blob/master/docs/download-counts.md
Downloads endpoint
Gets the total downloads for a given period, for all packages or a specific package.
GET https://api.npmjs.org/downloads/point/{period}[/{package}]</code>
Examples
- All packages, last day:
- https://api.npmjs.org/downloads/point/last-day">/downloads/point/last-day</a></dd>
- All packages, specific date:
- https://api.npmjs.org/downloads/point/2014-02-01">/downloads/point/2014-02-01</a></dd>
- Package "express", last week:
- https://api.npmjs.org/downloads/point/last-week/express">/downloads/point/last-week/express</a></dd>
- Package "express", given 7-day period:
- https://api.npmjs.org/downloads/point/2014-02-01:2014-02-08/express">/downloads/point/2014-02-01:2014-02-08/express</a></dd>
- Package "@slack/client", last 30 days:
- https://api.npmjs.org/downloads/point/last-month/@slack/client">/downloads/point/last-month/@slack/client</a></dd>
- Package "jquery", specific month:
- https://api.npmjs.org/downloads/point/2014-01-01:2014-01-31/jquery">/downloads/point/2014-01-01:2014-01-31/jquery</a></dd>
Solution 7 - node.js
You can find the documentation for the official public NPM registry API at the following GitHub repository: https://github.com/npm/registry
The documentation for the search endpoint can be found here: https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#get-v1search
> The following query parameters are accepted: text, size, quality, popularity, maintenance
curl -sL 'https://registry.npmjs.org/-/v1/search?text=react-native&size=5' | jq
Here we're using curl
to make the API request, and jq
to format the JSON response. The following output is returned:
{
"objects": [
{
"package": {
"name": "react-native",
"scope": "unscoped",
"version": "0.62.2",
"description": "A framework for building native apps using React",
"date": "2020-04-08T17:08:19.985Z",
"links": {
"npm": "https://www.npmjs.com/package/react-native",
"homepage": "https://github.com/facebook/react-native#readme",
"repository": "https://github.com/facebook/react-native",
"bugs": "https://github.com/facebook/react-native/issues"
},
"publisher": {
"username": "react-native-bot",
"email": "[email protected]"
},
"maintainers": [
{
"username": "cpojer",
"email": "[email protected]"
},
{
"username": "fb",
"email": "[email protected]"
},
{
"username": "grabbou",
"email": "[email protected]"
},
{
"username": "hectorramos",
"email": "[email protected]"
},
{
"username": "react-native-bot",
"email": "[email protected]"
}
]
},
"flags": {
"unstable": true
},
"score": {
"final": 0.4604636166574407,
"detail": {
"quality": 0.5886895871400879,
"popularity": 0.47768592528213616,
"maintenance": 0.3333333333333333
}
},
"searchScore": 100000.47
},
{
"package": {
"name": "react-native-svg",
"scope": "unscoped",
"version": "12.1.0",
"description": "SVG library for react-native",
"keywords": [
"react-component",
"react-native",
"ios",
"android",
"SVG",
"ART",
"VML",
"gradient"
],
"date": "2020-04-09T23:50:28.358Z",
"links": {
"npm": "https://www.npmjs.com/package/react-native-svg",
"homepage": "https://github.com/react-native-community/react-native-svg",
"repository": "https://github.com/react-native-community/react-native-svg",
"bugs": "https://github.com/react-native-community/react-native-svg/issues"
},
"publisher": {
"username": "msand",
"email": "[email protected]"
},
"maintainers": [
{
"username": "brentvatne",
"email": "[email protected]"
},
{
"username": "dustin.savery",
"email": "[email protected]"
},
{
"username": "magicismight",
"email": "[email protected]"
},
{
"username": "msand",
"email": "[email protected]"
}
]
},
"score": {
"final": 0.5244501419238121,
"detail": {
"quality": 0.9582364654638059,
"popularity": 0.34375010176572474,
"maintenance": 0.3333333333333333
}
},
"searchScore": 0.031369504
},
{
"package": {
"name": "react-native-device-info",
"scope": "unscoped",
"version": "5.6.1",
"description": "Get device information using react-native",
"keywords": [
"react-component",
"react-native",
"ios",
"android",
"windows",
"device",
"events",
"cocoapod"
],
"date": "2020-05-27T18:33:11.715Z",
"links": {
"npm": "https://www.npmjs.com/package/react-native-device-info",
"homepage": "https://github.com/react-native-community/react-native-device-info#readme",
"repository": "https://github.com/react-native-community/react-native-device-info",
"bugs": "https://github.com/react-native-community/react-native-device-info/issues"
},
"author": {
"name": "Rebecca Hughes",
"email": "[email protected]",
"url": "https://github.com/rebeccahughes"
},
"publisher": {
"username": "mikehardy",
"email": "[email protected]"
},
"maintainers": [
{
"username": "gantman",
"email": "[email protected]"
},
{
"username": "jeroenbourgois",
"email": "[email protected]"
},
{
"username": "lilach",
"email": "[email protected]"
},
{
"username": "machour",
"email": "[email protected]"
},
{
"username": "rebeccahughes",
"email": "[email protected]"
}
]
},
"score": {
"final": 0.5102794746987024,
"detail": {
"quality": 0.9614895473985808,
"popularity": 0.3004741251784613,
"maintenance": 0.3333333333333333
}
},
"searchScore": 0.016712856
},
{
"package": {
"name": "css-to-react-native",
"scope": "unscoped",
"version": "3.0.0",
"description": "Convert CSS text to a React Native stylesheet object",
"keywords": [
"styled-components",
"React",
"ReactNative",
"styles",
"CSS"
],
"date": "2019-10-10T17:46:00.297Z",
"links": {
"npm": "https://www.npmjs.com/package/css-to-react-native",
"homepage": "https://github.com/styled-components/css-to-react-native#readme",
"repository": "https://github.com/styled-components/css-to-react-native",
"bugs": "https://github.com/styled-components/css-to-react-native/issues"
},
"author": {
"name": "Jacob Parker"
},
"publisher": {
"username": "jacobp100",
"email": "[email protected]"
},
"maintainers": [
{
"username": "jacobp100",
"email": "[email protected]"
},
{
"username": "kristerkari",
"email": "[email protected]"
},
{
"username": "mxstbr",
"email": "[email protected]"
}
]
},
"score": {
"final": 0.4836548888489432,
"detail": {
"quality": 0.8524860977159072,
"popularity": 0.39264581380833835,
"maintenance": 0.25852292771786467
}
},
"searchScore": 0.013077582
},
{
"package": {
"name": "react-native-elements",
"scope": "unscoped",
"version": "2.0.4",
"description": "React Native Elements & UI Toolkit",
"keywords": [
"react-native",
"reactjs",
"reactnative",
"bootstrap"
],
"date": "2020-06-30T02:30:34.882Z",
"links": {
"npm": "https://www.npmjs.com/package/react-native-elements",
"homepage": "https://react-native-elements.github.io/react-native-elements/",
"repository": "https://github.com/react-native-elements/react-native-elements",
"bugs": "https://github.com/react-native-elements/react-native-elements/issues"
},
"author": {
"name": "Nader Dabit & Monte Thakkar"
},
"publisher": {
"username": "flyingcircle",
"email": "[email protected]"
},
"maintainers": [
{
"username": "dabit3",
"email": "[email protected]"
},
{
"username": "iroachie",
"email": "[email protected]"
},
{
"username": "monte9",
"email": "[email protected]"
}
]
},
"score": {
"final": 0.4921277780507845,
"detail": {
"quality": 0.9218028296308819,
"popularity": 0.28262932141386665,
"maintenance": 0.3333333333333333
}
},
"searchScore": 0.011005878
}
],
"total": 26290,
"time": "Wed Jul 08 2020 05:02:14 GMT+0000 (UTC)"
}
Solution 8 - node.js
Take a look at sinopia registry interface: https://github.com/rlidwka/sinopia/blob/master/lib/index-api.js. The default registry for npm client is https://registry.npmjs.org (try it out on console: npm config ls -l). So you can try the following (referencing the sinopia API) to fetch data about react 15.0.2
Solution 9 - node.js
I know that this answer is old but some can still find it relevant so:
After a lot of searching, I finally found something, The solution doesn’t use NPM API because of its bad documentation and many more disadvantages that it has
(This will be helpful for other usages other than searching like getting a list of popular packages (what I needed))
The solution is to use Libraries.io
> Libraries.io indexes data from 4,273,741 packages from 36 package managers. (Including NPM) From Libraries.io page
It has great API and good documentation, also it has some API wrapper in several languages
You can take a look at my CLI program that uses that library here
Solution 10 - node.js
You can use api-npm node module it directly queries the NPM registry and you can get all attributes of a module and download stat of any module of any time range https://www.npmjs.com/package/api-npm