Module 1 · FundamentalsLesson 4 of 887 minBeginner

Designing REST APIs

A REST API treats your data as things with addresses, and uses HTTP methods to act on them.

Try it

Try each method on the users API and read the status codes. Then switch to "Version 2" to see an old and a new app using the API at the same time.

An API (application programming interface) is how one program asks another for something. A REST API is a popular style built on plain HTTP.

Resources and addresses

In REST everything is a resource with a URL:

  • /users: the collection of users
  • /users/42: one user
  • /users/42/orders: that user's orders

Then you use HTTP methods on those addresses:

  • GET /users/42: read user 42
  • POST /users: create a new user
  • PATCH /users/42: change part of user 42
  • DELETE /users/42: delete user 42

Good habits

  • Use nouns, not verbs in paths. /users/42, not /getUser.
  • Return the right status code: 201 when created, 404 when missing, 400 when the input is bad.
  • Page through big lists: GET /orders?limit=50&cursor=abc, so one request never returns a million rows.
  • Be consistent: same naming, same error shape, same date format everywhere.

Versioning

Once apps are using your API, changing it can break them. A phone app from last year might still be in someone's pocket.

So you version it. Put the version in the path (/v1/users) or a header. When you need a breaking change, release /v2 and keep /v1 running until old clients have moved over.

Adding a new optional field is safe. Renaming or removing a field is a breaking change.

REST isn't the only option

GraphQL lets the client ask for exactly the fields it needs in one request. gRPC uses compact binary messages and is popular between internal services. REST stays the default for public APIs because every tool understands HTTP.

Quick check

Which is the most RESTful way to delete user 42?