What Is an API and How Does It Work?
An API explained with a restaurant analogy, then the real parts: endpoints, methods, JSON, status codes and API keys, with a small Python example.
Short answer: an API (Application Programming Interface) is a set of rules that lets one program ask another program for data or for an action, and get a structured answer back. Your program sends a request to an address (an endpoint), the other side's server does the work, and it sends back a response, usually in JSON, with a status code saying whether it worked.
The restaurant analogy
You sit at a table with a menu. The kitchen can cook many things, but you never walk in and cook yourself. You tell the waiter what you want, the waiter carries the order to the kitchen, and brings the dish back.
You are the client (your app or script).
The menu is the API documentation: what you may ask for.
The waiter is the API: it takes your request and returns the result.
The kitchen is the server and its database, hidden from you.
You do not need to know how the kitchen works. You only need to know how to order.
The real parts of an API call
Part | Meaning | Restaurant version |
|---|---|---|
Endpoint | The URL you call, such as | Which counter or menu section |
Method | What you want to do: GET (read), POST (create), PUT/PATCH (update), DELETE (remove) | “Show me”, “place an order”, “change it”, “cancel it” |
Request | Method + endpoint + optional headers and body | Your spoken order |
Response | The data and status code that come back | The dish, or “sorry, sold out” |
JSON
Most web APIs reply in JSON, a plain-text format of keys and values that every programming language can read: {"name": "Asha", "age": 25}.
Status codes
200: OK, it worked.
201: created something new.
400: your request was wrong.
401 / 403: not logged in, or not allowed.
404: that thing was not found.
429: too many requests, slow down.
500: the server had a problem.
API keys
Many APIs ask for an API key, a secret string that identifies you, like a membership card. The provider uses it to know who is calling and to apply limits. Keep it secret: never paste it into public code, and store it in an environment variable instead.
A tiny Python example
GitHub has a public API that needs no key for simple reads. This script asks for information about the public account octocat:
import requests
response = requests.get("https://api.github.com/users/octocat", timeout=10)
print(response.status_code) # 200 means success
if response.ok:
data = response.json() # JSON turned into a Python dict
print(data["login"], data["public_repos"])
else:
print("Request failed:", response.status_code)
Install the library first with pip install requests. Change the username to one that does not exist and you will see a 404 instead. Public APIs have rate limits, so do not call them in a tight loop. The chapters Calling APIs from Python with requests and HTTP methods, status codes, headers and JSON go deeper.
AI model APIs are APIs too
When an app uses an AI model, it usually sends a request containing your prompt to the model provider's API and receives the generated text as a JSON response. That request normally needs an API key. The app you chat with is just a friendly layer on top. See AI Model vs AI API vs AI Application for how the three fit together.
Common mistakes
Hard-coding the API key in code that you push to a public repository.
Ignoring the status code and assuming every response contains good data.
Skipping the documentation, which lists endpoints, required parameters and limits.
Forgetting a timeout, so your program hangs if the server is slow.
Key Takeaway
An API is the waiter between programs: you send a request to an endpoint with a method, and get back a response with JSON data and a status code. Learn to read the documentation, check status codes and protect your API keys, and you can connect to almost any service, including AI models.
Ready to start? Learn to call APIs from Python, read documentation and build your own small API. Begin with the free course Git, APIs and Backend for AI: Beginner.
Comments
Sign in to join the discussion.