If you are learning Python and keep seeing the term API, the short version is this: an API is a defined interface that lets one piece of software use another piece of software without needing to know its internal implementation. A Python library exposes functions and classes as an API; a web service exposes URLs, methods and data formats as an API.
This distinction answers the practical questions beginners usually have: when should you use an API, what does calling one look like, and how can you create a small API of your own?
What an API means in Python
Think of an API as a contract. It tells you the names of the operations available, the inputs they accept, the result they return and the errors you should handle. The implementation can change behind that contract while your program continues to work.
A library API
When you import a Python module, you use its library API. For example, datetime.now() is an API operation provided by the datetime module. You do not need to implement the clock or understand every line of the module; you call the documented function and use its result.
from datetime import datetime
now = datetime.now()
print(now.isoformat())Code language: Python (python)
A web API
A web API lets a program communicate with a service over a network. Your program sends an HTTP request to an endpoint and receives a response, often as JSON. The API documentation defines the URL, method, parameters, authentication and response shape.
import requests
response = requests.get("https://api.example.com/temperature", timeout=10)
response.raise_for_status()
reading = response.json()
print(reading["celsius"])Code language: Python (python)
When should you use an API?
- Use a library API when a maintained package already provides the capability you need, such as GPIO access, HTTP requests or JSON parsing.
- Use a web API when your program needs data or actions owned by another service, such as weather data, a database or a connected-project backend.
- Create an API when you want several programs, devices or dashboards to use the same data and operations through a stable contract.
An API is useful when it gives you a clear boundary. Your Raspberry Pi sensor reader can collect data, your API can validate and store it, and a dashboard can display it without all three components sharing the same code.
Create a small API with Flask
Here is a minimal read-only API. It exposes one endpoint that returns a JSON object. In a real project you would add validation, authentication, logging and persistent storage, but this example shows the contract clearly.
from flask import Flask, jsonify
app = Flask(__name__)
@app.get("/api/temperature")
def temperature():
return jsonify({"celsius": 21.4, "source": "demo"})
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5000, debug=True)Code language: Python (python)
Run the file, then request http://127.0.0.1:5000/api/temperature. The response is JSON because the endpoint’s contract says it returns a temperature object. A client does not need to know how the number was produced.
How to design a useful API contract
- Name the resource: use a clear endpoint such as
/api/temperatureor/api/readings. - Choose the HTTP method: GET reads, POST creates, PATCH changes part of a resource and DELETE removes it.
- Define the JSON shape: document field names, types, units and whether fields are required.
- Return useful status codes: make success, validation errors, authentication failures and server errors distinguishable.
- Document failure paths: a caller needs to know what to retry, what to fix and what not to repeat.
A simple learning path
- Call a library function and inspect the value it returns.
- Call a public HTTP endpoint with
curlor Python’srequestslibrary. - Build one Flask endpoint that returns a documented JSON response.
- Connect a real source, such as a Raspberry Pi sensor, and validate its input.
- Add authentication, persistence, tests and monitoring before exposing the API publicly.
The key idea
An API is not limited to the web and it is not a synonym for a Python keyword. It is the boundary and contract that lets one program use another program’s capability. Once you can identify the inputs, operation, output and failure cases, you can use an API confidently—and design one that other programs can use.
For the next step, compare the concepts in this guide with our API overview, then try the first curl request before building your own endpoint.