The user wants a simple REST API design for a blog application.
The core requirements are:
- Context: Blog application.
- Functionality: CRUD (Create, Read, Update, Delete) for blog posts.
- Format: REST API design.
1. Identify the Resource:
The primary resource is a "Blog Post".
Let's name the resource posts.
2. Determine the Base URL:
A standard convention is /api/v1.
So, the endpoint base will be /api/v1/posts.
3. Design the Endpoints (CRUD Mapping):
-
Create (POST):
- Method:
POST
- URL:
/posts
- Body: JSON data containing title, content, author, etc.
- Response: 201 Created, with the created object.
-
Read (GET):
- Get All: Method
GET, URL /posts. Needs pagination/filtering ideally, but for a "simple" design, I'll list it as a basic GET. Response: 200 OK, array of posts.
- Get One: Method
GET, URL /posts/{id}. Response: 200 OK, single post object, or 404 Not Found.
-
Update (PUT vs. PATCH):
- PUT (Full update): Replaces the entire resource.
- PATCH (Partial update): Updates only provided fields.
- Decision: For a simple API,
PUT is often easier to explain as "update this record", but PATCH is more realistic for blogs. I'll provide PUT as the primary update method for simplicity, or maybe offer both if I want to be thorough. Let's stick to PUT for the standard CRUD example, as it's often what beginners expect, but I will note PATCH is an option. Actually, let's use PUT for replacing the whole post.
- URL:
/posts/{id}
- Body: JSON data.
- Response: 200 OK (updated object).
-
Delete (DELETE):
- Method:
DELETE
- URL:
/posts/{id}
- Response: 204 No Content (standard for successful deletion with no body return).
4. Define the Data Model (Schema):
What does a "Post" look like?
id: Unique identifier (UUID or integer).
title: String.
content: String (text).
author: String (or author ID, but keeping it simple).
createdAt: Timestamp.
updatedAt: Timestamp.
tags: Array of strings (optional but good).
5. Structure the Response:
Standard JSON.
6. Error Handling:
Standard HTTP status codes.
- 200: Success.
- 201: Created.
- 204: No Content.
- 400: Bad Request (validation error).
- 404: Not Found.
- 500: Server Error.
7. Refining the Output:
I should organize the answer logically:
- Base URL: Define the root.
- Resource Model: JSON structure.
- Endpoints Table/List: Clear mapping of Method, Endpoint, Description.
- Detailed Examples: Show Request/Response for each operation. This is crucial for clarity.
Self-Correction/Refinement during drafting:
- Authentication: The prompt asks for a "simple" API. Adding JWT/OAuth might overcomplicate it. I will mention it as a note but not implement it in the design details.
- Versioning:
/api/v1 is best practice. I'll include it.
- Pagination: Getting "all" posts is dangerous in production. I should mention query parameters for pagination (e.g.,
?page=1&limit=10) even in a simple design to show good habits.
Final Plan:
- Title: REST API Design for Blog Posts.
- Resource Object: JSON example.
- Endpoints:
GET /posts (List)
POST /posts (Create)
GET /posts/{id} (Read)
PUT /posts/{id} (Update)
DELETE /posts/{id} (Delete)
- Request/Response Examples: Concrete JSON blocks.
- Status Codes: A quick reference list.
Let's write it out.
Here is a design for a simple, RESTful JSON API for a blog application.
1. The Resource Model (Post)
Before defining the endpoints, we need to define what a "Post" looks like. This is the JSON object that will be sent and received.
Schema: Post
{
"id": 1,
"title": "My First Blog Post",
"content": "This is the content of the post...",
"author": "John Doe",
"createdAt": "2023-10-27T10:00:00Z",
"updatedAt": "2023-10-27T10:00:00Z",
"tags": ["technology", "coding"]
}
2. API Endpoints
The base URL for the API is assumed to be /api/v1.
| Operation | HTTP Method | Endpoint | Description |
|---|
| List Posts | GET | /posts | Retrieve a list of all posts. |
| Create Post | POST | /posts | Create a new blog post. |
| Get Post | GET | /posts/{id} | Retrieve a specific post by its ID. |
| Update Post | PUT | /posts/{id} | Update an existing post. |
| Delete Post | DELETE | /posts/{id} | Delete a post. |
3. Detailed Examples
A. List all posts
Retrieves a list of posts. Ideally, this should support pagination (e.g., ?page=1&limit=10), but here is the simple version.
- Endpoint:
GET /api/v1/posts
- Response:
200 OK
{
"status": "success",
"data": [
{
"id": 1,
"title": "REST API Best Practices",
"content": "Content here...",
"author": "Alice",
"createdAt": "2023-10-26T12:00:00Z"
},
{
"id": 2,
"title": "Understanding HTTP",
"content": "Content here...",
"author": "Bob",
"createdAt": "2023-10-25T09:30:00Z"
}
],
"count": 2
}
B. Create a new post
Creates a new resource. The client sends the data, and the server returns the created object (usually with the generated ID and timestamp).
- Endpoint:
POST /api/v1/posts
- Request Body:
{
"title": "My New Post",
"content": "This is the body of the new post.",
"author": "Charlie",
"tags": ["news"]
}
{
"status": "success",
"data": {
"id": 3,
"title": "My New Post",
"content": "This is the body of the new post.",
"author": "Charlie",
"tags": ["news"],
"createdAt": "2023-10-27T14:00:00Z",
"updatedAt": "2023-10-27T14:00:00Z"
}
}
C. Get a single post
Retrieves a specific resource by its unique identifier.
- Endpoint:
GET /api/v1/posts/3
- Response:
200 OK
{
"status": "success",
"data": {
"id": 3,
"title": "My New Post",
"content": "This is the body of the new post.",
"author": "Charlie",
"createdAt": "2023-10-27T14:00:00Z"
}
}
- Error Response (if not found):
404 Not Found
D. Update a post
Updates an existing resource. PUT usually replaces the entire resource, while PATCH updates partially. We will use PUT here for simplicity.
- Endpoint:
PUT /api/v1/posts/3
- Request Body:
{
"title": "My Updated Title",
"content": "Updated content.",
"author": "Charlie",
"tags": ["news", "update"]
}
- Response:
200 OK (Returns the updated object)
{
"status": "success",
"data": {
"id": 3,
"title": "My Updated Title",
"content": "Updated content.",
"author": "Charlie",
"tags": ["news", "update"],
"updatedAt": "2023-10-27T15:00:00Z"
}
}
E. Delete a post
Removes a resource.
- Endpoint:
DELETE /api/v1/posts/3
- Response:
204 No Content
- Note: A 204 response usually has an empty body.
4. HTTP Status Codes Summary
- 200 OK: Successful request (GET, PUT).
- 201 Created: Successful resource creation (POST).
- 204 No Content: Successful deletion (DELETE).
- 400 Bad Request: Invalid input (e.g., missing title).
- 404 Not Found: Resource does not exist.
- 500 Internal Server Error: Something went wrong on the server.