What is an API and How Does it Work?
Short answer: an API (Application Programming Interface) is a set of rules that lets one piece of software ask another for data or actions. A client sends a request, the API's server processes it and sends back a response. You use APIs every day: a weather app asking a weather service for the forecast, or a shop checking a payment provider.
A restaurant analogy
You (the client) do not walk into the kitchen. You tell the waiter (the API) what you want, the kitchen (the server) prepares it, and the waiter brings it back. The menu is the API's documentation: it lists what you may ask for and how.
How an API call works
- The client sends an HTTP request to a URL (the endpoint).
- The server checks the request, runs its logic and may read a database.
- The server sends back an HTTP response with a status code and usually data in JSON.
GET https://api.example.com/users/42
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 42,
"name": "Asha",
"email": "asha@example.com"
}
Paste a response like this into the JSON formatter to read it easily.
The parts of a request
- Method: what to do (GET, POST, PUT, PATCH, DELETE).
- Endpoint (URL): which resource, for example
/users/42. - Headers: extra information such as authentication and content type.
- Query parameters: options in the URL, like
?limit=10. - Body: data sent with POST, PUT or PATCH.
HTTP methods
| Method | Meaning | Example |
|---|---|---|
| GET | Read data | Get a list of products |
| POST | Create data | Register a new user |
| PUT / PATCH | Replace / partially update | Change a profile |
| DELETE | Remove data | Delete a comment |
Common status codes
| Code | Meaning |
|---|---|
| 200 OK | Success |
| 201 Created | A new resource was created |
| 400 Bad Request | The request was invalid |
| 401 Unauthorized | Authentication is missing or invalid |
| 403 Forbidden | Not allowed to do this |
| 404 Not Found | The resource does not exist |
| 429 Too Many Requests | You hit a rate limit |
| 500 Server Error | Something failed on the server |
Try it in JavaScript
const res = await fetch("https://jsonplaceholder.typicode.com/posts/1");
if (!res.ok) throw new Error("HTTP " + res.status);
const post = await res.json();
console.log(post.title);
You can also call the same URL from the command line with curl https://jsonplaceholder.typicode.com/posts/1.
Types of APIs
- REST: the most common web style, built around resources and HTTP methods.
- GraphQL: clients choose exactly which fields to fetch. Compare them in REST vs GraphQL.
- SOAP: an older XML-based style still used in some enterprises.
- WebSocket APIs: keep a connection open for live updates such as chat.
APIs can also be public (open to everyone), partner (shared with specific companies) or private (internal only).
Authentication and keys
Most APIs identify callers with an API key, a bearer token or OAuth. Never put secret keys in front-end code or public repositories. Read API authentication methods for details.
Good API design in short
- Use clear, consistent resource names and the right HTTP methods.
- Return meaningful status codes and error messages.
- Version your API so changes do not break clients; see API versioning best practices.
- Document it with examples. See how to design a RESTful API.
FAQs
What is the difference between an API and a web service?
A web service is an API that communicates over a network. All web services are APIs, but not every API runs over a network.
Is JSON the only format?
No, but it is the most common. XML, form data and binary formats are also used.
What is an endpoint?
A specific URL where an API receives requests, such as /users.
Do I need to code to use an API?
Not always. Tools like curl or API clients let you send requests without writing a program.
