Practical software engineering foundations · Lesson 56 of 60 · Concept lesson · Optional
API fundamentals
An API lets one program use another through agreed requests and responses.
A web API has endpoints (URLs), uses methods such as GET to read and POST to create, and answers with a status code such as 200 for success or 404 for not found.
The API's contract says what to send and what comes back, so both sides can change independently.
Picture it
Code example
const users = { 7: "Ada" };
function getUser(id) {
if (!(id in users)) return { status: 404 };
return { status: 200, name: users[id] };
}
console.log(getUser(7).status, getUser(999).status);Output: 200 404
A tiny pretend endpoint that answers 200 when the user exists and 404 when it does not.
users = {7: "Ada"}
def get_user(user_id):
if user_id not in users:
return {"status": 404}
return {"status": 200, "name": users[user_id]}
print(get_user(7)["status"], get_user(999)["status"])Output: 200 404
A tiny pretend endpoint that answers 200 when the user exists and 404 when it does not.
Check yourself
Pick an answer for each question. You can retry as many times as you like.
Loading the questions…