What Is a REST API?
Exit

What Is a REST API?

Understand the request/response cycle and HTTP methods

How a REST API works

A REST API lets two programs exchange data over HTTP. One program sends a request. The other sends back a response.

Every request has three parts:

  • Method: the action to perform (GET, POST, PUT, DELETE)
  • URL: the resource to act on (/expenses, /expenses/3)
  • Body (optional): data to send, formatted as JSON

Every response has two parts:

  • Status code: a number that indicates success or failure (200 OK, 404 Not Found)
  • Body: the data returned, also formatted as JSON

Client                          Server
  |                               |
  |  POST /expenses  {JSON body}  |
  | ----------------------------> |
  |                               |
  |  201 Created  {new expense}   |
  | <---------------------------- |
  |                               |

The client sends a request. The server processes it and returns a response. Every API interaction follows this pattern.

HTTP methods

REST APIs use HTTP methods to signal what action to perform. Each method maps to a standard operation.

MethodActionExpense example
GETRead dataList all expenses
POSTCreate dataAdd a new expense
PUTReplace dataReplace an entire expense record
PATCHPartial updateChange only the category of an expense
DELETERemove dataDelete an expense by its unique identifier

You will use GET, POST, PATCH, and DELETE in this course. PUT replaces an entire record, which is less common for the use cases you will build.

What you will build

By the end of this course, your expense tracker API will:

  • Accept a POST request to add an expense
  • Return all expenses with GET, filtered by category
  • Delete an expense by its unique identifier
  • Update individual fields on an expense
  • Return a spending summary grouped by category
  • Save all data to a file so it survives a server restart

You will build each of these features one at a time, starting with the data model later in this lesson.

Next Chapter →