APIs are the backbone of modern web development, and RESTful conventions provide a standardized way to design and interact with web API servers. This lesson will guide you through creating RESTful endpoints for a JSON web API server and understanding their practical applications.
A web API server focuses solely on data—returning it, manipulating it, or both. Unlike traditional HTML web servers, which render entire pages, web API servers operate without needing to generate or return HTML forms or pages. Instead, they use various HTTP methods to handle specific actions.
RESTful APIs leverage these HTTP methods—GET, POST, PUT/PATCH, and DELETE—to align with CRUD operations (Create, Read, Update, Delete). This approach makes APIs more versatile and intuitive for developers.
| Path Pattern | HTTP Verb | Meaning |
|---|---|---|
| /resource-name | GET | Retrieve all records of the resource |
| /resource-name | POST | Create a new record for the resource |
| /resource-name/record-id | GET | Retrieve details of the specified record |
| /resource-name/record-id | PUT/PATCH | Update the specified record |
| /resource-name/record-id | DELETE | Delete the specified record |
For nested resources, the paths follow a similar pattern, but include the parent resource in the path.
For example, /posts/1/comments could represent all comments associated with a specific post.
Consider a hypothetical Twitter API. Here's how RESTful endpoints might look for managing tweets:
| HTTP Verb | Meaning | Example |
|---|---|---|
| GET | Retrieve all tweets | /my/tweets |
| POST | Create a new tweet | /my/tweets |
| DELETE | Delete all tweets | /my/tweets |
| HTTP Verb | Meaning | Example |
|---|---|---|
| GET | Retrieve a specific tweet | /my/tweets/17 |
| PUT | Replace tweet details | /my/tweets/17 |
| PATCH | Update specific tweet details | /my/tweets/17 |
| DELETE | Delete a specific tweet | /my/tweets/17 |
Imagine a weather API with the endpoint /weather/current. Instead of pointing to a static
record, the "current" keyword triggers a lookup for the most recent weather data. This dynamic behavior
highlights the power of RESTful APIs for real-time data retrieval.
GitHub's RESTful API is a gold standard in API design. Consider this endpoint: https://api.github.com/users/app-academy. It retrieves JSON-formatted information about the App Academy GitHub user. Here's a sample response:
{
"login": "app-academy",
"id": 3155975,
"url": "https://api.github.com/users/app-academy",
"repos_url": "https://api.github.com/users/app-academy/repos",
"followers_url": "https://api.github.com/users/app-academy/followers",
"public_repos": 2,
"followers": 3
}
By following links in the JSON (e.g., followers_url), you can explore related data, such as
a list of followers. This interconnected design makes APIs highly flexible and extensible.
/weather/current demonstrate dynamic behavior, while GitHub's API showcases
interconnected data access.With this knowledge, you're equipped to design, understand, and utilize RESTful APIs in modern web development.