Getting started overview
Wikidata serves as a central, openly licensed knowledge base, providing structured data that supports various applications, from semantic web projects to natural language processing. Unlike many commercial APIs, Wikidata does not require API keys or a formal signup process for read-only access to its data. Its data is freely available for use under a Creative Commons CC0 Public Domain Dedication, meaning there are no restrictions on its use, even for commercial purposes. This open access model simplifies the initial setup for developers who primarily need to query and retrieve data.
The primary method for programmatic interaction with Wikidata is through its SPARQL query service, which allows for complex queries against the knowledge graph. Additionally, Wikidata exposes a MediaWiki API for more direct item and property access, consistent with other Wikimedia projects. This guide focuses on getting started with querying via SPARQL, as it offers the most flexible and powerful way to extract structured information.
Quick Reference Steps
The following table outlines the essential steps to begin interacting with Wikidata programmatically:
| Step | What to Do | Where |
|---|---|---|
| 1. Understand Access | No account or API keys needed for read-only data access. | Wikidata Data Access documentation |
| 2. Choose Query Method | Decide between SPARQL endpoint for complex queries or MediaWiki API for direct item/property access. | Wikidata SPARQL Query Service or MediaWiki API documentation |
| 3. Construct Query | Formulate a SPARQL query or API request. | Wikidata Query Service interface |
| 4. Execute Request | Send your query/request to the appropriate endpoint. | Programmatic client (e.g., Python requests, JavaScript fetch) |
| 5. Process Response | Parse the JSON or XML response. | Your application logic |
Create an account and get keys
For read-only access to Wikidata's data, developers do not need to create an account or obtain API keys. Wikidata's data is publicly available and accessible without authentication. This simplifies the onboarding process significantly, allowing immediate access to the SPARQL endpoint and the MediaWiki API for data retrieval.
If you intend to contribute data to Wikidata (edit existing items, create new ones), then a Wikidata user account is required. However, such contributions fall outside the scope of a typical 'getting started with data access' guide and are primarily for editors, not API consumers. For most programmatic uses, such as building applications that consume Wikidata's knowledge, an account is not necessary.
Your first request
The most common way to interact with Wikidata programmatically is through its SPARQL endpoint. SPARQL (SPARQL Protocol and RDF Query Language) is a W3C standard query language for databases that can be queried via the Semantic Web. Wikidata's Query Service provides a web interface for constructing and testing queries, which can then be adapted for programmatic use.
Example: Finding all instances of a specific entity (e.g., rivers)
This example demonstrates how to query for items that are instances of 'river' (Q4022). The query will return the Wikidata item ID and its English label.
1. Construct the SPARQL Query
The following SPARQL query selects items that are an instance of 'river' (P31 is the property for 'instance of', Q4022 is the item for 'river'). It also retrieves the English label for each item.
SELECT ?river ?riverLabel WHERE {
?river wdt:P31 wd:Q4022.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} LIMIT 10
SELECT ?river ?riverLabel: Specifies that we want to retrieve the variable?river(the item ID) and its associated label?riverLabel.?river wdt:P31 wd:Q4022.: This is the core triple pattern. It means "find items (?river) where the property 'instance of' (wdt:P31) has the value 'river' (wd:Q4022)".SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }: This special service retrieves the human-readable labels for the items found, prioritizing the user's interface language and falling back to English.LIMIT 10: Restricts the results to the first 10 entries for demonstration purposes.
2. Send the Request Programmatically
You can send this query to the Wikidata SPARQL endpoint using an HTTP GET request. The endpoint is https://query.wikidata.org/sparql. The query should be URL-encoded and passed as the query parameter.
Python Example:
import requests
import json
sparql_query = """SELECT ?river ?riverLabel WHERE {
?river wdt:P31 wd:Q4022.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} LIMIT 10"""
endpoint_url = "https://query.wikidata.org/sparql"
headers = {
'Accept': 'application/sparql-results+json'
}
params = {
'query': sparql_query
}
try:
response = requests.get(endpoint_url, headers=headers, params=params)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
data = response.json()
print("First 10 rivers found:")
for binding in data['results']['bindings']:
river_id = binding['river']['value'].split('/')[-1]
river_label = binding['riverLabel']['value']
print(f" ID: {river_id}, Label: {river_label}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
except json.JSONDecodeError:
print("Failed to decode JSON response.")
JavaScript (Node.js/Browser) Example:
const sparqlQuery = `SELECT ?river ?riverLabel WHERE {
?river wdt:P31 wd:Q4022.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} LIMIT 10`;
const endpointUrl = 'https://query.wikidata.org/sparql';
async function queryWikidata() {
try {
const response = await fetch(endpointUrl + `?query=${encodeURIComponent(sparqlQuery)}`, {
headers: {
'Accept': 'application/sparql-results+json'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log("First 10 rivers found:");
data.results.bindings.forEach(binding => {
const riverId = binding.river.value.split('/').pop();
const riverLabel = binding.riverLabel.value;
console.log(` ID: ${riverId}, Label: ${riverLabel}`);
});
} catch (error) {
console.error(`An error occurred: ${error}`);
}
}
queryWikidata();
3. Interpret the Response
The response will be a JSON object (or XML, depending on the Accept header) containing the query results. For JSON, the relevant data is typically found under results.bindings, where each element is an object representing a row of results, with keys corresponding to the variables in your SELECT clause (e.g., river, riverLabel).
Each variable's value is an object containing its value (the actual data, often a URL for entities) and type (e.g., uri, literal). For URIs, you often need to extract the last part of the URL to get the Wikidata item ID (e.g., Q4022).
Common next steps
After successfully making your first request, consider these common next steps to deepen your integration with Wikidata:
- Explore More SPARQL Queries: The Wikidata Query Service offers many example queries. Experiment with different properties and items to understand the structure of Wikidata's data. Resources like the Wikidata SPARQL tutorial can provide a deeper understanding of the query language.
- Utilize the MediaWiki API: For specific item or property data retrieval, or to perform actions like fetching revisions, the MediaWiki API might be more suitable than SPARQL. This API is consistent with other Wikimedia projects and offers direct access to item data in JSON format.
- Understand Wikidata Identifiers: Familiarize yourself with Wikidata's Q-numbers (for items like Q4022 for 'river') and P-numbers (for properties like P31 for 'instance of'). These are fundamental to navigating and querying the knowledge base effectively. The Wikidata Help page on Items provides more detail.
- Data Downloads: For large-scale data analysis or offline processing, consider using the Wikidata data dumps. These are available in various formats, including RDF, and can be downloaded for local processing.
- Integrate with Libraries: For more complex applications, consider using existing libraries or frameworks that simplify interaction with SPARQL endpoints or Wikidata's API. For example, in Python, libraries like RDFLib can help manage RDF data and SPARQL queries.
- Explore Linked Data Principles: Wikidata is a prominent example of Linked Data. Understanding W3C's Linked Data principles can provide context for how Wikidata is structured and how it interoperates with other datasets on the Semantic Web.
Troubleshooting the first call
When making your first programmatic request to Wikidata, you might encounter some common issues. Here's how to troubleshoot them:
- Incorrect Endpoint URL: Ensure you are using the correct SPARQL endpoint URL (
https://query.wikidata.org/sparql). Mistakes in the URL will result in connection errors. - Improperly Encoded Query: The SPARQL query must be URL-encoded when passed as a parameter in an HTTP GET request. If characters like spaces,
#,?, or&are not encoded, the request URL will be malformed, leading to syntax errors or unexpected results. Libraries like Python'srequestsor JavaScript'sencodeURIComponenthandle this automatically when using theparamsargument or explicitly encoding. - Incorrect
AcceptHeader: Specify theAcceptheader to request the desired output format. For JSON results, useapplication/sparql-results+json. If this header is missing or incorrect, the server might return a default format (e.g., XML) or an error. - SPARQL Syntax Errors: Even a small typo in your SPARQL query can cause a server error. Use the Wikidata Query Service web interface to test your query first. It provides immediate feedback on syntax and helps debug logical issues before integrating into your code.
- Network Issues: Verify your internet connection and ensure no firewalls or proxies are blocking access to
query.wikidata.org. - Rate Limiting: While Wikidata is generally permissive, excessive requests in a short period might lead to temporary rate limiting. If you receive
429 Too Many Requestserrors, introduce delays between your requests or reduce their frequency. The Wikidata Data Access documentation on rate limits provides more details. - Empty Results: If your query returns no results, check the following:
- Correct Item/Property IDs: Ensure you are using the correct Q-numbers for items and P-numbers for properties. You can search for these on the main Wikidata website.
- Query Logic: Review your SPARQL query logic. Are the triple patterns correctly formed? Are you filtering correctly?
- Data Availability: The specific data you are looking for might not exist in Wikidata, or it might be structured differently than you expect.