A REST API (Representational State Transfer Application Programming Interface) is a set of rules that lets one piece of software talk to another over the internet using standard HTTP methods. You enter a URL (the endpoint), send a request, and the server returns a response, usually in JSON. This guide explains the key parts of a REST API—requests, responses, and endpoints—with real examples so you can read API documentation and make your first calls with confidence.
What Is a REST API?
REST stands for Representational State Transfer. It is an architectural style, not a protocol or a standard. It was introduced by Roy Fielding in his 2000 doctoral dissertation. REST defines a set of constraints that, when followed, make an API simple, scalable, and predictable. The most important constraint is that the API uses HTTP methods (GET, POST, PUT, DELETE) to perform actions on resources, and each resource is identified by a URL.
In practice, a REST API works like a waiter in a restaurant. You (the client) ask the waiter (the API) for a dish (a resource). The waiter takes your order to the kitchen (the server), and the kitchen returns the dish to the waiter, who brings it to you. The menu lists the available dishes—that is the API documentation. The waiter doesn’t let you into the kitchen; you only interact through the menu and the waiter. Similarly, a REST API exposes endpoints, and you never access the server’s internal data directly.
HTTP Requests and Methods
Every REST API call is an HTTP request. A request consists of:
- Method (or verb): what you want to do.
- URL (endpoint): which resource you want to act on.
- Headers: extra information like authentication tokens or content type.
- Body (optional): data you send, usually in JSON, for POST or PUT requests.
The most common methods are:
- GET – retrieve data. It never changes the server state. Example: GET /users/42 returns the user with ID 42.
- POST – create a new resource. Example: POST /users with a JSON body creates a new user.
- PUT – replace an entire resource. Example: PUT /users/42 replaces user 42 with the data you send.
- PATCH – partially update a resource. Example: PATCH /users/42 with {“name”: “New Name”} updates only the name.
- DELETE – remove a resource. Example: DELETE /users/42 deletes user 42.
Each method has a specific meaning. Using the wrong method is a common mistake. For example, using GET to delete a resource is not RESTful and can cause security issues because GET requests are often logged or cached.
Responses and Status Codes
The server replies with an HTTP response that includes a status code, headers, and a body. The status code tells you whether the request succeeded and why. You will see these often:
- 200 OK – the request succeeded, and the response contains the data.
- 201 Created – a new resource was created (typically after a POST).
- 204 No Content – the request succeeded, but there is no body (often after a DELETE).
- 400 Bad Request – the request was malformed or missing required fields.
- 401 Unauthorized – you need to authenticate (log in) first.
- 403 Forbidden – you are authenticated but don’t have permission.
- 404 Not Found – the endpoint does not exist or the resource was not found.
- 500 Internal Server Error – the server crashed or has a bug.
The response body is usually JSON, like this:
{
"id": 42,
"name": "Ada",
"email": "ada@example.com"
}
You can test a public REST API with tools like curl in your terminal or with a browser extension like Postman. For example, running curl https://api.example.com/users/42 sends a GET request and prints the JSON response.
Endpoints and Resources
An endpoint is a specific URL that points to a resource or a collection of resources. The URL structure follows a pattern. For example, in a blog API:
/posts– the collection of all posts./posts/1– the post with ID 1./posts/1/comments– the comments on post 1./users/5/posts– posts written by user 5.
Endpoints often accept query parameters to filter, sort, or paginate. For example:
GET /posts?author=5&sort=desc&page=2
This asks for posts by author 5, sorted by newest first, on page 2. The response might include a total count and a next link to fetch the next page.
Designing good endpoints is about using nouns, not verbs. So /posts is correct, while /getPosts is not RESTful. The HTTP method already tells the server what to do, so the URL should only identify the resource.
Authentication and Security
Most real-world REST APIs require authentication. The simplest way is an API key—a long string you send in a header. For example:
Authorization: Bearer your_api_key_here
OAuth 2.0 is a more complex but common method that lets users authorize third-party apps without sharing passwords. For public data, no authentication is needed.
Security best practices include:
- Always use HTTPS (not HTTP) to encrypt data in transit.
- Never expose API keys in client-side code like JavaScript in a browser.
- Validate all input on the server to prevent injection attacks.
- Rate-limit requests to prevent abuse.
If you are building an API, never log sensitive data like passwords or tokens. If you are consuming an API, keep your keys secret and rotate them regularly.
Common Mistakes and Misconceptions
Beginners often confuse REST with HTTP. REST is a style; HTTP is the protocol. Not every HTTP API is RESTful. A true REST API follows all constraints, including statelessness and uniform interface. In practice, many APIs call themselves REST but only use HTTP methods and JSON. That is common and often acceptable, but it is technically not fully RESTful.
Another mistake is ignoring status codes. If you only check whether the request succeeded (200) and ignore a 404 or 500, your app will break in confusing ways. Always handle non-200 responses.
Also, be careful with PUT vs. PATCH. PUT replaces the whole resource; PATCH updates only the fields you send. Sending a partial object to a PUT endpoint will wipe out missing fields.
Finally, don’t assume the response format is always JSON. Some APIs return XML or plain text. Check the Content-Type header in the response.
Frequently Asked Questions
What is the difference between REST and GraphQL? REST uses multiple endpoints, each returning a fixed structure. GraphQL uses a single endpoint where you specify exactly which fields you need. REST is simpler to cache and understand; GraphQL reduces over-fetching but has a steeper learning curve.
Can I use REST APIs without authentication? Yes, many public APIs are open. For example, weather APIs or public data APIs often require only a free API key. But any API that handles personal data will require authentication.
What does “stateless” mean in REST? It means the server does not store any client state between requests. Each request contains all the information needed to process it. This makes scaling easier because any server can handle any request.
How do I test a REST API? You can use curl in a terminal, or tools like Postman, Insomnia, or even your browser for GET requests. For POST or PUT, you need a tool that lets you set the method and body.
What is the difference between REST and SOAP? SOAP is a strict protocol with XML envelopes and built-in error handling. REST is an architectural style that uses JSON and HTTP methods. REST is lighter and more popular for modern web APIs.
What is an endpoint URL example? For GitHub’s API, https://api.github.com/users/octocat returns public information about the user “octocat”. That URL is an endpoint.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Campus Tech Innovation Hubs: What They Are
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It