Skip to main content

Go between M-IDs and filenames on Wikimedia Commons

Every file on Wikimedia Commons has two identifiers:

The APIs for searching/updating Wikimedia Commons typically accept both forms; sometimes it’s convenient to go between the two.

  1. To go from filename to numeric ID, you can use the Query API and do a search with the titles parameter. The M ID is included in the response.

    For example:

    curl \
      --get 'https://commons.wikimedia.org/w/api.php' \
      --data 'action=query' \
      --data 'format=xml' \
      --data-urlencode 'titles=File:Herestraat Groningen.JPG'
    
    <api batchcomplete="">
      <query>
        <pages>
          <page _idx="128" pageid="128" ns="6" title="File:Herestraat Groningen.JPG"/>
        </pages>
      </query>
    </api>
    
  2. To go from numeric ID to filename, go to https://commons.wikimedia.org/?curid=[ID].

    For example, https://commons.wikimedia.org/?curid=128.

    Alternatively, you can use the Query API with the pageids parameter. For example:

    curl \
      --get 'https://commons.wikimedia.org/w/api.php' \
      --data 'action=query' \
      --data 'format=xml' \
      --data 'pageids-128'
    
    <?xml version="1.0"?>
    <api batchcomplete="">
      <query>
        <pages>
          <page _idx="128" pageid="128" ns="6" title="File:Herestraat Groningen.JPG"/>
        </pages>
      </query>
    </api>
    

There’s a Python implementation of this code in Flickypedia, which includes some tests.