|
Download data/knowledge/webs/rest_api_design.md from amkyawdev/burme-coder-max: direct link, hf CLI and curl.
- Browser
- Download file 3.42 kB
-
https://huggingface.co/datasets/amkyawdev/burme-coder-max/resolve/main/data/knowledge/webs/rest_api_design.md
- Command line
-
hf download hf://datasets/amkyawdev/burme-coder-max/data/knowledge/webs/rest_api_design.md
-
curl -L -o rest_api_design.md https://huggingface.co/datasets/amkyawdev/burme-coder-max/resolve/main/data/knowledge/webs/rest_api_design.md
3.42 kB
REST API Design
HTTP Methods
| Method | Purpose | Example |
|---|---|---|
| GET | Retrieve data | GET /users |
| POST | Create new resource | POST /users |
| PUT | Replace entire resource | PUT /users/1 |
| PATCH | Partial update | PATCH /users/1 |
| DELETE | Remove resource | DELETE /users/1 |
URL Structure
https://api.example.com/v1/users/123/posts?page=1&limit=10
│ │ │ │ │
│ │ │ │ └── Query Parameters
│ │ │ └── Resource ID
│ │ └── Collection
│ └── Version
└── Base URL
Status Codes
Success
200 OK- Request succeeded201 Created- Resource created204 No Content- Success, no body (DELETE)
Client Errors
400 Bad Request- Invalid input401 Unauthorized- Authentication required403 Forbidden- Authenticated but not permitted404 Not Found- Resource doesn't exist422 Unprocessable Entity- Validation error
Server Errors
500 Internal Server Error- Server error502 Bad Gateway- Gateway error503 Service Unavailable- Server down
Best Practices
Request
GET /api/v1/users?status=active&sort=-created_at
Accept: application/json
Authorization: Bearer <token>
Response
{
"data": [
{
"id": 1,
"name": "John",
"email": "john@example.com"
}
],
"meta": {
"page": 1,
"per_page": 20,
"total": 100
},
"links": {
"self": "/api/v1/users?page=1",
"next": "/api/v1/users?page=2"
}
}
Error Response
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Invalid input data",
"details": [
{
"field": "email",
"message": "Invalid email format"
}
]
}
}
REST API Example (Node.js/Express)
Basic Setup
const express = require('express');
const app = express();
app.use(express.json());
// GET all users
app.get('/api/v1/users', (req, res) => {
res.json({ data: users });
});
// GET single user
app.get('/api/v1/users/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));
if (!user) return res.status(404).json({ error: 'Not found' });
res.json({ data: user });
});
// POST create user
app.post('/api/v1/users', (req, res) => {
const { name, email } = req.body;
// Validation
if (!name || !email) {
return res.status(400).json({ error: 'Missing fields' });
}
// Create
const newUser = { id: users.length + 1, name, email };
users.push(newUser);
res.status(201).json({ data: newUser });
});
// PUT update user
app.put('/api/v1/users/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));
if (!user) return res.status(404).json({ error: 'Not found' });
const { name, email } = req.body;
user.name = name || user.name;
user.email = email || user.email;
res.json({ data: user });
});
// DELETE user
app.delete('/api/v1/users/:id', (req, res) => {
const index = users.findIndex(u => u.id === parseInt(req.params.id));
if (index === -1) return res.status(404).json({ error: 'Not found' });
users.splice(index, 1);
res.status(204).send();
});