Skip to main content

Examples

These examples show how to use the API from Python and Power BI. The API also works with most standard ETL tools.

The examples use http://localhost:9090/clean/graphql and the header BLIKSUND-X-API-KEY. Replace them with your own URL, endpoint and header name (see How to use the API).

Python example​

This example requires Python and the requests package.

import requests

# Configuration
API_URL = "http://localhost:9090/clean/graphql"
API_KEY = "your-api-key"

# Set up headers
headers = {
"BLIKSUND-X-API-KEY": API_KEY,
"Content-Type": "application/json"
}

# Define your GraphQL query
query = """
{
records {
created_at
deleted_at
id
en_GB
metadata_inserted_at
}
}
"""

# Create request payload
payload = {"query": query}

# Send request
response = requests.post(API_URL, json=payload, headers=headers)

# Errors in the query are returned with status 200 and an "errors" list
if response.status_code != 200:
print(f"Error: {response.status_code}")
print(response.text)
else:
data = response.json()
if data.get("errors"):
print(data["errors"])
else:
records = data["data"]["records"]
print(f"Retrieved {len(records)} records")

# Print first few records
for record in records[:5]:
print(f"ID: {record['id']}, Created: {record['created_at']}")

Power BI Example: CData GraphQL Connector​

This is the easiest Power BI approach, but it requires a GraphQL connector with a paid subscription.

  1. Download the GraphQL Connector from the CData website and run the installation. If you get the error “This setup could not copy important connector files”, follow the solution in the installation guide.
  2. Set up the ODBC driver and DSN:
    1. On your machine, select Start > Search, and enter ODBC Data Sources in the search box.
    2. Choose CData PBI GraphQL Source under User DSN, and select Configure.
    3. Choose basic authentication, and enter the login details of your CData account and the Analytics API URL.
    4. Choose Advanced (under Connection Properties), find Custom Headers in the list, and enter your API key as BLIKSUND-X-API-KEY:<your-api-key>.
    5. Select OK twice to close the ODBC Data Source Administrator.
  3. Open Power BI.
  4. Go to Home > Get data > More and search for ODBC.
  5. In the ODBC window, choose CData PBI GraphQL Source as your DSN.
  6. When the connection works, you can view and open the tables from the API.

Power BI Example: Power Query​

This approach is more advanced, but uses only the Advanced Editor in Power BI, so there is no extra cost.

  1. Open Power BI.
  2. Select Home > Get data > Blank Query. This opens the Power Query Editor.
  3. In the editor, select Advanced Editor (in the Query menu).
  4. The query below is an example of how to load the results of a GraphQL query. Adjust the URL, query and API key to your needs.
let
url = "http://localhost:9090/clean/graphql",

query = "{ records { created_at deleted_at id en_GB metadata_inserted_at } }",
body = Json.FromValue([query = query]),

response = Web.Contents(url,
[
Headers = [
#"Content-Type" = "application/json",
#"BLIKSUND-X-API-KEY" = "your-api-key"
],
Content = body
]
),

json = Json.Document(response),
recordsList = json[data][records],

table = Table.FromRecords(recordsList)

in
table
  1. Select Done to close the Advanced Editor, and Close & Apply to close the Power Query Editor.
  2. The loaded data appears in the Data pane on the right side of the Power BI window.