I am trying to use the Wikimedia Api to get the corresponding Wikimedia Commons category to a specific Wikipedia page. I assume that it is possible as most Wikipedia pages include an "in other projects" - section in the sidebar which has a link that redirects to the Commons Category (for example: https://de.wikipedia.org/wiki/Albert_Einstein)
Thanks in advance.
You can do it in two API calls, the first call to German Wikipedia gets you the Wikidata Qid:
https://de.wikipedia.org/w/api.php?action=query&format=json&prop=wbentityusage&titles=Albert%20Einstein&wbeuprop=&wbeuaspect=
Which returns:
Then you can use the Wikidata API to get the name of the Commons category: https://www.wikidata.org/w/api.php?action=wbgetclaims&format=json&entity=Q937&property=P373
Which returns:
The value in datavalue is the name of the category. You get the full URL by just prepending
https://commons.wikimedia.org/wiki/Category:
https://commons.wikimedia.org/wiki/Category:Albert Einstein