When you want to get raw data from a report through an API, you can use the API call definition and request information directly from the database.
To fetch report data, follow these steps:
1 – Go to Menu> Analytics.
2 – Navigate to Reports, Custom reports or Goals.
3 – On the left, select the report you want to work with.
4 – Click the ⋯ three-dot icon next to the report you want to use.

5 – Click View API call definition.

6 – Here’s your API endpoint and API query.

7 – Find your Client IDand Client secret
- Go Menu> your email address.
- On the left, click API keys.
- Click Create a key.
- Name your key and click OK.
- Copy Client ID and Client secret. They won’t be available after you close this window.

8 – Call to get your access token.
In the code below, make the following changes:
<account-name>: Use your account name or use the custom account address <account-name>.example.com. For example, ‘salesdemo’ must be used here because, as you can see in the API endpoint above, our account name is called ‘salesdemo’.
import requests
# Make the POST request to get the access token
auth_response = requests.post(
"https://salesdemo.piwik.pro/auth/token",
headers={
'Content-Type': 'application/json'
},
json={
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret
}
)
# Check the status code and analyse the response
if auth_response.status_code == 200:
access_token = auth_response.json().get('access_token')
else:
print(f'Failed to get access token: {auth_response.status_code}')
print(auth_response.text)
9 – Call to get data
In the code below, make the following changes:
<website_id>: The website_id is already contained in the copied API query.
Use the copied API query as payload and the API endpoint as url.
# Endpoint and query for the API request
url = "https://salesdemo.piwik.pro/api/analytics/v1/query/"
payload = {
"date_from": start_date,
"date_to": end_date,
"website_id": "116515f4-9e4c-4ba7-ad97-a71a832db5c9",
"offset": 0,
"limit": 10,
"columns": [
{
"column_id": "referrer_type"
},
{
"column_id": "source_medium"
},
{
"column_id": "visitors"
},
{
"column_id": "sessions"
},
{
"column_id": "bounce_rate"
},
{
"column_id": "goal_conversions"
},
{
"column_id": "goal_conversion_rate"
},
{
"transformation_id": "sum",
"column_id": "goal_revenue"
}
]
}
# API request with the access token
response = requests.post(
url,
headers={
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
},
json=payload
)
# Check the status code and analyse the response
if response.status_code == 200:
response_data = response.json()
print('Data received successfully:')
print(response_data)
else:
print(f'Failed to get data: {response.status_code}')
print(response.text)