What is an API?
In short
An API (application programming interface) is a set of rules for how one program can ask another for data or to do something.
When a weather app shows tomorrow's forecast, it didn't measure the weather itself. It asked a weather service's API and got the answer back as data. APIs let programs use each other's work without knowing how it's done inside.
Most web APIs send their answers as JSON, a text format that looks like JavaScript objects. A big part of using an API is reading that JSON and picking out the bits you need, like in the example below.
Python
Read a JSON answer like an API would send it
import json
reply = '{"city": "London", "temp": 14, "rain": true}'
weather = json.loads(reply)
print(weather["city"], weather["temp"])
print(weather["rain"])Output
London 14
TrueJavaScript
The same idea in JavaScript
const reply = '{"city": "London", "temp": 14, "rain": true}';
const weather = JSON.parse(reply);
console.log(weather.city, weather.temp);
console.log(weather.rain);Output
London 14
trueLearn it properly
These free lessons teach it step by step, with questions and a program you write yourself.
- fetch() and JSON APIsLearn JavaScript →
- Saving Data with jsonLearn Python →
- Destructuring, Spread and JSONLearn JavaScript →
Try your first lesson free
Codiqo teaches coding in five-minute lessons with real code. Free on the web, iPhone and Android.