The Command-line interface interface to Wikidata
- Dependencies
- Installation
- Commands
- See Also
- You may also like
- License
npm install -g wikidata-cliInstalling globally allows to make the command wd accessible from your shell $PATH
Working with Wikidata, we often end up with obscure ids. We can always look-up those ids labels on the website but that means loading pages and pages, when a small API call and parsing could return just what we need: a label
wd label Q1103345
# => The Cluetrain ManifestoOptions:
-c, --clipboard: copy the result to clipboard-l, --lang: specify the label's language
By default, the result uses your environment local language (process.env.LANG), but you can pass a 2-letters language code as second argument
wd label Q1103345 -l de
# => Cluetrain-Manifest
wd label Q123 -l zh
# => 9月A quick way to access the claims of an entity
# all Q2001's claims
wd claims Q2001
# or just his place of birth
wd claims Q2001 P19Options:
-v, --verbose: make results more verbose-l, --lang: specify the properties labels' language
wd claims Q2001 -l es
wd claims Q2001 P19 --lang ruOptions when passing both an id an property:
-c, --clipboard: copy the result to clipboard-j, --json: format the result as JSON
A command to output the geographic coordinates (latitude and longitude) of an entity
wd coord Q2112
# => 52.016666666667 8.5166666666667A quick way to access an entity's raw JSON data
wd data Q1496This simply outputs the result of https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=Q1496, parsed to keep only what is relevant to the requested entity (here Q1496).
The output is valid json, so it lets you the possibility to pipe it to a JSON parser such as jsondepth:
wd data Q1496 | jd labels.pt
# => { language: 'pt', value: 'Fernão de Magalhães' }This one is kind of the inverse of wd label: pass it the title of a Wikipedia article and it will return the corresponding Wikidata id
wd id Cantabria
# => Q3946
wd id New Delhi
# => Q987Options:
-v, --verbose: make results more verbose-c, --clipboard: copy the result to clipboard-l, --lang: specify from which language the title comes By default, it will look at the Wikipedia corresponding to your environment local language (process.env.LANG), but you can specify another language by passing a 2-letters language code
wd id -l fr science politique
# => Q36442You can also pass it full Wikipedia urls and let it deduce the language from there
wd id https://en.wikipedia.org/wiki/Friedrich_Nietzsche
# => Q9358A command to access the list of all Wikidata properties in a given language (by default the environment local language)
wd propsOutputs a JSON object of the kind:
[...]
"P2897": "identifiant Eldoblaje Movie",
"P2898": null,
"P2899": "âge minimal",
[...]
NB: properties without a label in the requested language are set to null, as you can see above for P2898 in French
This is especially convenient when you're looking for a property:
# look for a property dealing with images
wd props |grep imageOutputs:
{
"P6": "head of government",
"P10": "video",
"P14": "graphic symbol of thoroughfare",
"P15": "route map",
"P16": "highway system",
"P17": "country",
"P18": "image",
[...]
Option: -l, --lang: specify the properties labels language
wd props -l sv
# outputs the properties in SwedishThis command first tries to find the list in the props folder (created at wikidata-cli root), and request them to query.wikidata.org if missing.
This means that after a while, your local version will miss new and updated properties: this can be solved by using the --reset options
Option: -r, --reset: clear properties cache
Every property accepts values of a precise type, one of CommonsMedia, ExternalId, String, WikibaseItem, Time, GlobeCoordinate, Monolingualtext, Quantity, Url, WikibaseProperty, or Math
Option: -t, --type: include properties types
wd props --typeOutputs:
{
"P6": { "type": "WikibaseItem", "label": "head of government" },
"P10": { "type": "CommonsMedia", "label": "video" },
"P14": { "type": "CommonsMedia", "label": "graphic symbol of thoroughfare" },
"P15": { "type": "CommonsMedia", "label": "route map" },
"P16": { "type": "WikibaseItem", "label": "highway system" },
"P17": { "type": "WikibaseItem", "label": "country" },
"P18": { "type": "CommonsMedia", "label": "image" },
[...]
A command to run a SPARQL query and get its JSON output
From this SPARQL query file: ./path/to/query.rq
SELECT ?work WHERE {
?work wdt:P50 wd:Q42 .
}get its output from your terminal like so:
wd sparql ./path/to/query.rq > ./results.jsonOptions:
-v, --verbose: log the generated SPARQL-r, --raw: output raw SPARQL results (instead of results simplified by wikidata-sdksimplifySparqlResultsfunction
wd sparql ./path/to/query.rq > ./results.json
wd sparql -r ./path/to/query.rq > ./raw_sparql_results.jsonA command to generate and run a simple SPARQL query, passing one or two of the elements that make a statement:
-s, --subject-p, --property-o, --object
# what is the entity id matching the twitter username (P2002) "timberners_lee"?
wd query --property P2002 --object timberners_lee
# which works have exoplanets (Q44559) for main subject (P921)?
wd query --property P921 --object Q44559 --labels
# or with the short options syntax
wd query -p P921 -o Q44559 -aOther Options:
-r, --raw: output raw SPARQL results (instead of results simplified by wikidata-sdksimplifySparqlResultsfunction-a, --labels: requests results labels-l, --lang <lang>: specify the labels' language-t, --limit <num>: set the request results limit (defaults to 1000)-v, --verbose: log the generated request
A command to open a pages on Wikidata in a browser from the command line (yep, you can be that lazy)
wd open Q123
# opens https://wikidata.org/wiki/Q123 in your default browser
wd open P659
# opens https://www.wikidata.org/wiki/Property:P659
# also working with any string that matches /(Q|P)\d+/
wd open https://inventaire.io/entity/wd:Q33977
# opens https://wikidata.org/wiki/Q33977Options:
-p, --wikipedia: open the Wikipedia article instead
wd open -p Q123
# opens https://fr.wikipedia.org/wiki/Septembre because my system language is French-l, --lang: specify which Wikipedia edition should be targeted
wd open -p -l sv Q123
# opens https://sv.wikipedia.org/wiki/September insteadwd open Dan Simmons
# opens https://www.wikidata.org/w/index.php?title=Special:Search&search=Dan%20SimmonsThose command modify Wikidata so you will be asked your Wikidata username and password to use them. Those will be persisted in clear text in this module's folder: ./config.json. Alternatively, in the case writing to this module's folder would require special rights, the config file with your crendentials can be found in your home folder: ~/.config/wikidata-cli/config.json. This allows not having to re-enter crendentials everytimes, but it can problematic on a non-personal computer: in such a case, make sure to run wd config clear once you're done.
Set a label on an entity in a given language
wd set-label <entity> <language> <label>
# Alias:
wd sl <entity> <language> <label>Example:
# Set the label 'Bac à sable bulgroz' to the Sandbox entity (Q4115189) in French
wd set-label Q4115189 fr "Bac à sable bulgroz"Add a claim to an entity
wd add-claim <entity> <property> <value>
# Alias:
wd ac <entity> <property> <value>Example:
# Add the Twitter account (P2002) 'bulgroz' to the Sandbox (Q4115189) entity
wd add-claim Q4115189 P2002 bulgroz
# The same but using the command alias
wd ac Q4115189 P2002 bulgroz
# The same but passing a reference as 4th argument:
# Add the statement that the Sandbox (Q4115189) has for part (P527) the sand (Q34679)
wd ac Q4115189 P527 Q34679Simply add a 4th argument, either a reference URL (P854), or the id of the project it is imported from (P143)
# this will be interpreted as being imported from Wikipedia in Uyghur (Q60856)
wd ac Q4115189 P527 Q34679 Q60856Add a reference to an claim
wd add-reference <claim-guid> <URL or project entity id>
# Alias:
wd ar <claim-guid> <URL or project entity id>Example:
# /!\ be ware of the '$' sign that might need escaping
wd add-reference "Q4115189\$E66DBC80-CCC1-4899-90D4-510C9922A04F" 'https://example.org/rise-and-box-of-the-holy-sand-box'
# or
wd add-reference 'Q4115189$E66DBC80-CCC1-4899-90D4-510C9922A04F' 'https://example.org/rise-and-box-of-the-holy-sand-box'Allows to persist options
wd config <key> [value]# output the current config and the help menu
wd config
# output the config value for the key 'clipboard'
wd config clipboardwd config clipboard truecopy the result to clipboard, when the command offers this option (same as allows passing -c,--clipboard)
# Default: false
wd config clipboard trueformat output as JSON when possible (same as allows passing -j,--json)
# Default: false
wd config json trueset the prefered language (same as allows passing -l,--lang)
# Default: process.env.LANG.slice(0, 2)
wd config lang nlset commands to verbose (same as allows passing -v,--verbose)
# Default: false
wd config verbose trueA javascript tool suite to query and work with Wikidata data, heavily used by wikidata-cli
Edit Wikidata from NodeJS, used in wikidata-cli for all write operations
A command-line tool to filter a Wikidata dump by claim
Tools to setup an ElasticSearch instance fed with subsets of Wikidata
A command-line tool to extract taxonomies from Wikidata
Do you know inventaire.io? It's a web app to share books with your friends, built on top of Wikidata! And its libre software too.



