An API is a menu
API stands for Application Programming Interface. Think of a restaurant menu: it lists what you can order and how to ask for it, without showing you the kitchen. A web API lists the requests a program may send to a service and what it will get back.
Most web APIs work over HTTP, the same system browsers use to load pages (see how the web works). The difference is that an API usually answers with data, often in JSON, instead of a page for people to read.
Endpoints and methods
Each thing you can ask for has its own address, called an endpoint. The HTTP method says what you want to do with it:
GET /lessons: list lessons.GET /lessons/12: get lesson 12.POST /lessons: create a new lesson (the details go in the request body).PATCH /lessons/12orPUT /lessons/12: change lesson 12.DELETE /lessons/12: delete lesson 12.
Extra options often go in the query string, for example GET /lessons?subject=maths&limit=10.
Reading a JSON response
JSON (JavaScript Object Notation) is text that describes objects, lists, strings, numbers, true/false and null. Here is a response an API might send, and how a program reads it:
const responseText = `{
"lesson": { "id": 12, "title": "Fractions", "subject": "maths" },
"questions": [
{ "text": "1/2 + 1/4 = ?", "answer": "3/4" },
{ "text": "2/3 of 9 = ?", "answer": "6" }
],
"published": true
}`;
const data = JSON.parse(responseText);
console.log(data.lesson.title);
console.log(data.questions.length, "questions");
for (const q of data.questions) {
console.log("-", q.text);
}
console.log(JSON.stringify({ answer: "3/4", correct: true }));
JSON.parse turns JSON text into JavaScript objects, and JSON.stringify turns objects back into text to send. In JSON, keys and strings must use double quotes.
Status codes tell you what happened
- 200 OK / 201 Created: it worked.
- 400 Bad Request: something in your request is wrong; the body usually says what.
- 401 Unauthorized / 403 Forbidden: you are not signed in, or not allowed.
- 404 Not Found: that endpoint or item does not exist.
- 429 Too Many Requests: slow down; you hit a rate limit.
- 500 and above: a problem on the server, not in your code.
Always check the status before using the data. With fetch that means checking response.ok, as shown in loading data with fetch.
API keys and keeping them secret
Many APIs need an API key: a long secret string that identifies your app, sent in a header such as Authorization: Bearer .... Treat it like a password:
- Never put a secret key in browser JavaScript or a public code repository: anyone can read it there.
- Call key-protected APIs from your own server, and keep the key in a server setting or environment variable.
- If a key leaks, revoke it in the provider's dashboard and create a new one.
Unays offers free developer tools and an API with documentation at Unays Developers.
Try it yourself
In Unays Code, change the JSON example: add a third question, count how many answers are numbers, and build a new object with your score to JSON.stringify. Then read a public API's documentation and write down one endpoint, its method and what it returns.
This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].