REST API Guide
Understanding REST APIs
Learn how modern web applications communicate over the internet
A REST API (Representational State Transfer Application Programming Interface) is a standardized way for web applications to communicate with each other over the internet. It enables frontend applications to interact with backend servers, retrieve or modify data, and integrate dynamic functionalities seamlessly.
REST APIs follow a stateless architecture, meaning each request is independent and must contain all necessary information for the server to process it. They use standard HTTP methods to perform operations on resources, making them easy to use and scalable.
Key Components of a REST API
1. Base URL
The base URL is the foundation of an API. All API endpoints are built upon this base URL.
https://crudifydev.vercel.app/api/v1
All requests to Crudify's API must begin with this base URL.
HTTP Methods
REST APIs rely on HTTP methods to perform specific actions on resources. The most commonly used HTTP methods are:
Method | Operation | Description |
---|---|---|
GET | Read | Retrieves resources or collections of resources |
POST | Create | Creates a new resource |
PUT | Update/Replace | Updates an entire resource or replaces it completely |
PATCH | Update/Modify | Partially updates a resource |
DELETE | Delete | Removes a resource |
Example Requests:
GET (Retrieve Data)
-H "Authorization: Bearer your-api-token-here"
POST (Create a New Entry)
-H "Authorization: Bearer your-api-token-here" \
-H "Content-Type: application/json" \
-d '{"task": "curd api todo list api"}'
PATCH (Update Partially)
-H "Authorization: Bearer your-api-token-here" \
-H "Content-Type: application/json" \
-d '{"task": "Cooking","isDone": false}'
DELETE (Remove an Entry)
-H "Authorization: Bearer your-api-token-here"
Endpoints and Resources
An endpoint is a specific URL where the API listens for requests. Each endpoint corresponds to a resource (e.g., users, tasks, orders) and supports specific HTTP methods.
Example Endpoints:
GET /todo-list
→ Retrieve a list of to-do items.POST /todo-list
→ Create a new to-do item.GET /todo-list/{id}
→ Retrieve a specific to-do item.PATCH /todo-list/{id}
→ Update an existing to-do item.DELETE /todo-list/{id}
→ Delete a specific to-do item.
Query Parameters
Many APIs allow filtering, sorting, or pagination via query parameters to optimize data retrieval.
Example Query Parameters:
- Pagination:
GET /todo-list?page=2&limit=10
(Retrieve 10 items per page, starting from page 2) - Sorting:
GET /todo-list?sort=created_at&order=desc
(Sort by creation date in descending order) - Filtering:
GET /todo-list?isDone=false
(Retrieve only incomplete tasks)
Example Request Using Query Parameters:
-H "Authorization: Bearer your-api-token-here"
Request Headers
Headers provide additional information in API requests, such as authentication details and content format.
Common Headers:
- Authorization: Typically a Bearer token used for secure access (e.g.,
Bearer your-api-token-here
). - Content-Type: Specifies the request format (e.g.,
application/json
).
Request and Response Formats
Most REST APIs use JSON (JavaScript Object Notation) as the standard format for sending and receiving data.
Example Request (JSON Body in POST Request):
"task": "New Task"
}
Example Response (JSON Format):
"id": 1,
"task": "sksk",
"isDone": false,
"createdAt": "2024-11-16T03:05:41.855Z",
"updatedAt": "2024-11-16T03:05:41.855Z"
}
Status Codes and Error Handling
REST APIs return HTTP status codes to indicate the success or failure of a request.
Code Range | Category | Examples |
---|---|---|
2xx | Success | 200 OK, 201 Created, 204 No Content |
4xx | Client Error | 400 Bad Request, 401 Unauthorized, 404 Not Found |
5xx | Server Error | 500 Internal Server Error, 503 Service Unavailable |
Example Error Response (JSON Format):
"error": "Invalid API token",
"status": 401
}
Using REST API with JavaScript (Fetch Example)
To interact with the Crudify API in a frontend application, you can use JavaScript's fetch()
method.
method: "GET",
headers: {
"Authorization": "Bearer your-api-token-here",
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error("Error:", error));
Best Practices for Using REST APIs
- Use Environment Variables for API Tokens: Avoid exposing API keys in your code.
- Follow API Rate Limits: Respect any request limits set by the API provider.
- Handle Errors Gracefully: Implement error handling for failed requests.
- Use Pagination for Large Data Requests: If an API returns a lot of data, use pagination to manage responses efficiently.
- Secure API Requests: Always use HTTPS and authentication mechanisms to protect sensitive data.
Start Building with REST APIs
Now that you understand the fundamentals of REST APIs, you can integrate them into your projects using CRUD operations. Whether you're developing a React, Vue.js, or mobile application, the Crudify API provides you with real-world API endpoints to practice and enhance your skills.
Explore the Crudify API Documentation to dive deeper into the available endpoints and start building today!
Happy coding! 🚀