Chapter 8: Talking to SAP via APIs (Hands-On Project)

Build your first API-powered SAP-style report

Project Goal

We will simulate pulling SAP material data via an API and displaying it like an ALV report.

Instead of explaining APIs, we will BUILD one workflow step-by-step.

Step 1: Run Your First API Script

import requests url = "https://jsonplaceholder.typicode.com/posts" response = requests.get(url) data = response.json() print(data[:2])

What you should see

[ { "userId": 1, "id": 1, "title": "...", "body": "..." }, { "userId": 1, "id": 2, "title": "...", "body": "..." } ]

Step 2: Think Like ABAP

ABAPPython/API
SELECT *requests.get()
Internal Tabledata (list)
Work Arearow (dict)

Step 3: Simulated SAP OData Response

{ "d": { "results": [ {"Material":"1001","MaterialType":"FERT"}, {"Material":"1002","MaterialType":"HALB"} ] } }

Visual Mapping

Internal Table = results
Row = {"Material":"1001","MaterialType":"FERT"}

Step 4: Extract Data

records = data["d"]["results"] for row in records: print(row["Material"])

Expected Output

1001
1002

Step 5: Convert to Table

import pandas as pd df = pd.json_normalize(records) print(df.head())

Expected Table

Material | MaterialType
1001 | FERT
1002 | HALB

Step 6: Build a Mini SAP-like Report

import requests import pandas as pd url = "https://jsonplaceholder.typicode.com/posts" response = requests.get(url) data = response.json() df = pd.DataFrame(data) print(df[["id","title"]].head())

Expected Output

id | title
1 | ...
2 | ...

Step 7: Debug Like an Engineer

print(response.status_code) print(response.text)
If something fails, ALWAYS print the raw response first.

Checkpoint

End of Chapter 8