Getting started overview

Getting started with the Wikipedia API, based on the MediaWiki API, primarily involves understanding its structure and making HTTP requests. Unlike many commercial APIs, direct API keys are generally not required for read-only access to Wikipedia's public content. This simplifies the initial setup process for developers looking to retrieve information, perform searches, or analyze article data. The API supports various formats, including JSON, XML, and plain text, making it versatile for different application needs.

The core of Wikipedia's API interaction revolves around constructing appropriate URL queries to access specific data points. For more complex operations, such as editing or user-specific actions, authentication via OAuth may be necessary. However, for most data retrieval tasks, direct unauthenticated requests are sufficient. Developers can choose to interact with the API directly using standard HTTP libraries or leverage community-developed SDKs for popular languages like Python and JavaScript, which abstract away some of the complexities of URL construction and response parsing.

This guide focuses on the immediate steps to make your first successful API call, covering account creation (if relevant for advanced use cases), and demonstrating basic content retrieval. For a comprehensive understanding of all available modules and parameters, refer to the official MediaWiki API documentation.

Create an account and get keys

For most read-only access to Wikipedia content via the MediaWiki API, creating an account and obtaining API keys are not required. The API is designed to be publicly accessible for data retrieval without authentication. This means developers can immediately begin making requests to fetch article content, search for pages, or query metadata without any preliminary registration steps.

When an account might be useful

While not mandatory for basic data retrieval, creating a Wikipedia account (which is free) can be beneficial for specific advanced use cases:

  • Editing and Contributions: If your application intends to contribute to Wikipedia (e.g., automated edits, bot operations), an authenticated account is necessary. This typically involves logging in via the API and often requires specific bot flags or permissions.
  • Higher Rate Limits: Although public rate limits are generous, authenticated users or registered bots might be granted higher limits for extensive data scraping or frequent requests.
  • Personalized Data: Accessing user-specific data or watchlists, if applicable, would require an authenticated session.

Summary of getting started steps

The following table outlines the initial steps for accessing the Wikipedia API:

Step What to Do Where
1. Review Documentation Understand API endpoints and parameters. MediaWiki API Main Page
2. Decide on Authentication Determine if unauthenticated (read-only) or authenticated (write/advanced read) access is needed. MediaWiki API Login Documentation
3. Choose a Method Direct HTTP requests or use an SDK (Python, JavaScript). MediaWiki API Client Code

For the purposes of this getting started guide, we will proceed with unauthenticated read-only access, as it covers the majority of initial use cases and requires no prior setup beyond an internet connection.

Your first request

Making your first request to the Wikipedia API involves constructing a URL that specifies the desired action, format, and parameters. The primary endpoint for the MediaWiki API is https://en.wikipedia.org/w/api.php for the English Wikipedia. Other language Wikipedias use similar URLs with different subdomains (e.g., es.wikipedia.org for Spanish).

Example: Retrieving page content

Let's retrieve the plain text content of the page titled "API" from English Wikipedia. We'll use the action=query module with prop=extracts to get a summary, and format=json for a structured response.

GET https://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=API&format=json&exintro=1&explaintext=1

Breakdown of parameters:

  • action=query: Specifies that we want to query for information.
  • prop=extracts: Requests the introductory text (extract) of the page.
  • titles=API: Specifies the title of the page to retrieve.
  • format=json: Sets the response format to JSON.
  • exintro=1: Limits the extract to the introductory section only.
  • explaintext=1: Returns the extract as plain text, stripping HTML.

Using curl to make the request

You can test this request directly from your terminal using curl:

curl "https://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=API&format=json&exintro=1&explaintext=1"

Expected JSON response structure

A successful response will return a JSON object containing page data, including the extract. The exact structure can vary, but generally, you'll find the content nested under query.pages, with a page ID as a key.

{
  "batchcomplete": "",
  "query": {
    "pages": {
      "618": {
        "pageid": 618,
        "ns": 0,
        "title": "API",
        "extract": "In computer programming, an application programming interface (API) is a way for two or more computer programs or components to communicate with each other. It is a type of software interface, offering a service to other pieces of software."
      }
    }
  }
}

Using Python to make the request

For programmatic access, Python is a common choice. The requests library simplifies HTTP interactions.

import requests

URL = "https://en.wikipedia.org/w/api.php"

params = {
    "action": "query",
    "prop": "extracts",
    "titles": "API",
    "format": "json",
    "exintro": True,
    "explaintext": True
}

response = requests.get(URL, params=params)
data = response.json()

# Extract the page ID and then the extract content
page_id = next(iter(data["query"]["pages"]))
extract = data["query"]["pages"][page_id]["extract"]

print(f"Page Title: {data['query']['pages'][page_id]['title']}")
print(f"Extract: {extract}")

Using JavaScript (Node.js or browser)

For JavaScript environments, fetch is a standard way to make HTTP requests.

const URL = "https://en.wikipedia.org/w/api.php";

const params = new URLSearchParams({
    action: "query",
    prop: "extracts",
    titles: "API",
    format: "json",
    exintro: "1",
    explaintext: "1"
});

fetch(`${URL}?${params.toString()}`)
    .then(response => response.json())
    .then(data => {
        const pageId = Object.keys(data.query.pages)[0];
        const page = data.query.pages[pageId];
        console.log(`Page Title: ${page.title}`);
        console.log(`Extract: ${page.extract}`);
    })
    .catch(error => console.error("Error fetching data:", error));

Common next steps

Once you've successfully made your first request, consider these common next steps to further integrate with the Wikipedia API:

  • Search Functionality: Explore the action=query&list=search module to implement search capabilities within your application. This allows users to find Wikipedia articles based on keywords, similar to the main Wikipedia search bar. For details, consult the MediaWiki API search documentation.
  • Page Revisions and History: Access historical versions of pages using prop=revisions. This is useful for tracking changes, understanding content evolution, or data analysis over time. The revisions API reference provides comprehensive options.
  • External Links and Categories: Query for external links on a page (prop=extlinks) or categories a page belongs to (prop=categories). This helps in building knowledge graphs or content classification systems.
  • Image and Media Access: Retrieve information about images and other media files associated with Wikipedia articles using prop=images or directly querying Wikimedia Commons via its API, which is also based on MediaWiki.
  • SDK Exploration: For more complex projects, consider using a high-level SDK like wikipedia-api for Python or wikipedia-api for JavaScript. These libraries often provide more ergonomic methods for common tasks, reducing boilerplate code.
  • Rate Limit Management: While Wikipedia's rate limits are generous for unauthenticated access, it's good practice to implement exponential backoff and respect Retry-After headers in your application, especially for high-volume data collection. The API etiquette guidelines offer best practices.
  • Data Parsing and Cleaning: Wikipedia content can contain MediaWiki markup. For cleaner text, ensure you use parameters like explaintext=1. For more advanced parsing, consider libraries that can process MediaWiki syntax or HTML if you opt for prop=revisions&rvprop=content without explaintext.

Troubleshooting the first call

Encountering issues with your first API call is common. Here are some troubleshooting tips for the Wikipedia API:

  • Incorrect URL or Endpoint: Double-check the base URL (e.g., https://en.wikipedia.org/w/api.php) and ensure there are no typos. Different language Wikipedias have different subdomains.
  • Malformed Parameters: Verify that all parameters are correctly spelled and have valid values. For example, action=query is correct, not action=queries. Consult the MediaWiki API documentation for exact parameter names.
  • Missing Required Parameters: Some actions require specific parameters. For instance, action=query needs at least one list (list=) or property (prop=) parameter.
  • Network Issues: Ensure your internet connection is stable and that no firewalls or proxies are blocking your outgoing HTTP requests. Test with a simple request to another public API (e.g., Cloudflare API endpoints list) to confirm network connectivity.
  • JSON Parsing Errors: If you receive a response but your code fails to parse it, check the response content. It might be an HTML error page from a server issue, or the JSON structure might be different than expected. Use a JSON linter or pretty-printer to inspect the raw response.
  • Rate Limit Exceeded: Although unlikely for a single first call, repeated rapid requests can trigger rate limiting. The API will typically return an error message indicating this, often with a Retry-After header. Wait for the specified duration before retrying.
  • Page Not Found: If you're querying for a specific title, ensure the page exists and the title is spelled correctly (case-sensitive for some parameters). The API might return an empty pages object or a missing flag within the page data.
  • Using an Outdated SDK: If you're using a community SDK, ensure it's up-to-date. Outdated versions might not support newer API features or correctly handle responses.
  • Error Messages in Response: Always inspect the API response for explicit error messages. The MediaWiki API often includes descriptive error codes and messages within the JSON response under an error key.