We’re working on this feature in beta. If you’re interested, reach out to: connect@statista.com for a beta key.

This guide provides a simple workflow example of the Research AI API.

Using your API key, submit a question to the POST endpoint described in the API Reference.

import requests
import time

url_post = "https://api.statista.ai/v1/research-ai/ask"

payload = {"question": "What are the economic impacts of climate change in Europe?"}
headers = {
    "x-api-key": "<your_research_ai_api_key>",
    "Content-Type": "application/json"
}

response = requests.request("POST", url_post, json=payload, headers=headers)

In the response we should receive a researchToken that can be used to poll the answer endpoint.

You should continue polling the answer endpoint until the response contains a status of “done” in the state.status field, indicating that the answer is ready.

research_token = response.json()['researchToken']
url_get = "https://api.statista.ai/v1/research-ai/answer"

querystring = {"research_token": research_token}

poll_attempts = 0
while poll_attempts < 10:
    response = requests.request("GET", url_get, headers=headers, params=querystring)
    print(response.status_code)
    if response.status_code != 200:
        break
    answer_status = response.json()['state']['status']
    poll_attempts += 1
    if answer_status == "done":
        print('Research AI has a generated answer ready for us!')
        break
    time.sleep(2)

On a successful poll, you’ll be able to fetch the answer from the payload:

print(response.json()['response']['answer'])

In addition you’ll be able to extract key facts and sources that the answer is based on:

for key_fact in response.json()['response']['keyFacts']:
    print(key_fact)