REST APIs explained with simple examples and real-world use cases

Documentation Made Easy3 min read#api #rest #python #flask #developer-docs

Infographic: REST APIs in four verbs, GET, POST, PUT and DELETE, using an online bookstore as the example

Streaming a film, checking your bank balance, booking a cab: behind each of these, REST APIs are moving data between applications. This guide explains the basics with examples you can try.

What is a REST API?

A REST API (Representational State Transfer Application Programming Interface) lets software applications talk to each other using standard HTTP requests. It's lightweight and scalable, which is why it replaced most older, heavier integration methods.

Think of an API as a waiter in a restaurant. You place an order, the waiter takes it to the kitchen and comes back with your dish. An API takes your request to a server and comes back with the data, or confirms that the action happened.

What makes REST different

REST APIs are stateless: every request carries everything the server needs to process it. They also use a small set of standard HTTP methods:

Method Purpose Bookstore example
GET Read data List available books
POST Create data Add a new book
PUT Update data Change a book's price
DELETE Remove data Take an old listing down

Because every REST API uses the same verbs, a developer who has used one can quickly understand another.

Real-world use cases

Social media. Platforms such as X (formerly Twitter) offer APIs so other apps can read posts, publish updates or analyse trends. Reading a user's recent posts looks like this:

GET https://api.x.com/2/users/{user_id}/tweets

The response is JSON: the posts plus details such as like and repost counts. Check the X API documentation for current access tiers.

Healthcare. Electronic health record systems use APIs to share data securely. A patient-portal call might be:

GET /appointments/12345

The response lists the patient's upcoming appointments, so the portal, the clinic and reminder systems all show the same information.

Build a small REST API with Python and Flask

Flask is a lightweight Python web framework. Here is a minimal API for a library.

1. Install Flask:

pip install flask

2. Create app.py:

from flask import Flask, jsonify, request

app = Flask(__name__)

books = [
    {"id": 1, "title": "1984", "author": "George Orwell"},
    {"id": 2, "title": "To Kill a Mockingbird", "author": "Harper Lee"},
]

@app.route("/books", methods=["GET"])
def get_books():
    return jsonify(books)

@app.route("/books", methods=["POST"])
def add_book():
    new_book = request.get_json()
    books.append(new_book)
    return jsonify({"message": "Book added"}), 201

@app.route("/books/<int:book_id>", methods=["PUT"])
def update_book(book_id):
    updated = request.get_json()
    for book in books:
        if book["id"] == book_id:
            book.update(updated)
            return jsonify({"message": "Book updated"})
    return jsonify({"message": "Book not found"}), 404

@app.route("/books/<int:book_id>", methods=["DELETE"])
def delete_book(book_id):
    global books
    books = [b for b in books if b["id"] != book_id]
    return jsonify({"message": "Book deleted"})

if __name__ == "__main__":
    app.run(debug=True)

3. Run it:

python app.py

4. Try it with cURL or Postman:

curl http://127.0.0.1:5000/books
curl -X POST http://127.0.0.1:5000/books \
  -H "Content-Type: application/json" \
  -d '{"id": 3, "title": "Dune", "author": "Frank Herbert"}'

Best practices for a REST API people enjoy using

  • Document it. Clear docs decide whether developers adopt your API. Tools like Swagger / OpenAPI and Postman generate interactive documentation.
  • Authenticate. Use API keys or OAuth 2.0 so only the right clients get in.
  • Rate-limit. Cap requests per client and time window to protect performance.
  • Return useful errors. A clear message and the right status code (400, 404, 429) save developers hours.
  • Version it. Use paths like /v1/books and /v2/books so you can improve the API without breaking existing users.

Where REST APIs are heading

REST still powers most integrations: storefronts talking to payment gateways, ride-hailing apps talking to maps and payments, analytics tools talking to everything. Learning the basics is useful for developers, product managers and founders alike.

More from the blog: JavaScript asynchronous programming, which is how a web page calls an API without freezing. Want to see API documentation I've written? Browse my projects and samples.

Found this useful? Share it, or tell me what to write next.