Building a REST API

A REST API is a web interface that lets programs talk to each other over HTTP using standard verbs — GET, POST, PUT, and DELETE — to read and change structured data instead of returning HTML pages.

Learn Building a REST API in our free Flask course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Flask course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll build a small JSON API in Flask, map HTTP methods to create-read-update-delete operations, and return proper status codes so any client can use your service.

REST (Representational State Transfer) is a set of conventions for building APIs around resources — things like users, articles, or tasks. Each resource has a URL, and you act on it using HTTP verbs .

These four verbs map cleanly onto the four basic data operations, often called CRUD : Create, Read, Update, Delete. A well-designed API is predictable because every client already knows what each verb means.

The jsonify() helper turns a Python dictionary or list into a proper JSON response with the correct Content-Type header. Pair it with a GET route to expose your data.

The API below lists all tasks at /tasks and a single task at /tasks/<id> , returning 404 when the id doesn't exist. Run it with flask run and visit the URLs to see the JSON.

jsonify() serializes lists and dictionaries automatically. You return it just like any other response — and you can attach a status code by returning a tuple.

To accept data from a client, read the request body with request.get_json() . Then return the right status code: 201 Created for a successful POST, 200 OK for an update, and 204 No Content for a delete.

The example below adds a task with POST, updates one with PUT, and removes one with DELETE — the full lifecycle of a resource.

Complete the API so it lists books with GET and adds one with POST returning 201. Replace each ___ .

You sent a verb the route doesn't accept. List every method a route should handle in its methods=[...] argument, e.g. methods=["GET", "POST"] .

The client didn't send a JSON body or the Content-Type header isn't application/json . Always guard with a check before using the data, and return 400 if it's missing.

Build a tiny notes API that supports reading all notes and deleting one by id.

Lesson 16 complete — you built a working REST API!

You can map HTTP verbs to CRUD operations, return JSON with jsonify() , read bodies with request.get_json() , and send correct status codes like 201 and 404.

🚀 Up next: JSON & Serialization — go deeper into turning Python objects into JSON safely and handling tricky data types.

Practice quiz

Which HTTP verb is used to remove a resource?

  • GET
  • POST
  • PUT
  • DELETE

Answer: DELETE. DELETE removes a resource, mapping to the D in CRUD.

Which verb reads a resource without changing it?

  • GET
  • POST
  • PUT
  • PATCH

Answer: GET. GET reads data and should not modify the resource.

Which verb creates a new resource?

  • GET
  • POST
  • DELETE
  • HEAD

Answer: POST. POST creates a new resource, mapping to the C in CRUD.

Which verb typically updates an existing resource?

  • GET
  • POST
  • PUT
  • OPTIONS

Answer: PUT. PUT (or PATCH) updates an existing resource.

What status code signals a resource was successfully created?

  • 200 OK
  • 404 Not Found
  • 500 Internal Error
  • 201 Created

Answer: 201 Created. 201 Created is returned after a successful POST that creates a resource.

Which helper turns a Python dict or list into a JSON response?

  • jsonify()
  • render_template()
  • str()
  • marshal()

Answer: jsonify(). jsonify() serializes data and sets the application/json Content-Type.

How do you read a JSON request body in a Flask view?

  • request.form
  • request.get_json()
  • request.args
  • request.body

Answer: request.get_json(). request.get_json() parses the JSON body into a Python object.

What status fits a successful DELETE that returns no body?

  • 201 Created
  • 400 Bad Request
  • 204 No Content
  • 302 Found

Answer: 204 No Content. 204 No Content signals success with an empty response body.

What status should you return when a requested resource id doesn't exist?

  • 200 OK
  • 201 Created
  • 500 Internal Error
  • 404 Not Found

Answer: 404 Not Found. 404 Not Found indicates the resource doesn't exist.

Why do REST APIs commonly return JSON instead of HTML?

  • It is compact and parseable by any language/client
  • It is required by Flask
  • It loads CSS automatically
  • It is faster than all other formats by law

Answer: It is compact and parseable by any language/client. JSON is compact and universally parseable, so any client can consume the endpoint.