Forum Discussion
Fabric Create report API
- 7 months ago
Hello HeetParkhiya
Have you been able to run the Python script to create the payload and test it? I have provided the Python script in my earlier messages.
Here's the contents of my report.json, definition.pbir and .platform, modified slightly because I don't want to expose anything pertaining to my tenant.
report.json
{ "config": "{\"version\":\"5.68\",\"themeCollection\":{\"baseTheme\":{\"name\":\"CY25SU11\",\"version\":{\"visual\":\"2.4.0\",\"report\":\"3.0.0\",\"page\":\"2.3.0\"},\"type\":2}},\"activeSectionIndex\":0,\"defaultDrillFilterOtherVisuals\":true,\"settings\":{\"useNewFilterPaneExperience\":true,\"allowChangeFilterTypes\":true,\"useStylableVisualContainerHeader\":true,\"queryLimitOption\":6,\"exportDataMode\":1,\"useDefaultAggregateDisplayName\":true,\"useEnhancedTooltips\":true},\"objects\":{\"section\":[{\"properties\":{\"verticalAlignment\":{\"expr\":{\"Literal\":{\"Value\":\"'Top'\"}}}}}]}}", "layoutOptimization": 0, "resourcePackages": [ { "resourcePackage": { "disabled": false, "items": [ { "name": "CY25SU11", "path": "BaseThemes/CY25SU11.json", "type": 202 } ], "name": "SharedResources", "type": 2 } } ], "sections": [ { "config": "{}", "displayName": "Page 1", "displayOption": 1, "filters": "[]", "height": 720.00, "name": "7a9c9127a761dab1a1bb", "visualContainers": [], "width": 1280.00 } ] }definition.pbir
{ "$schema": "https://developer.microsoft.com/json-schemas/fabric/item/report/definitionProperties/2.0.0/schema.json", "version": "4.0", "datasetReference": { "byConnection": { "connectionString": "Data Source=\"powerbi://api.powerbi.com/v1.0/myorg/Fabric Workspace\";initial catalog=testCatalog;access mode=readonly;integrated security=ClaimsToken;semanticmodelid=838c6f17-025d-47cb-9dd5-11ce4b95db6d" } } }.platform
{ "$schema": "https://developer.microsoft.com/json-schemas/fabric/gitIntegration/platformProperties/2.0.0/schema.json", "metadata": { "type": "Report", "displayName": "legacy_report" }, "config": { "version": "2.0", "logicalId": "8b4d6212-7372-4427-bec7-78855f7a4cab" } }
Hello deborshi_nag,
I am going with the PBIR-Legacy format and It does not specify to use the .platform files? Also do you have a fully working payload that I can test/use or just generate a template from the power bi and use it to test it?
https://learn.microsoft.com/en-us/rest/api/fabric/articles/item-management/definitions/report-definition#definition-payload-example-using-pbir-legacy
Here's a python code you can use to generate the payload. Create a legacy format PBIR file using Power BI Desktop first! The payload will be written to the root directory.
You can use the "Try It" button and paste the payload in the below link using your Fabric/Power BI workspace GUID.
Items - Create Report - REST API (Report) | Microsoft Learn
#!/usr/bin/env python3
# PBIR-Legacy payload validator + builder (Fabric Items API - Reports)
import os, sys, json, base64
# --- CONFIG: Point to your PBIR-legacy project root (contains definition.pbir and report.json) ---
PBIP_ROOT = r"C:\Legacy Report\legacy_report.Report" # <-- UPDATE THIS to your legacy project folder
INCLUDE_PLATFORM = True # include .platform if present
INCLUDE_REGISTERED_RESOURCES = True # include files under RegisteredResources/**
INCLUDE_STATIC_RESOURCES = True # include files under StaticResources/**
# Optionally constrain resource file extensions to reduce payload bloat
RESOURCE_EXTENSIONS = {".json", ".jpg", ".jpeg", ".png", ".gif", ".pbiviz"}
def die(msg):
print(f"ERROR: {msg}", file=sys.stderr)
sys.exit(1)
def is_file(rel):
return os.path.isfile(os.path.join(PBIP_ROOT, rel))
def must_file(rel):
p = os.path.join(PBIP_ROOT, rel)
if not os.path.isfile(p):
die(f"Missing required file: {rel} at {p}")
return rel
def b64(rel):
with open(os.path.join(PBIP_ROOT, rel), "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")
def add_part(parts, rel):
# Enforce forward slashes in 'path' for the API
api_rel = rel.replace("\\", "/")
parts.append({"path": api_rel, "payload": b64(rel), "payloadType": "InlineBase64"})
print(f" + Added part: {api_rel}")
def walk_resources(root_rel, parts):
"""Add resource files beneath 'root_rel' (e.g., RegisteredResources or StaticResources)."""
root_abs = os.path.join(PBIP_ROOT, root_rel)
if not os.path.isdir(root_abs):
return
for dirpath, _, filenames in os.walk(root_abs):
for name in filenames:
ext = os.path.splitext(name)[1].lower()
if RESOURCE_EXTENSIONS and ext not in RESOURCE_EXTENSIONS:
continue
# Build relative path that mirrors the on-disk tree from PBIP_ROOT
rel_path = os.path.relpath(os.path.join(dirpath, name), PBIP_ROOT)
add_part(parts, rel_path)
# --- Validate PBIR-legacy layout ---
print(f"Using PBIP_ROOT: {PBIP_ROOT}")
must_file("definition.pbir")
must_file("report.json")
# --- Build parts ---
parts = []
print("\nAdding PBIR-legacy core parts:")
add_part(parts, "definition.pbir")
add_part(parts, "report.json")
# Optional .platform
if INCLUDE_PLATFORM and is_file(".platform"):
add_part(parts, ".platform")
# Optional RegisteredResources/** and StaticResources/**
# (themes, images, private custom visuals, etc.)
if INCLUDE_REGISTERED_RESOURCES:
print("\nScanning RegisteredResources/**")
walk_resources("RegisteredResources", parts)
if INCLUDE_STATIC_RESOURCES:
print("\nScanning StaticResources/**")
walk_resources("StaticResources", parts)
# --- Print summary and write payload.json ---
print("\nSummary:")
print(f" Total parts: {len(parts)}")
present_paths = [p["path"] for p in parts]
print(" Paths included:")
for p in present_paths:
print(" -", p)
# Confirm the specific required paths exist in parts
if "definition.pbir" not in present_paths:
die("CRITICAL: 'definition.pbir' was not included in parts[]")
if "report.json" not in present_paths:
die("CRITICAL: 'report.json' was not included in parts[]")
payload = {"definition": {"parts": parts}}
out_path = os.path.join(PBIP_ROOT, "payload.json")
with open(out_path, "w", encoding="utf-8") as f:
json.dump(payload, f, indent=2)
print(f"\nPayload written to: {out_path}")
Hope this helps - please appreciate leaving a Kudos or accepting as a Solution!