download_spapi_report
Download a custom SP-API report file by its report_request_id. The bytes are returned gzip-compressed and base64-encoded — you must decompress them yourself before reading.
WORKFLOW:
Run execute_sql on the
custom_reportsview first to find the right report. Filter by report_type, date range, merchant_id, etc. The view exposes:report_request_id ← pass this to download_spapi_report
report_type (e.g. GET_FBA_INVENTORY_AGED_DATA)
file_size_bytes (compressed .gz size; NULL for legacy rows)
file_format (TSV / CSV / JSON / XML / TXT; NULL = unknown)
compression (currently always "gzip")
data_start_time / data_end_time / report_generated_ts / created_ts
download_path (you generally don't need this — call this tool)
Inspect file_size_bytes BEFORE calling. Decompressed size is typically 3-5× the compressed size for TSV/CSV, ~3× for JSON. If file_size_bytes looks large (above a few MB compressed), CHECK WITH THE USER before downloading — loading a big file into your context will consume a substantial chunk of your token budget. The user might prefer a summary, a narrower date range, or having the file saved to disk via a different flow.
Call download_spapi_report(report_request_id). The response contains bytes_b64 (base64 of the .gz bytes), file_format, compressed_size_bytes, filename_suggestion, and a
warningstring when the file is over 5 MB compressed. Ifwarningis set, surface it to the user before parsing.Decompress and parse. CRITICAL: decode the base64 string DIRECTLY to bytes in memory — do NOT write
bytes_b64to a file first and re-read it. Text-mode file writes can mangle base64 payloads in ways that still decode to a plausible byte count: a+getting URL-decoded to space, a newline inserted mid-string by a line-wrapper, or a single character dropped by a serializer will all produce bytes that look the right size but are byte-shifted in the middle. The downstream symptom is a gzip "CRC check failed" or garbled output, falsely blamed on a corrupt source file. Decode straight from the tool response: import base64, gzip data = gzip.decompress(base64.b64decode(result["bytes_b64"]))Parse by file_format:
TSV → csv.reader(text, delimiter='\t')
CSV → csv.reader(text)
JSON → json.loads(text) (often line-delimited — try .splitlines())
XML → xml.etree.ElementTree.fromstring(text)
TXT → str(text)
First line of TSV/CSV is usually the header row.
SIZE HANDLING:
The tool DOES NOT silently refuse big files. Files above 5 MB compressed
are still delivered, but the response includes a warning field telling
you the file is large. Read the warning to the user and confirm before
processing. The hard upper bound is ~500 MB compressed — above that the
tool refuses purely because the response can't be buffered safely server-
side. That refusal is a transport limit, not a policy decision. Such
files exist only as edge-cases; in practice ask the user to narrow the
date range or download the file via the customer portal directly.
ERRORS:
"report not found" run id is unknown OR belongs to another org
"still in progress" run hasn't reached DOWNLOADED status yet
"download disabled" report type contains PII for which the caller's org doesn't have download rights
"file expired" S3 lifecycle dropped the file (past retention)
"exceeds transport limit" compressed size > 500 MB — point user at the customer portal download path instead
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"reportRequestId": "<reportRequestId>"
},
"name": "download_spapi_report"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| reportRequestId | Yes | Numeric report_request_id from the `custom_reports` DH view (execute_sql first). Identifies the specific finished run whose .gz file should be returned. |