SDKs overview

Wikidata does not provide a single, monolithic official SDK. Instead, its open nature and reliance on standard web technologies like the SPARQL endpoint and the MediaWiki API have fostered a diverse ecosystem of community-developed libraries and tools. These libraries enable developers to interact with Wikidata's knowledge graph programmatically, supporting tasks such as querying entities, retrieving item properties, and performing data edits.

The primary methods for accessing Wikidata data include HTTP requests to the SPARQL endpoint for complex queries and the MediaWiki API for more granular interactions with items, properties, and lexemes. Given this architecture, SDKs and libraries typically wrap these underlying interfaces, providing more convenient, language-specific abstractions.

Official SDKs by language

While the Wikimedia Foundation maintains the core Wikidata infrastructure and provides extensive data access documentation, it does not offer a suite of officially supported client SDKs in the traditional sense. Developers typically interact with Wikidata through its HTTP-based SPARQL endpoint for querying or the MediaWiki API for data manipulation. This approach encourages the community to develop and maintain libraries tailored to specific programming languages and use cases.

The following table outlines common approaches and pseudo-SDKs, rather than officially published and maintained client libraries from Wikidata itself:

Language Package/Approach Install Command (Example) Maturity/Status
Python Wikidata-API (community) pip install wikidata-api Actively maintained community library for basic access
JavaScript/Node.js wikibase-sdk (community) npm install wikibase-sdk Widely used, actively maintained for Wikibase/Wikidata interaction
Java Wikidata Toolkit (community) Maven/Gradle dependency (see project site) Comprehensive, actively developed for data processing and access
PHP MediaWiki API Client (community) composer require mediawiki/api-client General MediaWiki API client, adaptable for Wikidata

Installation

Installation methods vary based on the programming language and the specific community library chosen. Below are examples for popular languages. Developers should consult the documentation for their chosen library for precise and up-to-date instructions.

Python example (wikidata-api)

To install wikidata-api, a community-maintained Python library for accessing Wikidata:

pip install wikidata-api

JavaScript/Node.js example (wikibase-sdk)

To install wikibase-sdk, a popular JavaScript library for interacting with Wikibase instances, including Wikidata:

npm install wikibase-sdk

Java example (Wikidata Toolkit)

For Java projects, the Wikidata Toolkit is often used. It's typically included as a Maven or Gradle dependency. An example Maven dependency might look like this (check the official project page for the latest version):

<dependency>
    <groupId>org.wikidata.wdtk</groupId>
    <artifactId>wdtk-datamodel</artifactId>
    <version>0.14.0</version> <!-- Replace with latest version -->
</dependency>

Quickstart example

This quickstart demonstrates fetching information about an entity (e.g., Q42 for Douglas Adams) using a common Python library, wikidata-api. This example illustrates how to retrieve labels, descriptions, and claims for a specific Wikidata item.

Python Quickstart (wikidata-api)

First, ensure you have the wikidata-api library installed:

pip install wikidata-api

Then, use the following Python code to fetch and print details for a Wikidata item:

from wikidata_api import WikidataAPI

# Initialize the WikidataAPI client
wikidata = WikidataAPI()

# Define the Wikidata item ID for Douglas Adams
item_id = 'Q42' 

try:
    # Fetch the item data
    item = wikidata.get_item(item_id)

    if item:
        print(f"Item ID: {item.id}")
        print(f"Label (English): {item.label}")
        print(f"Description (English): {item.description}")
        print("\nClaims:")
        for claim_id, claims_list in item.claims.items():
            for claim in claims_list:
                # Attempt to get the main value of the claim
                # This is a simplified representation; real-world parsing might be more complex
                value = "N/A"
                if hasattr(claim, 'mainsnak') and hasattr(claim.mainsnak, 'datavalue'):
                    if claim.mainsnak.datavalue and hasattr(claim.mainsnak.datavalue, 'value'):
                        value = claim.mainsnak.datavalue.value
                print(f"  {claim_id}: {value}")
    else:
        print(f"Item with ID {item_id} not found.")

except Exception as e:
    print(f"An error occurred: {e}")

This script connects to the Wikidata API, retrieves the specified item, and prints its label, description, and a simplified view of its claims. For more advanced querying, developers often interact directly with the Wikidata SPARQL endpoint.

Community libraries

The open and collaborative nature of Wikidata has led to a rich collection of community-contributed libraries across various programming languages. These libraries often address specific use cases, from simple data retrieval to complex data manipulation and integration with other semantic web tools. Developers should evaluate these options based on their project requirements, language preference, and the library's maintenance status and documentation.

Python

  • Wikidata-API: A Python wrapper for the Wikidata API, simplifying item and property retrieval.
  • Pywikibot: A comprehensive bot framework for MediaWiki sites, including Wikidata, used for automated editing and data management. It's extensively documented on the Wikidata Pywikibot page.
  • SPARQLWrapper: A generic Python wrapper around a SPARQL endpoint, suitable for constructing and executing complex SPARQL queries against Wikidata. This is a foundational library for many semantic web applications, as described in W3C SPARQL Protocol documentation.

JavaScript/Node.js

  • wikibase-sdk: Provides utilities to build and parse Wikibase API and SPARQL queries and responses. It's highly adaptable for both client-side and server-side applications.
  • wikidata-sdk: Another JavaScript library offering functions to convert Wikidata IDs, build URLs, and query the API.
  • sparql-http-client: A minimal HTTP client for SPARQL endpoints, useful for direct querying without higher-level abstractions.

Java

  • Wikidata Toolkit (WDTK): A Java library for processing data from Wikidata. It supports reading dumps, accessing the API, and working with the Wikidata data model. It's valuable for large-scale data analysis and integration.
  • RDF4J (formerly Sesame): A framework for processing RDF data, which can be used to interact with Wikidata's SPARQL endpoint and parse RDF dumps. More information is available on the RDF4J project site.

PHP

  • MediaWiki API Client: A PHP client for interacting with the MediaWiki API, which Wikidata extends. It allows for programmatic access to items, properties, and other data.
  • Wikidata Query Service Client: A PHP library specifically designed to interact with the Wikidata Query Service (SPARQL endpoint).

When selecting a community library, consider factors such as active maintenance, community support, documentation quality, and compatibility with the specific Wikidata features required for your project.