What is an API?
You used fetch() to get data from a URL someone else set up. But what is actually sitting at that URL? A server — one that waits for requests and sends back data instead of HTML pages. That server is called an API.
API stands for Application Programming Interface. The name sounds formal. The idea is simple: a URL you visit that gives back data.
When you fetched from https://jsonplaceholder.typicode.com/users, you did not get an HTML page — you got a list of users in JSON. That is an API.
The request and the response
Every API works the same way:
- Request — you ask for something by visiting a URL
- Response — the API sends something back, usually JSON
Think of it like a restaurant. You order from the menu (the request). The waiter brings your food (the response). You never went into the kitchen yourself — you just asked, and something came back.
You have already been doing this
Look at any Flask route you have written:
@app.route('/patients')
def get_patients():
return "hello"
That is already a URL that responds to requests. To make it a proper API, it just needs to return JSON instead of plain text. That is all the difference is.
What JSON looks like
You have seen JSON in the browser. It looks like Python dictionaries and lists:
[
{ "name": "Alice", "age": 24 },
{ "name": "Bob", "age": 31 }
]
The outer [] means it is a list. Each {} is one item. Keys are strings in double quotes. Values can be strings, numbers, other lists, or more objects.
When fetch() receives this, .json() converts it into JavaScript objects you can loop through and read.
Challenge
Open https://jsonplaceholder.typicode.com/posts/1 in your browser. What fields does that single post have? Now open https://jsonplaceholder.typicode.com/posts — how is the response different from a single post?
← JavaScript 102 Exercises Next: Building a JSON API with Flask →