Todos
The Todos resource represents todo/task items for users. Each todo belongs to a user and has a completion status.
Base URL
bash
https://api.jsonplaceholder.dev
bash
http://localhost:3000
Schema
typescript
interface Todo {
id: number
userId: number
title: string
completed: boolean
}
Endpoints
Get All Todos
bash
GET /todos
Response:
json
[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
// ... 199 more todos
]
Get Todos by User
bash
GET /todos?userId={userId}
Parameters:
userId
(number, query) - Filter todos by user ID
Response:
json
[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
// ... more todos for user 1
]
Get Single Todo
bash
GET /todos/{id}
Parameters:
id
(number) - Todo ID
Response:
json
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
Create Todo
bash
POST /todos
Request Body:
json
{
"userId": 1,
"title": "Learn Bun.js",
"completed": false
}
Response: 201 Created
json
{
"id": 201,
"userId": 1,
"title": "Learn Bun.js",
"completed": false
}
Update Todo (Full Replace)
bash
PUT /todos/{id}
Parameters:
id
(number) - Todo ID
Request Body:
json
{
"userId": 1,
"title": "Learn Bun.js thoroughly",
"completed": true
}
Response: 200 OK
json
{
"id": 1,
"userId": 1,
"title": "Learn Bun.js thoroughly",
"completed": true
}
Update Todo (Partial)
bash
PATCH /todos/{id}
Parameters:
id
(number) - Todo ID
Request Body: Partial todo object
json
{
"completed": true
}
Response: 200 OK
- Updated todo object
Delete Todo
bash
DELETE /todos/{id}
Parameters:
id
(number) - Todo ID
Response: 200 OK
json
{}
Examples
javascript
// Get all todos
const todos = await fetch('http://localhost:3000/todos')
.then(res => res.json());
// Get todos by user
const userTodos = await fetch('http://localhost:3000/todos?userId=1')
.then(res => res.json());
// Create new todo
const newTodo = await fetch('http://localhost:3000/todos', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
userId: 1,
title: 'Build a REST API with Bun',
completed: false
})
}).then(res => res.json());
// Mark todo as completed
const completedTodo = await fetch('http://localhost:3000/todos/1', {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
completed: true
})
}).then(res => res.json());
// Delete todo
await fetch('http://localhost:3000/todos/1', {
method: 'DELETE'
});
bash
# Get all todos
curl http://localhost:3000/todos
# Get todos by user
curl http://localhost:3000/todos?userId=1
# Create new todo
curl -X POST http://localhost:3000/todos \
-H "Content-Type: application/json" \
-d '{
"userId": 1,
"title": "Build a REST API with Bun",
"completed": false
}'
# Mark todo as completed
curl -X PATCH http://localhost:3000/todos/1 \
-H "Content-Type: application/json" \
-d '{"completed": true}'
# Delete todo
curl -X DELETE http://localhost:3000/todos/1
python
import requests
# Get all todos
todos = requests.get('http://localhost:3000/todos').json()
# Get todos by user
user_todos = requests.get('http://localhost:3000/todos?userId=1').json()
# Create new todo
new_todo = requests.post('http://localhost:3000/todos', json={
'userId': 1,
'title': 'Build a REST API with Bun',
'completed': False
}).json()
# Mark todo as completed
completed_todo = requests.patch('http://localhost:3000/todos/1', json={
'completed': True
}).json()
# Delete todo
requests.delete('http://localhost:3000/todos/1')
Common Use Cases
Todo Management App
javascript
// Get user's incomplete todos
const incompleteTodos = await fetch('http://localhost:3000/todos?userId=1')
.then(res => res.json())
.then(todos => todos.filter(todo => !todo.completed));
// Complete multiple todos
const todoIds = [1, 2, 3];
const completedTodos = await Promise.all(
todoIds.map(id =>
fetch(`http://localhost:3000/todos/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ completed: true })
}).then(res => res.json())
)
);
Progress Tracking
javascript
// Calculate completion percentage
const userTodos = await fetch('http://localhost:3000/todos?userId=1')
.then(res => res.json());
const completedCount = userTodos.filter(todo => todo.completed).length;
const completionRate = (completedCount / userTodos.length) * 100;
console.log(`User completion rate: ${completionRate.toFixed(1)}%`);
Related Resources
- Users - The user this todo is assigned to