SDKs overview

The Wikipedia ecosystem, supported by the Wikimedia Foundation, provides access to its content and data primarily through the MediaWiki API. This API allows programmatic interaction with Wikipedia, Wikidata, and other Wikimedia projects. To simplify development, both official and community-maintained SDKs (Software Development Kits) and client libraries are available across various programming languages. These tools streamline the process of querying Wikipedia's vast datasets, retrieving article content, performing searches, and interacting with user data, all while abstracting the underlying HTTP requests and JSON parsing.

Developers commonly use these SDKs for applications requiring access to general knowledge, natural language processing (NLP) research, educational tools, and data science projects that leverage structured and unstructured information available on Wikipedia. The MediaWiki API itself is designed to be comprehensive, offering extensive methods for data retrieval and manipulation. The client libraries further enhance the developer experience by providing idiomatic language constructs and handling common tasks such as authentication (though most read operations do not require it) and rate limiting considerations MediaWiki API etiquette.

Official SDKs by language

While the MediaWiki project itself does not provide a single 'official' SDK that covers all languages, the primary interaction method is direct use of the MediaWiki API. However, there are widely recognized and maintained libraries that are de facto standard for interacting with Wikipedia's content. The Python mwapi library is a low-level client for the MediaWiki API, providing direct access to its functionalities. The Python wikipedia-api and JavaScript wikipedia-api libraries offer higher-level abstractions specific to Wikipedia content retrieval, simplifying common tasks for developers focused on consuming encyclopedia data.

Widely Used Wikipedia Client Libraries
Language Package Name Installation Command Description
Python mwapi pip install mwapi A low-level, async-first MediaWiki API client, suitable for complex interactions and bot development.
Python wikipedia-api pip install wikipedia-api High-level wrapper for Wikipedia API, focused on easy content retrieval, parsing, and searching.
JavaScript wikipedia-api npm install wikipedia-api A JavaScript library for fetching, parsing, and interacting with Wikipedia content programmatically.

Installation

Installing Wikipedia client libraries typically involves using the package manager specific to the programming language. These commands fetch the library from its respective repository and make it available for use in your development environment.

Python

For Python, the pip package installer is used. To install the mwapi library for direct MediaWiki API interaction:

pip install mwapi

To install the higher-level wikipedia-api library for simpler Wikipedia content access:

pip install wikipedia-api

JavaScript (Node.js/npm)

For JavaScript projects running in Node.js environments, npm (Node Package Manager) is the standard tool. To add the wikipedia-api library to your project:

npm install wikipedia-api

After installation, the libraries can be imported into your code, providing access to their functions and classes for interacting with Wikipedia content.

Quickstart example

The following examples demonstrate how to retrieve a Wikipedia page's summary and full content using the Python and JavaScript wikipedia-api libraries. These snippets illustrate common use cases for developers seeking to integrate Wikipedia data into their applications.

Python quickstart

This Python example shows how to fetch the summary and full text of a Wikipedia page using the wikipedia-api library:


import wikipediaapi

wiki_wiki = wikipediaapi.Wikipedia('en') 

def print_page_info(page_title):
    page_py = wiki_wiki.page(page_title)

    if page_py.exists():
        print(f"Page - Title: {page_py.title}")
        print(f"Page - Summary: {page_py.summary[0:500]}...")
        # print(f"Page - Full Text: {page_py.text}") # Uncomment for full content
    else:
        print(f"Page '{page_title}' does not exist.")

print_page_info('Python (programming language)')
print_page_info('NonExistentPage123')

JavaScript quickstart

This JavaScript example demonstrates fetching a page summary and full content from Wikipedia using the wikipedia-api library in a Node.js environment:


const wikipedia = require('wikipedia-api');

async function getWikipediaPageInfo(pageTitle) {
    try {
        const page = await wikipedia.page(pageTitle);
        if (page.exists()) {
            console.log(`Page - Title: ${page.title}`);
            const summary = await page.summary();
            console.log(`Page - Summary: ${summary.slice(0, 500)}...`);
            // const fullText = await page.content(); // Uncomment for full content
            // console.log(`Page - Full Text: ${fullText}`);
        } else {
            console.log(`Page '${pageTitle}' does not exist.`);
        }
    } catch (error) {
        console.error(`Error fetching page '${pageTitle}':`, error);
    }
}

getWikipediaPageInfo('JavaScript');
getWikipediaPageInfo('NonExistentPage456');

Community libraries

Beyond the commonly used clients, the open-source nature of Wikipedia and the MediaWiki API has fostered a diverse ecosystem of community-developed libraries. These libraries often target specific use cases, provide bindings for less common programming languages, or offer advanced features built on top of the basic API interactions. While not officially maintained by the Wikimedia Foundation, many community libraries are actively supported by their creators and user communities, offering valuable tools for developers.

Some prominent examples and categories include:

  • Language-specific wrappers: Libraries exist for languages like Ruby, PHP, Java, and Go, providing idiomatic interfaces to the MediaWiki API. These can often be found on platforms like GitHub or language-specific package repositories.
  • Data processing and NLP tools: Libraries that specifically focus on parsing Wikipedia dumps, extracting structured data, or performing natural language processing on article content. For instance, the Google Knowledge Graph API is an alternative for structured data, but Wikipedia-focused NLP tools directly process the raw text.
  • Bot frameworks: Specialized libraries designed to build bots for automated tasks on Wikipedia, such as content moderation, data synchronization, or inter-wiki linking. These often require advanced understanding of MediaWiki's internal structure and user permissions. Developers interested in building bots can refer to the MediaWiki Bots manual for detailed guidelines and recommended practices.

When choosing a community library, it is advisable to check its documentation, active development status, and community support to ensure it meets project requirements and reliability standards. The MediaWiki API documentation often lists various community libraries and tools that integrate with its services.