Need an API key? Visit Get API Key to generate one via the web UI.
Axios — if you prefer using Axios, include it in your HTML with:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
Then set the x-api-key once (example):
const KEY = 'YOUR_API_KEY';
axios.defaults.headers.common['x-api-key'] = KEY;
All ToDo endpoints require an HTTP header x-api-key. Examples below include curl, fetch and axios usage (axios examples assume you set the default header once as shown above).
GET /todos
# curl
curl -s -H "x-api-key: YOUR_KEY" https://cse2004.us/todos
# fetch
const API = 'https://cse2004.us';
const KEY = 'YOUR_API_KEY';
const res = await fetch(API + '/todos', { headers: { 'x-api-key': KEY } });
const todos = await res.json();
# axios
const response = await axios.get(API + '/todos');
const todos = response.data;
Response: Array of { id: number, user: string, text: string, completed: boolean, created: number, updated: number }
POST /todos with body { text: string }
# curl
curl -s -X POST https://cse2004.us/todos \
-H "x-api-key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{ "text": "Buy milk" }'
# fetch
const res = await fetch(API + '/todos', {
method: 'POST',
headers: {
'x-api-key': KEY,
'content-type': 'application/json'
},
body: JSON.stringify({ text: 'Buy milk' })
});
const todo = await res.json();
# axios
const response = await axios.post(API + '/todos', { text: 'Buy milk' });
const todo = response.data;
Response: { id: number, user: string, text: string, completed: boolean, created: number, updated: number }
GET /todos/:id
# curl
curl -s -H "x-api-key: YOUR_KEY" https://cse2004.us/todos/1
# fetch
const id = 1;
const res = await fetch(API + '/todos/' + id, {
headers: { 'x-api-key': KEY }
});
const todo = await res.json();
# axios
const response = await axios.get(API + '/todos/' + id);
const todo = response.data;
Response: { id, user, text, completed, created, updated }
PUT /todos/:id with body { text?: string, completed?: boolean }
# curl
curl -s -X PUT https://cse2004.us/todos/1 \
-H "x-api-key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{ "completed": true }'
# fetch
const id = 1;
const res = await fetch(API + '/todos/' + id, {
method: 'PUT',
headers: {
'x-api-key': KEY,
'content-type': 'application/json'
},
body: JSON.stringify({ completed: true })
});
const updated = await res.json();
# axios
const response = await axios.put(API + '/todos/' + id, { completed: true });
const updated = response.data;
Response: { id, user, text, completed, created, updated }
DELETE /todos/:id
# curl
curl -s -X DELETE https://cse2004.us/todos/1 -H "x-api-key: YOUR_KEY"
# fetch
const id = 1;
const res = await fetch(API + '/todos/' + id, {
method: 'DELETE',
headers: { 'x-api-key': KEY }
});
const result = await res.json(); // {}
# axios
const response = await axios.delete(API + '/todos/' + id);
const result = response.data;