Link Search Menu Expand Document

Step-by-Step Guide: Using the Spaaza API to Push Data to Power BI (BETA)

This is an example guide to show how a developer can use the Spaaza API to fetch voucher data and push it to Power BI for visualization and reporting.


Step 1: Understand the Data Flow

  1. Fetch Data from Your API: Use the Spaaza vouchers export API endpoint to retrieve voucher data based on specific criteria (e.g., vouchers created after a certain date).
  2. Transform Data: Format the fetched data to match the schema of a Power BI dataset.
  3. Push Data to Power BI: Use Power BI’s Push Dataset API to insert the transformed data.

Step 2: Fetch Voucher Data

  1. Make a POST Request: Use a tool like cURL or a library like axios (JavaScript) or requests (Python) to query your API. For example:
    curl 'https://services.spaaza.com/segments/query/vouchers?from=0&size=20' \
    -X 'POST' \
    -H 'Content-Type: application/json' \
    -H 'session-key: YOUR_SESSION_KEY' \
    --data-binary '{
        "entity": "vouchers",
        "index": "vouchers",
        "chain_id": 1796,
        "rules": [
            {
                "field_name": "date_time_creation",
                "operator": "is_after_date",
                "start_date": "2024-01-01T00:05:00+01:00"
            }
        ]
    }'
    

    Exporting other objects from Spaaza works in a similar way. For example:

    • to export people (users) use: /segments/query/vouchers
    • to export wallet entries use: /segments/query/wallet-ledger
    • to export transactions use: /segments/query/transactions

    If you are using the Staging environment use https://services-test01.spaaza.com/..

  2. Response: Parse the JSON response to extract the relevant fields.

Step 3: Prepare Power BI Dataset

  1. Define the Dataset Schema: Decide on the structure of your Power BI dataset. For example:
    {
        "name": "VouchersDataset",
        "tables": [
            {
                "name": "Vouchers",
                "columns": [
                    { "name": "VoucherID", "dataType": "Int64" },
                    { "name": "Title", "dataType": "String" },
                    { "name": "Description", "dataType": "String" },
                    { "name": "CreationDate", "dataType": "DateTime" },
                    { "name": "ExpiryDate", "dataType": "DateTime" },
                    { "name": "Status", "dataType": "String" }
                ]
            }
        ]
    }
    
  2. Transform the Data: Map fields from your API response to the Power BI schema:
    • idVoucherID
    • titleTitle
    • descriptionDescription
    • date_time_creationCreationDate
    • date_time_expiryExpiryDate
    • statusStatus

Step 4: Push Data to Power BI

  1. Create a Dataset: If the dataset does not already exist in Power BI, use the Create Dataset API:
    POST https://api.powerbi.com/v1.0/myorg/datasets
    Authorization: Bearer YOUR_ACCESS_TOKEN
    Content-Type: application/json
    
    {
        "name": "VouchersDataset",
        "tables": [
            {
                "name": "Vouchers",
                "columns": [
                    { "name": "VoucherID", "dataType": "Int64" },
                    { "name": "Title", "dataType": "String" },
                    { "name": "Description", "dataType": "String" },
                    { "name": "CreationDate", "dataType": "DateTime" },
                    { "name": "ExpiryDate", "dataType": "DateTime" },
                    { "name": "Status", "dataType": "String" }
                ]
            }
        ]
    }
    
  2. Insert Rows into the Dataset: Push the transformed data to Power BI using the Add Rows API:
    POST https://api.powerbi.com/v1.0/myorg/datasets/{dataset_id}/tables/Vouchers/rows
    Authorization: Bearer YOUR_ACCESS_TOKEN
    Content-Type: application/json
    
    {
        "rows": [
            {
                "VoucherID": 3074801,
                "Title": "Kafe + Muffin me Çokollatë",
                "Description": "Shijoni kombinimin e përsosur...",
                "CreationDate": "2025-01-20T07:00:48+00:00",
                "ExpiryDate": "2025-01-23T07:00:48+00:00",
                "Status": "generated"
            }
        ]
    }
    

Step 5: Automate the Workflow

  1. Schedule Data Fetching:
    • Use a cron job, cloud scheduler, or a background worker to periodically fetch new vouchers from your API.
  2. Retry Mechanism:
    • Implement retries with exponential backoff to handle temporary API failures.
  3. Batch Processing:
    • Fetch vouchers in batches to stay within Power BI’s API limits.

Step 6: Visualize the Data

  1. Log in to Power BI and create a new dashboard.
  2. Select your VouchersDataset and build visualizations such as:
    • A table to list vouchers.
    • A timeline to display voucher creation dates.
    • Aggregated metrics, e.g., total vouchers by status.

This approach ensures seamless integration between Spaaza and Power BI for real-time reporting and analysis. A similar approach can also be followed to fetch and send data to other BI and analytics tools.