What is a REST API? A beginner's guide with curl examples
Learn what an API is, how HTTP URLs, methods, status codes, and headers work, the principles behind REST, and how to test a real API from the terminal with curl.
Development$ curl -i https://httpbin.org/get▍When you open a banking app and see your balance, the app asks one of the bank’s servers, which responds with the data. That conversation between programs happens through an API, and most APIs on the web follow a style called REST. Let’s break down the pieces of that conversation and test a real API, right from the terminal.
What is an API? The restaurant analogy
API stands for Application Programming Interface. It’s a contract that defines how one program can ask another for something.
Think of a restaurant:
- You are the client (the app, the script, the browser).
- The kitchen is the server, where the data and the rules live.
- The menu is the API documentation: it lists what you can order and in what format.
- The waiter is the API: they take your order to the kitchen and bring back the response.
You don’t need to know how the dish is made; you just order the way the menu describes.
HTTP: the language the API speaks
REST APIs use HTTP, the same protocol your browser uses. Every order (the request) has a few parts.
URL: the resource’s address
The URL says where the thing you want lives. For example, in https://api.example.com/v1/users/42, we have the protocol (https), the server (api.example.com), the API version (v1), and the resource (users/42, user number 42).
Methods: what you want to do
The method (or verb) says what to do with the resource:
| Method | What it’s for | Safe? | Idempotent? |
|---|---|---|---|
| GET | Read a resource or a list | Yes | Yes |
| POST | Create a resource or trigger an action | No | No |
| PUT | Replace an entire resource | No | Yes |
| PATCH | Change part of a resource | No | Not guaranteed |
| DELETE | Delete a resource | No | Yes |
Two important terms:
- Safe: doesn’t change anything on the server. A GET only reads.
- Idempotent: repeating the request leaves the server in the same state as doing it once. Deleting user 42 twice has the same effect as deleting it once; a repeated POST, on the other hand, can create two identical orders, which is why automatic retries need care.
Status codes: how the request went
Every response carries a three-digit status code. The first digit already tells you a lot: 2xx it worked, 3xx redirect, 4xx the client made a mistake, 5xx the server made a mistake.
| Code | Name | When you see it |
|---|---|---|
| 200 | OK | Successful request, with a response body |
| 201 | Created | A new resource was created (common after a POST) |
| 204 | No Content | It worked, but there’s no body (common after a DELETE) |
| 301 / 302 | Moved Permanently / Found | The resource lives at another URL (permanent / temporary) |
| 400 | Bad Request | Malformed request, like invalid JSON |
| 401 | Unauthorized | You didn’t identify yourself or the credential is invalid |
| 403 | Forbidden | You’re identified, but you don’t have permission |
| 404 | Not Found | The resource doesn’t exist |
| 409 | Conflict | Conflict with the current state, like an email that’s already registered |
| 422 | Unprocessable Content | The format is right, but the data fails validation |
| 429 | Too Many Requests | You went over the request limit |
| 500 | Internal Server Error | Unexpected error on the server |
| 502 | Bad Gateway | An intermediary (proxy, load balancer) got an invalid response from the server |
| 503 | Service Unavailable | Server overloaded or under maintenance |
| 504 | Gateway Timeout | An intermediary gave up waiting for the server’s response |
Headers and body
Headers are metadata for the request and the response, in the Name: value format. The ones you’ll see most:
Content-Type: the format of the body being sent, likeapplication/json.Accept: the format the client wants to get back.Authorization: the credential, likeBearer <token>.
The body carries the data, almost always as JSON, a text format made of key-value pairs: {"name": "Ana", "team": "platform"}.
REST principles, in plain words
REST (Representational State Transfer) was described by Roy Fielding in his doctoral dissertation in 2000. It isn’t a tool, but an architectural style:
- Everything is a resource: users, orders, and invoices each have their own URLs. The URL uses nouns (
/orders), and the method says the action. - Stateless: each request carries everything the server needs, including the credential. The server doesn’t “remember” the previous conversation, so any replica can handle any request.
- Uniform interface: every resource uses the same methods and codes. Learn one REST API and you already understand a good chunk of the others.
- Separate client and server, with responses that can be cached when it makes sense.
Hands-on with curl
curl is a command-line tool for making HTTP requests. It ships with Linux, macOS, and current versions of Windows (in Windows PowerShell, type curl.exe, because curl may be an alias for a different command). If the terminal is still new to you, see how to use the terminal. We’ll use httpbin.org, a public testing service that echoes back what it receives:
# Simple GET: the response body comes back as JSON
curl https://httpbin.org/get
# -i also shows the response status and headers
curl -i https://httpbin.org/get
# POST sending JSON
curl -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"name": "Ana", "team": "platform"}'
# Just the status code, great for scripts and health checks
curl -s -o /dev/null -w '%{http_code}\n' https://httpbin.org/status/404
# Sending a token stored in an environment variable
curl -H "Authorization: Bearer $API_TOKEN" https://httpbin.org/bearer
In the POST response, httpbin returns the JSON it received in a json field, something like:
{
"json": {
"name": "Ana",
"team": "platform"
},
"url": "https://httpbin.org/post"
}
In the last example, the token comes from $API_TOKEN instead of being typed into the command, keeping it out of your shell history and out of versioned scripts. Without the header, /bearer responds with 401; try it.
Authentication: how the API knows who you are
- API key: a fixed key sent in a header (for example,
X-API-Key). Simple, but whoever copies the key becomes you. - Bearer token: a token sent in
Authorization: Bearer ..., usually short-lived. It’s often a JWT, a signed token that carries information about who it was issued to. - OAuth 2.0: a protocol for obtaining tokens, which lets an app access your data in another service without knowing your password. Between machines (a CI job calling an API), the common flow is client credentials.
In every case: only over HTTPS, and never with credentials inside the code.
Good practices for API designers
- Versioning:
/v1/orderslets you evolve to/v2without breaking whoever still uses the old version. - Pagination: large lists come in pages (
?limit=50&cursor=...), never all at once. - Rate limiting: a request limit per client, responding with 429 and, ideally, a
Retry-Afterheader. - Idempotency keys: the client sends a header like
Idempotency-Keyon a POST; if the same key arrives again, the server returns the previous result instead of charging twice. - Standardized errors: a single error format, with a code, a message, and details. RFC 9457 (Problem Details) defines a ready-made format, served as
application/problem+json. - Contract with OpenAPI: the OpenAPI specification (formerly Swagger) describes endpoints, parameters, and responses in YAML or JSON, and you can generate documentation, clients, and tests from it.
These practices also show up in our guide to production-ready applications. And if you want to automate API calls, see which programming language to learn for DevOps.
REST, GraphQL, and gRPC
| Style | Format | How it works | Good for |
|---|---|---|---|
| REST | Usually JSON | Many endpoints, one per resource, using HTTP methods | Public APIs and integrations in general |
| GraphQL | JSON | One endpoint; the client describes exactly the fields it wants | Front ends that need to combine lots of data in one call |
| gRPC | Protocol Buffers (binary) | Function calls defined in a contract, over HTTP/2 | Internal communication between microservices, with low latency |
REST is still the starting point: it’s the simplest to test and the most widely supported by tools.
Summary
- An API is a contract that lets one program ask another for something
- Every HTTP request has a URL, a method, headers, and sometimes a body
- GET, PUT, and DELETE are idempotent; POST isn’t, and deserves care with retries
- 4xx status codes mean a client error; 5xx, a server error
- REST treats everything as a resource, with no state between requests
- With
curl -iand-w '%{http_code}', you can test any API from the terminal - Credentials go in headers, over HTTPS, coming from environment variables
Before writing a single line of code against an API, make the first call with curl: it shows you the contract as it really is.