Architecture

What Is an API? A Practical Explanation With Real Requests

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

An API, or Application Programming Interface, is an agreed way for one piece of software to ask another for data or actions. When a mobile app shows your order history, it does not read the company's database directly; it sends a request to an API, and the API decides what to return. When a website takes a card payment, it calls the payment provider's API. APIs are the contracts that let programs written by different people, in different languages, on different machines, work together without knowing each other's internals.

The restaurant analogy, briefly

You do not walk into a restaurant kitchen to cook. You read the menu, tell the waiter what you want, and receive a dish. The menu is the API documentation, the waiter is the API, and the kitchen is the system behind it. The kitchen can change its equipment, as long as the menu and the dishes stay the same. That separation is the main value of an API: callers depend on the contract, not the implementation.

Kinds of APIs

The word covers more than web services:

  • Library APIs: functions a programming library exposes, such as Python's open().
  • Operating system APIs: system calls like read() and write() on Linux, or the Win32 API on Windows.
  • Web APIs: services reached over the network, usually HTTP. This is what most people mean today, and the focus of this article.

Anatomy of an HTTP API request

A web API request has a few parts:

  • Method: what you want to do.
  • URL: which resource, for example https://api.example.com/v1/orders/42.
  • Headers: metadata such as authentication and content type.
  • Body: data you send, usually JSON, for methods that create or change things.

You can make one directly from a terminal with curl:

curl -s https://api.example.com/v1/orders/42 \
  -H "Authorization: Bearer $API_TOKEN" \
  -H "Accept: application/json"

And create a resource:

curl -s -X POST https://api.example.com/v1/orders \
  -H "Authorization: Bearer $API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"product_id": 7, "quantity": 2}'

A typical JSON response:

{
  "id": 43,
  "product_id": 7,
  "quantity": 2,
  "status": "pending"
}

HTTP methods

MethodPurposeSafe to repeat?
GETRead a resourceYes, no changes
POSTCreate a resource or trigger an actionNo, may create duplicates
PUTReplace a resource completelyYes, idempotent
PATCHChange part of a resourceDepends on the API
DELETERemove a resourceYes, idempotent

Status codes worth knowing

  • 200 OK, 201 Created, 204 No Content: success.
  • 400 Bad Request: the input is invalid.
  • 401 Unauthorized: missing or invalid credentials.
  • 403 Forbidden: authenticated, but not allowed.
  • 404 Not Found: no such resource.
  • 409 Conflict and 422 Unprocessable Content: the request conflicts with current state or fails validation.
  • 429 Too Many Requests: you hit a rate limit; check the Retry-After header.
  • 500, 502, 503, 504: server-side problems, often worth retrying with backoff.

API styles

REST models everything as resources with URLs and uses HTTP methods and status codes as intended. It is the most common style for public APIs.

GraphQL exposes a single endpoint where the client describes exactly which fields it wants, which avoids over-fetching on complex screens but makes caching and rate limiting more involved.

gRPC uses Protocol Buffers over HTTP/2 for compact, strongly typed calls, popular between internal services.

Webhooks reverse the direction: the provider calls your URL when an event happens, such as a completed payment.

WebSockets keep a connection open for real-time, two-way messages, used for chat and live dashboards.

Authentication and security

Most APIs require you to prove who you are:

  • API keys: a secret string sent in a header. Simple, suitable for server-to-server calls.
  • Bearer tokens and OAuth 2.0: short-lived tokens, often obtained on behalf of a user, with scopes that limit what they can do.
  • Signed requests and HMAC: common for webhooks, so you can verify that a call really came from the provider.

Practical rules:

  • Keep keys in environment variables or a secrets file outside your Git repository, never in front-end code.
  • Always use HTTPS.
  • Give each key the minimum permissions it needs and rotate it if it may have leaked.
  • Validate every input on the server, even from your own app.
  • Apply rate limits so one client cannot exhaust your resources.

Building your own API

Any web framework can serve an API: Laravel, Express, FastAPI, Django REST framework, ASP.NET Core, Go's net/http. A minimal FastAPI example:

from fastapi import FastAPI

app = FastAPI()

@app.get("/v1/health")
def health():
    return {"status": "ok"}

Run it with uvicorn main:app --host 127.0.0.1 --port 8000 and put nginx in front for TLS. When the number of services grows, an API gateway can centralize authentication, routing and rate limits.

Design checklist

  • Version the API (/v1/) so you can change it without breaking clients.
  • Use consistent naming, plural nouns and JSON field styles.
  • Return clear error bodies with a machine-readable code and a human message.
  • Paginate list endpoints.
  • Document it, for example with an OpenAPI specification.
  • Accept an idempotency key on POST requests that create payments or orders.

A small VPS is enough to host an API and its database while you build; you can deploy a server in a few minutes and scale later.

FAQ

Is an API the same as a website?

No. A website returns pages designed for people, usually HTML. An API returns structured data, usually JSON, designed for programs. Many products have both, and the website itself often calls the API behind the scenes.

What is an API endpoint?

An endpoint is a specific URL plus method that performs one operation, such as GET /v1/orders/42 to read an order. An API is the full set of endpoints and the rules for using them.

Are APIs free to use?

It depends on the provider. Some APIs are free with rate limits, some charge per request or per plan, and internal APIs cost only the servers they run on. Always read the provider's terms and limits before building on them.

Related guides