Learn to Use APIs
Guide: How to Learn to Use an API with fetch: A Simple PlanA small course in 8 short lessons on using web APIs from JavaScript. You learn what a request and a response are, how to send a request with fetch, how to read the JSON of the answer, how async and await work, how to add parameters to an address, why you should check the status of a response, how to send data with POST and how to handle errors. The address api.example.com in the lessons is made up, no request is sent and your code is not run: the course checks that your answer contains what the task asks for. A typing mistake in your code is not detected. Your code stays in your browser and your progress is remembered there.
This course does not send requests. The address api.example.com is made up, and your code is not run: the course checks that it contains what the task asks for. A typing mistake in your code is not detected, so try your code in a real project too.
0 of 8 lessons done. Your progress is saved in this browser.
1. What an API is: a request and a response
An API is a way for one program to ask another program for data. Your code sends a **request** to an address (a URL) and gets a **response** back. Often the response is JSON. The address has a base, such as https://api.example.com, and a path, such as /users. The address api.example.com in this course is made up: nothing is sent anywhere.
Your task: Store the address https://api.example.com/users in a constant called url.
What you type stays in this browser. It is not saved or sent anywhere.
Features
- 8 short lessons, from a first request to error handling
- Check my answer, a hint and the model answer for every lesson
- Made-up address: nothing is sent anywhere
- Your progress is remembered in this browser
How to use Learn to Use APIs
- Read the lesson and the task.
- Write your code in the editor.
- Press Check my answer. Use the hint or the answer if you are stuck.
- Go to the next lesson.
Frequently asked questions
Does this course send real requests?
No. The address api.example.com is made up and your code is not run. The course uses patterns to check that your code contains what the task asks for. A typing mistake is not detected, so try your code in a real project or in the console of your browser too.
Do I need to know JavaScript first?
It helps. The lessons use plain JavaScript, such as constants, functions and if. If that is new, do our Learn JavaScript course first. The Learn JSON course explains the data format that most APIs use.
Why does fetch not fail on a 404 or a 500?
A fetch promise only fails when the network fails. A response with a status such as 404 or 500 still arrives as a normal response, so you check response.ok yourself, which is true for the status codes 200 to 299.
Is my code saved?
Only which lessons you finished is saved, in this browser. The code you type is not stored or sent anywhere.