API in JS
API in JS
API Basics in
JavaScript
Quick & Easy Guide for Beginners!
{ API }
Js
Slim toumi
Slim toumi
JavaScript tips
What is an API ?
An API (Application Programming Interface) allows applications to talk
to each other. For example, your JavaScript application can use an API
to fetch data from a server or a database and display it on a website.
Slim toumi
Slim toumi
JavaScript tips
Explanation :
fetch('URL'): This makes a request to the URL.
.then(response => response.json()): Converts the response to JSON format so
we can easily use it.
.then(data => console.log(data)): Logs the data in the console to check it.
.catch(error => console.error('Error:', error)): Catches any errors (e.g., if the API is
down).
Slim toumi
Slim toumi
JavaScript tips
Explanation :
method: 'POST': Specifies we are sending data to the server.
headers: Tells the server what type of data to expect.
body: This is the data we’re sending, converted to a JSON string using
JSON.stringify().
Slim toumi
Slim toumi
JavaScript tips
Explanation :
method: 'PUT': Specifies an update request.
headers: Tells the server what type of data to expect.
body: Contains the new data you want to update, converted to a JSON string.
id: In https://api.example.com/data/1, the number 1 represents the unique ID of
the specific item you want to update. The server uses this ID to identify exactly
which item in its database needs to be modified.
Slim toumi
Slim toumi
JavaScript tips
Explanation :
method: 'DELETE': Specifies a delete request to remove data..
id: In “ https://api.example.com/data/1 “ , the number 1 represents the unique ID
of the specific item you want to delete. The server uses this ID to locate and
remove the correct item from its records, ensuring only the specified data is
deleted.
Slim toumi
Slim toumi
JavaScript tips
Explanation :
query=${searchQuery}: Here, query is the key, and ${searchQuery} is the value
(in this case, "javascript"). Adding ?query=javascript to the URL tells the server
that we want to search specifically for "javascript".
Query Parameters: These are key-value pairs added to the URL after a ?. They let
you send specific details to the server, like search terms or filters, to refine the
results. For example, ?query=javascript tells the server to filter results based on
"javascript".
Slim toumi
Slim toumi
JavaScript tips
Error Handling
When making requests, things can go wrong, such as network issues or
server errors. You can handle these potential issues by checking the
response status and using .catch() to log any errors.
Explanation :
if (!response.ok): Checks if the server’s response status is not OK (e.g., 404 or
500).
throw new Error: Creates a new error if the response isn’t OK, stopping further
execution.
.catch(error => ...): Catches and logs any errors from the fetch request, making
them easy to troubleshoot.
Slim toumi
Slim toumi
JavaScript tips
Explanation :
async: Marks the function as asynchronous, allowing you to use await inside.
await: Pauses execution until the fetch or JSON conversion is complete.
try and catch: Catches and handles any errors, ensuring they don’t break the
app.
Slim toumi
Slim toumi
JavaScript tips