Edexcel International Advanced Level Computer Science, point 6.3.4
Definition
An application programming interface (API) is a defined set of rules, functions and data formats that lets one piece of software communicate with and use the services of another, without needing to know how it works inside.
It is an interface (an abstraction, 10.1.1): it says what you can ask for and what you will get back, and hides the details.
Analogy: a restaurant menu and waiter. You choose from the menu (the API) and the waiter takes your order to the kitchen. You don't need to know how the food is cooked.
Function
- Lets programs share data and features safely and consistently.
- Lets developers reuse existing services instead of writing everything.
- Separates the user of a service from the way it is built, so each can change independently.
Types of API
| Type | Description | Example |
|---|---|---|
| Library / language API | Functions provided by a library or the language | Python's math.sqrt(), random.randint() |
| Operating system API | Services from the OS for programs | Opening a file, creating a window |
| Web API | Services over the Internet, using HTTP requests and data formats such as JSON | A weather, maps, payments or social media service |
Operation: a web API
- The program sends a request to a URL (endpoint), often with parameters and perhaps a key to identify the user.
- The server processes the request.
- It returns a response, usually JSON (or XML), with a status code (for example 200 = OK, 404 = not found).
- The program reads the data and uses it.
Example response (JSON):
{"from": "GBP", "to": "EUR", "rate": 1.17}
Using the response
The program turns the JSON text into a dictionary (4.2.1) and reads the values it needs, such as the rate.
Characteristics of an API
| Characteristic | What it means | Why it is needed |
|---|---|---|
| Authentication | Each app sends an API key (or token) that identifies it | The provider knows who is making each request, and can control what each app may do or block misuse |
| Rate limiting | A maximum number of requests per period for each key (for example 100 an hour) | Protects the service from overload, shares it fairly between users, and limits abuse |
| Error handling | The response includes a status code saying whether the request worked: 200 OK, 401 not authorised, 404 not found, 429 too many requests | The app can check the code and react sensibly (retry later, show a message) instead of crashing |
Benefits
- Saves development time: reuse well-tested features (payments, maps, speech recognition).
- Abstraction: you don't need to know how it's built.
- Access to data and services you couldn't create yourself.
- Standard formats let different languages and systems work together.
- Easy to update: the provider can improve the inside without breaking the programs that use it, as long as the interface stays the same.
- Security control: the provider decides what is allowed (keys, permissions).
Drawbacks
- Dependency: if the provider changes or removes the API, or goes offline, your program breaks.
- Cost: some APIs charge per request or have usage limits.
- Needs a connection (for web APIs), and adds delay (latency, 3.1.2).
- Security and privacy: sending data to a third party, and API keys must be kept secret.
- Learning: you must read the documentation and handle errors (failed requests, missing data).
- Less control over how the service works, and limited to what it exposes.
APIs and data science
APIs are a common way to collect data (6.1.2): a program can request data (weather, transport, social media) in a structured form, which is cleaner and more reliable than web scraping, and the provider permits it.
Worked example
Task: an online shop wants to take card payments and fill in customers' addresses from their postcode. Explain why it would use APIs, and one risk.
- Why: building a secure card-payment system or a complete address database from scratch would be impractical and risky. A payments API takes the card details and returns whether the payment succeeded, and a postcode API returns the list of addresses for a postcode as JSON. The shop sends requests and uses the responses.
- Abstraction: the shop doesn't need to know how the bank checks the card or how the address data is stored.
- Risk: if the payment provider goes offline or changes its API, the shop can't take orders unless it handles the error (for example with a clear message and a retry). The API keys must be kept secret, and customers' data is sent to a third party, so the shop must use a trusted provider.
Exam tip: define an API as an interface that lets one program use the services of another without knowing how it works, and give an example (a web API returning JSON). Link the benefit to abstraction and reuse, and the drawback to dependency on the provider.
Check yourself
What is an API?
A defined set of rules and functions that lets one piece of software use the services of another without knowing how it works inside.
What data format do many web APIs return?
JSON (or sometimes XML).
Give two benefits of using an API
Any two of: saves development time; reuses tested services; hides the complexity (abstraction); gives access to data or features you couldn't build; the provider can improve the inside without breaking users.
What is an API endpoint?
The URL that a program sends its requests to, for a particular service of the API.
How does an API relate to abstraction?
It exposes what can be requested and returned, and hides how the service is implemented.