What Is a REST API? A Beginner’s Guide With Real Examples

What Is a REST API? A Beginner's Guide With Real Examples

Almost every app you use talks to a server through an API. When a weather app shows tomorrow’s forecast, a shopping app loads your cart, or a website shows your profile picture, a request has gone to an API and a response has come back. The most common style of web API is called REST.

This guide explains REST APIs from the ground up, with real request and response examples. No prior backend experience is needed.

What is an API?

An API (Application Programming Interface) is a set of rules that lets one program ask another program for data or ask it to do something. A useful comparison is a restaurant: you (the app) do not walk into the kitchen (the database). You give your order to a waiter (the API), who takes it to the kitchen and brings back your food (the response). The menu is the API documentation: it tells you what you are allowed to ask for and how.

What makes an API “REST”?

REST (Representational State Transfer) is a set of design principles for web APIs, described by Roy Fielding in 2000. A REST API typically:

  • Organises data as resources such as users, products or orders, each with its own URL.
  • Uses standard HTTP methods (GET, POST, PUT, PATCH, DELETE) to say what to do with a resource.
  • Is stateless: each request carries everything the server needs, such as an API key, so the server does not have to remember earlier requests.
  • Returns representations of resources, today almost always in JSON.

Anatomy of a request

Every REST request has a few parts:

Part Example What it does
Method GET The action to perform
Base URL https://api.example.com/v1 Where the API lives
Endpoint /users/7 Which resource you want
Query parameters ?page=2&per_page=10 Filters, sorting and pagination
Headers X-API-Key: abc123 Extra information such as authentication and content type
Body {"name": "Asha"} Data you send, for POST, PUT and PATCH

The main HTTP methods

Method Purpose Example
GET Read data GET /products lists products
POST Create something new POST /orders places an order
PUT Replace a resource completely PUT /users/7 replaces user 7
PATCH Update part of a resource PATCH /users/7 changes only the email
DELETE Remove a resource DELETE /todos/3 deletes task 3

The pattern is predictable: the URL names the thing, and the method names the action. Once you know an API’s resources, you can often guess its endpoints.

Anatomy of a response

A response has a status code, headers and usually a body. A successful request for a user might return:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 7,
  "name": "Asha Verma",
  "email": "asha.verma@example.com",
  "city": "Pune"
}

Status codes you will see most

Code Meaning What to do
200 OK Request succeeded Use the data
201 Created A new resource was created Often returns the new item
204 No Content Success, nothing to return Common after DELETE
400 Bad Request Your request was malformed Check the body and parameters
401 Unauthorized Missing or invalid credentials Check your API key or token
403 Forbidden You are known but not allowed Check permissions
404 Not Found The resource does not exist Check the URL and ID
429 Too Many Requests Rate limit reached Wait and retry later
500 Internal Server Error Something broke on the server Retry; report if it continues

A simple rule: codes in the 200s mean success, 400s mean the problem is in your request, and 500s mean the problem is on the server.

Authentication: proving who you are

Most APIs need to know who is calling. Common methods:

  • API keys: a secret string sent in a header such as X-API-Key. Simple and common for public data APIs.
  • Bearer tokens: sent as Authorization: Bearer <token>, often issued after logging in.
  • OAuth 2.0: lets a user grant an app limited access to their account on another service without sharing their password.

Never put a secret API key in public front-end code or a public code repository. Anyone who sees it can use it.

Make your first request

You can try a REST API without writing an app. The freetoolza Fake API & Mock Data Generator gives you a free key and realistic sample data.

In the browser console (JavaScript)

const res = await fetch(
  "https://freetoolza.com/wp-json/freetoolza/v1/users?level=simple&per_page=3",
  { headers: { "X-API-Key": "YOUR_API_KEY" } }
);
console.log(res.status);          // 200
console.log(await res.json());    // array of 3 users

From the command line (curl)

curl -H "X-API-Key: YOUR_API_KEY" \
  "https://freetoolza.com/wp-json/freetoolza/v1/products/1?level=medium"

If the response is hard to read, paste it into the JSON Formatter. If a query value contains spaces or symbols, encode it first with the URL Encoder.

Pagination, filtering and rate limits

Real APIs rarely return everything at once. Look for:

  • Pagination parameters such as page and per_page, or a next link in the response.
  • Filters such as ?status=active or ?city=Pune.
  • Rate limit headers such as X-RateLimit-Remaining. When it reaches zero you will get a 429 until the limit resets.

REST vs other API styles

REST is the most common style but not the only one. GraphQL lets the client ask for exactly the fields it needs in a single request, which helps complex apps avoid many round trips. gRPC uses a compact binary format and is popular for fast communication between internal services. SOAP is an older XML-based standard still found in enterprise and banking systems. For public web and mobile APIs, REST with JSON remains the usual starting point.

Frequently asked questions

Is REST the same as HTTP?
No. HTTP is the protocol that carries requests and responses. REST is a way of designing an API on top of HTTP.

Do REST APIs have to use JSON?
No, but JSON is by far the most common. Some APIs also offer XML; see our JSON vs XML guide for the differences.

How do I test an API without a backend?
Use a mock API. Our guide to prototyping app screens with a fake API walks through it step by step.