NPM package API: how to query a package with "@"?

396 views Asked by At

You can query a NPM package with the NPM public API. E.g. to query the package react:

curl "https://api.npms.io/v2/package/react"

This works fine. See https://api-docs.npms.io/#api-Package-GetPackageInfo

But how do you query a package that is scoped, i.e prefixed with an "@"?

curl 'https://api.npms.io/v2/package/@types/react-router-dom'

This doesn't work.

1

There are 1 answers

0
jonrsharpe On BEST ANSWER

You just need to URL-encode the name:

https://api.npms.io/v2/package/%40types%2Freact-router-dom

The crucial part seems to be encoding the /, so the name isn't treated as two path segments, rather than the @ itself:

$ curl 'https://api.npms.io/v2/package/@types%2Freact-router-dom' | jq '.collected.metadata.name'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6907    0  6907    0     0  66413      0 --:--:-- --:--:-- --:--:-- 66413
"@types/react-router-dom"