This page provides documentaion for using the ToDo API for CSE 204A. This API allows you to list, get, create, update,
and delete ToDo items via a REST API interface. This API is hosted in AWS and you can view the source code on
GitHub
here.
Generate an API key here.
This API key is used to reference all data associated with your user. Note: It is essentially a password that authorizes API calls to access your data; so, keep it private!
The API key must be sent with every request to the API and is sent via a header with a key of
x-api-key
.
Each ToDo is stored in the database with a number of fields. Some can be modified, and some are set automatically.
See the table below for an explanation of the properties in each
todo
object.
Property Name | Data Type | Example | Source | Description |
---|---|---|---|---|
text
|
string |
Buy Groceries | Set by user in POST request | The main text of the ToDo |
id
|
string |
0ef61ae0-dc34-11e8-bd4d-71a1c19eb6c3 | Automatically generated | Unique ID of the ToDo. Used when making updates or deleting. |
completed
|
boolean |
false | Defaults to false | Whether the ToDo has been completed. Needs to be manually updated to
true . |
created
|
number |
1540897678 | Set when ToDo is created | Unix timestamp representing time when ToDo was created. Can be used to sort. |
updated
|
number |
1540897798 | Set when ToDo is modified | Unix timestamp representing time when ToDo was modified. Can be used to sort. |
user
|
string |
38c5439331dbc7e... | User API Key | Unique ID representing a specific user who the ToDo belongs to. |
The ToDo API endpoints accept JSON formatted data in the request body where indicated. All endpoints return JSON data in the response body.
Endpoint URL | Method | JSON Request Body | JSON Response Body |
---|---|---|---|
https://cse204.work/todos
List ToDos: This endpoint returns an array of all ToDos for the provided user. More Info |
GET
|
(none) |
Show Example Response
[ { "completed": false, "user": "<your api key>", "text": "A thing", "id": "4f8c4ff0-da29" }, { "completed": true, "user": "<your api key>", "text": "Another thing", "id": "50d8cd20-da29" } ] |
https://cse204.work/todos
Create A ToDo: Sending data via a
|
POST
|
{ "text": "Another new thing" } |
Show Example Response
{ "user": "<your api key>", "id": "3a3d6a40-da37", "text": "Another new thing", "completed": false } |
https://cse204.work/todos/{id}
Get A ToDo: Sending a
|
GET
|
(none) |
Show Example Response
{ "completed": false, "user": "<your api key>", "text": "Another new thing", "id": "3a3d6a40-da37" } |
https://cse204.work/todos/{id}
Update A ToDo: A
|
PUT
|
{ "completed": true } |
Show Example Response
{ "completed": true, "user": "<your api key>", "text": "A thing", "id": "50d8cd20-da29" } |
https://cse204.work/todos/{id}
Delete A ToDo: A
|
DELETE
|
(none) | (none) |
On page load, your webpage will need to render all existing ToDos tied to your API key. To add the data to your HTML, make a GET AJAX request to this URL:
https://cse204.work/todos
Note: The API key must be sent as an
x-api-key
header. This is done using the
setRequestHeader
method:
xhttp.setRequestHeader("x-api-key", "abc123");
This AJAX request will return an array of objects as JSON. Each object will be a ToDo that has been previously
submitted with your API key. To parse this JSON into a usable array, use
JSON.parse()
:
let todos = JSON.parse(this.responseText);
Note: Before you have successfully submitted any ToDos, this array will be empty.
let xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { let todos = JSON.parse(this.responseText); console.log(todos); } }; xhttp.open("GET", "https://cse204.work/todos", true); xhttp.setRequestHeader("x-api-key","abc123"); xhttp.send();
When your new ToDo form is submitted, you want to trigger a JavaScript event that will make an AJAX call to submit your new ToDo to the server, and if saving it is successful, add your new ToDo to the page's HTML. Do this by making an AJAX POST request to this URL:
https://cse204.work/todos
Your POST will need the following parameters, encoded as JSON:
If unsuccessful, this POST request will return a text error. If successful, it will return a single object containing
the ToDo that has been saved. This object will have a
text
,
id
, and
user
property that you can use to add a new HTML element to your existing ToDo.
// Setting variable for form input (get from HTML form) let data = { text: "Some Text" } // Initalize AJAX Request let xhttp2 = new XMLHttpRequest(); // Response handler xhttp2.onreadystatechange = function() { // Wait for readyState = 4 & 200 response if (this.readyState == 4 && this.status == 200) { // parse JSON response let todo = JSON.parse(this.responseText); console.log(todo); } else if (this.readyState == 4) { // this.status !== 200, error from server console.log(this.responseText); } }; xhttp2.open("POST", "https://cse204.work/todos", true); xhttp2.setRequestHeader("Content-type", "application/json"); xhttp2.setRequestHeader("x-api-key", "abc123"); xhttp2.send(JSON.stringify(data));
To retrieve the info for a single ToDo, send a GET request to this URL, with the ToDo's
id
in place of
{id}
.
https://cse204.work/todos/{id}
Make sure you include your API Key in the
x-api-key
header.
If unsuccessful, this GET request will return a text error. If successful, it will return a single JSON object
containing the ToDo that you requested. This object will have
text
,
id
,
completed
,
created
,
updated
, and
user
properties.
Note: Depending on how you structure your application, you may not need to use this endpoint, since the LIST endpoint returns full ToDo objects.
// Setting variable for ToDo id const id = "abcd-1234" // Initalize AJAX Request let xhttp2 = new XMLHttpRequest(); // Response handler xhttp2.onreadystatechange = function() { // Wait for readyState = 4 & 200 response if (this.readyState == 4 && this.status == 200) { // parse JSON response let todo = JSON.parse(this.responseText); console.log(todo); } else if (this.readyState == 4) { // this.status !== 200, error from server console.log(this.responseText); } }; xhttp2.open("GET", "https://cse204.work/todos/"+id, true); xhttp2.setRequestHeader("Content-type", "application/json"); xhttp2.setRequestHeader("x-api-key", "abc123"); xhttp2.send(JSON.stringify(data));
To change the properties of a ToDo, send a PUT
request with the data you want to update to this URL, with the
id
of the ToDo you want to update in place of
{id}
.
https://cse204.work/todos/{id}
You will use this endpoint to change a ToDo's status to "completed" once it has been checked off. Optionally,
if you want to allow for editing the text of your ToDos, you can accomplish that by including a new
text
value in your PUT
request to this endpoint.
If unsuccessful, this PUT
request will return a text error. If successful, it will return a
200
response code and an empty body response.
// You're on your own for this one! // Make sure you include a ToDo ID in the URL, you will probably get that ID from the ID field of your ToDo element in HTML inside your click event. // Also, make sure you make the request method "PUT" and include a JSON object with your updates, probably something like: let data = { completed: true }
To delete a ToDo, send a DELETE
request with the data you want to update to this URL, with the
id
of the ToDo you want to update in place of
{id}
.
https://cse204.work/todos/{id}
If unsuccessful, this DELETE
request will return a text error. If successful, it will return a
200
response code and an empty body response.
// You're on your own for this one! // Make sure that you set the request method to "DELETE"