Accessing reconciliation API via

I am trying to access the https://wikidata.reconci.link/ reconciliation API through a Python script. I found a Python script here as an example. By looking at it and the Reconcilliation Service example for a reconciliation query, I assembled this minimal Python script:

import requests
import json

http = requests.Session()

reconciliation_endpoint = 'https://wikidata.reconci.link/en/api'

query_string = '''{
    "queries": [
      {
        "query": "Christel Hanewinckel",
        "type": "Q5",
        "limit": 5,
        "type_strict": "should"
      }
    ]
}'''

response = http.post(reconciliation_endpoint, data=json.loads(query_string))
print(json.dumps(response.json(), indent=2))

Using this script I got an error message from the server:

{
  "arguments": {
    "lang": "en",
    "queries": "query"
  },
  "details": "Expecting value: line 1 column 1 (char 0)",
  "message": "invalid query",
  "status": "error"
}

I am getting an API response, so an incorrect API URL doesn’t seem to be the problem (although the API doesn’t seem to follow the protocol specification, which says that the route should be /reconcile rather than /api). Based on the error message, the problem doesn’t seem to be a malformed query. Rather, the API seems to not be seeing the query JSON that I am posting. I didn’t see anything in the documentation about any particular required Content-Type headers or authorization being required. So I am mystified as to why it doesn’t work. I can get the other functions that are defined in the service definition (/suggest, /preview, etc.) to work, just not the reconcile function.

The “latest” reconciliation specification does (somewhat confusingly) not refer to the last version but to the latest draft. So to see the documentation for the latest version you need to check the 0.2 documentation:

https://reconciliation-api.github.io/specs/0.2/#reconciliation-queries

In practice the JSON structure is slightly different in 0.2:

import requests
import json

http = requests.Session()
reconciliation_endpoint = 'https://wikidata.reconci.link/en/api'

query_string = '''{
      "q0": {
        "query": "Christel Hanewinckel",
        "type": "Q5",
        "limit": 5,
        "type_strict": "should"
      }
}'''

response = http.post(reconciliation_endpoint, data=json.loads(query_string))
print(json.dumps(response.json(), indent=2))
1 Like

Thank you so much for the help, @abbe98 ! This got me unstuck. The final version of the script that worked was slightly different because I needed to make the query string be the value of the form element "queries":

import requests
import json

http = requests.Session()
reconciliation_endpoint = 'https://wikidata.reconci.link/en/api'

query_string = '''{
    "q0": {
        "query": "Christel Hanewinckel",
        "type": "Q5",
        "limit": 5,
        "type_strict": "should"
      }
}'''

payload = {'queries': query_string}

response = http.post(reconciliation_endpoint, data=payload)
print(json.dumps(response.json(), indent=2))

But otherwise the change in the form of the query string was what was needed. Thanks!

1 Like