What Is an API? Explained with a Trotro Fare Example

What an API is, in plain English: requests, responses, methods, status codes and JSON, with a tiny trotro fares API you build and call yourself.

AAyim Nelson27 Nov 20268 min read2

You climb into a trotro at Kasoa and ask the mate, "How much to Circle?" He says, "Fifteen cedis." You didn't need to see the station's fare list, know who owns the car or how the fares were decided. You asked a question in the way everyone understands, and you got an answer in the way everyone understands.

That, in a nutshell, is an API. In this post you'll see what an API really is, what a request and a response look like, and you'll build and call a tiny trotro fares API of your own, with real output at every step.

What an API is

API stands for Application Programming Interface. It's an agreed way for one program to ask another program for data, or to ask it to do something. The program asking doesn't need to know how the other one works inside. It only needs to know which questions it may ask and what the answers will look like.

A web API does this over the internet, using HTTP, the same language your browser uses to load pages. The difference is who reads the answer. A website sends back a page for a person to look at. A web API sends back data, usually in a format called JSON, for a program to read.

You use web APIs every day without seeing them. When a shopping app shows your mobile money balance, when a weather app shows today's forecast for Kumasi, or when a school portal app shows your results, the app is calling an API behind the scenes.

The trotro mate is an API

Here's how the trotro trip lines up with a web API:

At the stationIn a web API
You, the passengerThe client: an app, a website or a script
The mate you talk toThe API: the questions you're allowed to ask
The station's fare list and the driverThe server and its database, hidden from you
"How much to Circle?"The request
"Fifteen cedis."The response
"This car doesn't go to Tema."An error response

The mate is useful precisely because he hides the details. The station could change how fares are worked out tomorrow, and you'd still ask the same question. A good API works the same way: the server can change inside, and the apps that use it keep working.

What a request says

Every request to a web API has two main parts:

  • A URL, which names the thing you're asking about, such as /routes/1 for route number 1. Each URL an API answers is called an endpoint.
  • A method, which says what you want to do with it.

Most web APIs follow a style called REST: each thing has its own URL, and the method is the action. For a fares API that could look like this:

RequestMeans
GET /routesList all the routes
GET /routes/1Show route 1
POST /routesAdd a new route
PATCH /routes/1Change part of route 1, such as its fare
DELETE /routes/1Remove route 1

Requests can also carry headers (extra information, such as an API key) and a body (data you're sending, such as a new route). To read data, a plain GET is enough.

Build a tiny fares API

The best way to understand an API is to make one. This one uses FastAPI, a Python framework for building APIs. Save it as main.py:

from fastapi import FastAPI, HTTPException

app = FastAPI()

ROUTES = {
    1: {"origin": "Kasoa", "destination": "Circle", "fare": 15.0},
    2: {"origin": "Madina", "destination": "Circle", "fare": 10.5},
    3: {"origin": "Adum", "destination": "Kejetia", "fare": 6.0},
}


@app.get("/routes")
def list_routes():
    return [{"id": route_id, **route} for route_id, route in ROUTES.items()]


@app.get("/routes/{route_id}")
def get_route(route_id: int):
    if route_id not in ROUTES:
        raise HTTPException(status_code=404, detail="Route not found")
    return {"id": route_id, **ROUTES[route_id]}

Two endpoints: one lists every route, and one shows a single route by its number. The fares live in a dictionary here; a real API would keep them in a database. In a virtual environment, install FastAPI and start the server:

pip install "fastapi[standard]"
fastapi dev main.py

Your API is now running on your own computer at http://127.0.0.1:8000. Leave that terminal open.

Ask it a question

Open http://127.0.0.1:8000/routes/1 in your browser and you'll see the JSON. To see the whole response, use curl in a second terminal. The -i option shows the status and headers too. (On Windows, type curl.exe, because in Windows PowerShell plain curl means a different command.)

curl -i http://127.0.0.1:8000/routes/1

This is the real response:

HTTP/1.1 200 OK
date: Fri, 27 Nov 2026 03:52:21 GMT
server: uvicorn
content-length: 60
content-type: application/json

{"id":1,"origin":"Kasoa","destination":"Circle","fare":15.0}

Read it from the top:

  • The status line, 200 OK, says the request worked.
  • The headers describe the answer. content-type: application/json tells the client to read the body as JSON.
  • The body, after the blank line, is the data itself: the route from Kasoa to Circle costs GHS 15.

Try http://127.0.0.1:8000/routes too. You get a JSON list with all three routes. And open http://127.0.0.1:8000/docs: FastAPI has written interactive documentation for your API, where you can try each endpoint with a button.

When the answer is "no"

A good API also tells you clearly when something is wrong, with a status code. Ask for a route that doesn't exist:

curl -i http://127.0.0.1:8000/routes/9
HTTP/1.1 404 Not Found
date: Fri, 27 Nov 2026 03:52:21 GMT
server: uvicorn
content-length: 28
content-type: application/json

{"detail":"Route not found"}

That's the mate saying "this car doesn't go there". Now send something that isn't a number at all, like /routes/abc:

HTTP/1.1 422 Unprocessable Content
date: Fri, 27 Nov 2026 03:52:22 GMT
server: uvicorn
content-length: 153
content-type: application/json

{"detail":[{"type":"int_parsing","loc":["path","route_id"],"msg":"Input should be a valid integer, unable to parse string as an integer","input":"abc"}]}

You didn't write that check. Because the function says route_id: int, FastAPI refused the request before your code even ran, and explained which part was wrong. Status codes come in families, so you can understand any code by its first digit:

  • 2xx: it worked (200 OK, 201 Created).
  • 3xx: look somewhere else (a redirect).
  • 4xx: the client made a mistake (404 Not Found, 422 bad data, 401 not logged in).
  • 5xx: the server had a problem.

Call the API from Python

Apps don't use curl; they call APIs from code. Here's a small Python client, using only the standard library. Save it as fare.py and run it while the server is still running:

import json
from urllib.request import urlopen

with urlopen("http://127.0.0.1:8000/routes/1") as response:
    print("Status:", response.status)
    route = json.loads(response.read())

print(f"{route['origin']} to {route['destination']}: GHS {route['fare']:.2f}")
Status: 200
Kasoa to Circle: GHS 15.00

json.loads() turns the JSON text into a normal Python dictionary, so you can use the fare like any other value. A JavaScript app does exactly the same with fetch() and response.json(). Two programs, written in different languages, can talk because they agree on HTTP and JSON.

Keys and limits

Public APIs, such as payment, SMS or weather services, usually ask you to sign up for an API key: a long secret string you send with each request, often in a header, so the service knows who is calling. Treat it like a password: keep it out of your code and out of Git. Many APIs also have rate limits, a maximum number of requests per minute or day. Go over it and you'll get 429 Too Many Requests, so read the service's documentation before you build.

Learn APIs on JeliScript

You've seen the core idea: a client sends a request to a URL with a method, and the server answers with a status code and JSON. To go further:

  • How the Web Works: URLs, HTTP requests and responses, methods, status codes, JSON and calling APIs, in short lessons with Python practice.
  • Building APIs with FastAPI: grow this example into the full Trotro Fares API, with Pydantic validation, a database, logins with JWT, tests and deployment.
  • REST APIs with Django REST Framework: if you use Django, turn a blog into a JSON API with serializers, viewsets, tokens, permissions and pagination.

Comments

Log in to join the conversation.

Loading comments...