Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Tools
Back to What Is
What is an API and How Does it Work?

What is an API and How Does it Work?

What IsBy Muhammad Fareed
api-development

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

  1. The client sends an HTTP request to a URL (the endpoint).
  2. The server checks the request, runs its logic and may read a database.
  3. 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

MethodMeaningExample
GETRead dataGet a list of products
POSTCreate dataRegister a new user
PUT / PATCHReplace / partially updateChange a profile
DELETERemove dataDelete a comment

Common status codes

CodeMeaning
200 OKSuccess
201 CreatedA new resource was created
400 Bad RequestThe request was invalid
401 UnauthorizedAuthentication is missing or invalid
403 ForbiddenNot allowed to do this
404 Not FoundThe resource does not exist
429 Too Many RequestsYou hit a rate limit
500 Server ErrorSomething 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

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.