INTRO O
PROBLEM SOLVING AND
PROGRAMMING IN PYTHON
(use the Space key to navigate through all slides)

Prof. Andrea Gallegati |
Prof. Dario Abbondanza |

CIS 1051 - APIs

Understanding APIs
in Python
What Is an API ?
Imagine you order a coffee at a café:
- You ask the barista for a coffee.
- The barista prepares it.
- You receive the coffee.
APIs (Application Programming Interface) works the same way:
- You request something.
- The system processes it.
- You receive the result.
Why Do We Need APIs?
Allow different programs to communicate efficiently.
They bridge between software components, making reusability, modularity, and automation easier.
... in a word integration !
✅ Operating System APIs
- Python code interacts with file systems:
open()
,read()
,write()
✅ Library APIs
- import and use pre-built functions from modules.
Examples: APIs in Everyday Life
with open("file.txt", "r") as file:
data = file.read()
print(data)
import math
# Uses math API to
# calculate the square root
print(math.sqrt(25))
✅ Hardware APIs
- Programs interact with sensors, cameras, or printers.
Examples: APIs in Everyday Life
import RPi.GPIO as GPIO
# Turn on a device connected to pin 18
GPIO.output(18, GPIO.HIGH)
APIs make software modular
no need to rebuild everything from scratch!
Web APIs:
Extending APIs to the Internet
A Web API allows:
- Programs to request data from online services.
- Websites, apps, and databases to "talk" to each other.
-
Integration of third-party services
(Google Maps, Twitter, etc.).
💡 Example: Weather Apps
- You type "Weather in Rome" in a weather app.
- The app sends a request to a weather API.
- The API returns the temperature, humidity, etc.
HTTP Requests:
Communicating with Web APIs
APIs on the web use HTTP requests to exchange data.
Types of HTTP Requests
Method | What It Does |
---|---|
GET | Retrieve data (e.g., weather, user details) |
POST | Send new data (e.g., register a user) |
PUT | Update existing data (e.g., change user email) |
DELETE | Remove data (e.g., delete a user account) |
Example: GET Request (Fetching Data)
🔹 This requests GitHub API data and prints the response.
import requests
response = requests.get("https://api.github.com")
print(response.json()) # Convert response to a dictionary
REST APIs vs. Non-REST APIs
Most modern APIs follow the REST model (Representational State Transfer), but not all.
REST API Characteristics
✅ Uses standard HTTP methods (GET, POST, etc.).
✅ Returns structured data (usually JSON).
✅ Stateless (each request does not depend on previous ones).
Example: REST API
(Fetching User Data)
🔹 This fetches user information from a public API.
import requests
url = "https://jsonplaceholder.typicode.com/users/1"
response = requests.get(url)
print(response.json()) # Display user data
Non-REST APIs:
When REST Isn't Enough
Some APIs don’t follow REST principles. Examples are:
- GraphQL APIs (Flexible queries instead of fixed endpoints).
- WebSockets (Live, two-way communication).
- RPC-based APIs (Remote Procedure Calls, like gRPC).
Example: GraphQL Query
🔹 This fetches country data using GraphQL.
import requests
url = "https://countries.trevorblades.com/"
query = {"query": "{ country(code: \"IT\") { name, capital } }"}
response = requests.post(url, json=query)
print(response.json())
This was crafted with
A Framework created by Hakim El Hattab and contributors
to make stunning HTML presentations
api-intro
By Andrea Gallegati
api-intro
- 95