I searched the internet, and I couldnโt find a free API that accepts CORS and lets you do POST requests.
Most APIs out there donโt accept CORS requests. You canโt send them a request through the browser. You can only send them requests through a server.
Examples of these APIs include:
Some APIs accept CORS without requiring any authentication. You can only send GET requests to these APIs. You canโt send POST, PUT, or DELETE requests to them (because they donโt want you to change their databases).
An example of such an API is the Open Dota API.
I want to let you experience building an interface that uses a real API. So I decided to build an API just for the Todolist.
The Todolist API lets you:
- Get a list of tasks
- Create a new task
- Edit a task
- Delete a task
Youโll learn more about the API in this lesson.
The root endpoint
The root endpoint for the API is:
https://api.learnjavascript.todayUsers
You should have access to your own todolist. Other students should not be able to access your todolist. To do this, we need user accounts.
Create user
You need a user to use the Todolist API. The API returns the username and id of the user if the request is successful:
POST /usersInput:
| Field | Type | Description |
|---|---|---|
username |
String | Required. The userโs username |
password |
String | Required. The userโs password |
Example:
zlFetch.post(`${rootendpoint}/users`, { body: { username: 'transporterduo', password: '12345678', },})Response:
{ "username": "transporterduo", "id": "5cc90a78f267395a2811145e"}Get user
Use this to check if your username exists. If it exists, the endpoint will respond with a username and an id
GET /users/:usernameExample:
zlFetch(`${rootendpoint}/users/transporterduo`)Response:
{ "username": "transporterduo", "id": "5cc90a78f267395a2811145e"}Delete user
Deletes the user and the tasks theyโve created. Returns the username and id of the deleted user if successful.
DELETE /users/:usernameResponse:
{ "username": "transporterduo", "id": "5cc90a78f267395a2811145e"}Tasks
This is where youโll spend most of your time on. There are endpoints for:
- Getting tasks
- Creating a task
- Editing a task
- Deleting a task
Get tasks
Gets a list of tasks the user has created. Requires authentication. If successful, responds with an array of tasks that contains the following properties:
id: id of the tasktask: the taskdone: whether the task is completed
GET /tasksExample:
zlFetch(`${rootendpoint}/tasks`, { auth: { username: 'transporterduo', password: '12345678', },})Response:
[ { "id": "5b8f7b2713b6e114ab5e5645", "task": "Learn JavaScript for 30 minutes", "done": "false" }]Create a task
Creates a task. Requires authentication. Responds with the created task if successful.
POST /tasksInput:
| Field | Type | Description |
|---|---|---|
name |
String | Required. The task |
done |
Boolean | Whether the task completed |
Example:
zlFetch.post(`${rootendpoint}/tasks`, { auth: { username: 'transporterduo', password: '12345678', }, body: { name: 'Learn JavaScript for 30 minutes', },})Response:
{ "id": "5b8f7b2713b6e114ab5e5645", "name": "Learn JavaScript for 30 minutes", "done": "false"}Update a task
Updates the specified task. Requires authentication. Responds with the edited task if successful:
PUT /tasks/:taskidInput:
| Field | Type | Description |
|---|---|---|
name |
String | The task |
done |
Boolean | Whether the task completed |
Note: Both name and done are optional. If you are editing a task, make sure to change one of them. Otherwise, whatโs the point in making this request?
Example:
zlFetch.put(`${rootendpoint}/tasks/5b8f7b2713b6e114ab5e5645`, { auth: { username: 'transporterduo', password: '12345678', }, body: { done: true, },})Response:
{ "id": "5b8f7b2713b6e114ab5e5645", "name": "Learn JavaScript for 30 minutes", "done": "true"}Delete a task
Deletes a specified task. Requires authentication. Responds with the deleted task if successful.
DELETE /tasks/:taskidExample:
zlFetch.delete(`${rootendpoint}/tasks/5b8f7b2713b6e114ab5e5645`, { auth: { username: 'transporterduo', password: '12345678', },})Response:
{ "id": "5b8f7b2713b6e114ab5e5645", "name": "Learn JavaScript for 30 minutes", "done": "true"}Challenge yourself
Use this API to create the Todolist yourself.
Move on to the next lesson whenever youโre ready.
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson