Skip to content

Hunderegasa/API-with-HTML

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

API-with-HTML

Build a Website using an API with HTML, JavaScript, and JSON

o build a weather report website using an API, HTML, JavaScript, and JSON, we will integrate an external weather API, such as OpenWeatherMap, which provides weather data in JSON format. Below is a step-by-step guide with the complete source code for building a weather report website. Steps:

Get API Key: You need an API key from OpenWeatherMap to access the weather data. Sign up at OpenWeatherMap and get your free API key.

Create HTML Structure: This will include fields to enter the city name, a button to fetch the weather, and a section to display the results.

Fetch Weather Data using JavaScript: Use JavaScript to fetch weather data from the OpenWeatherMap API and display it in the HTML.

Handle JSON Data: Parse the returned JSON data and dynamically update the HTML to show the weather report.

Explanation of the Code HTML File (index.html)

Input Field:
    <input type="text" id="city" class="input-field" placeholder="Enter city name" /> allows the user to enter the city for which they want the weather.
Button:
    <button class="btn" id="getWeather">Get Weather</button> triggers the JavaScript function to fetch the weather data.
Weather Info Section:
    <div class="weather-info" id="weatherInfo"> is where the weather details will be displayed after fetching data.

JavaScript File (script.js)

API Key: Replace 'your_api_key_here' with your actual OpenWeatherMap API key.
fetchWeather(city):
    This function uses the fetch API to make a request to the OpenWeatherMap API.
    It appends the city name and API key to the URL, with &units=metric to get temperature in Celsius.
    The function checks if the response is successful (status code 200).
displayWeather(data):
    This function extracts relevant weather information (e.g., temperature, humidity, wind speed) from the JSON data and dynamically updates the webpage.

Fetch API

fetch(url): This sends an HTTP request to the OpenWeatherMap API with the query parameters: city name, API key, and temperature unit.
response.json(): This parses the JSON data returned from the API.
Error Handling: If the city is not found, or if there is an error fetching the data, an alert is shown.

Styling (CSS)

The design uses simple styles to center the content, make input fields look clean, and add a button hover effect for better user experience.

How to Run the Weather Report Website:

Sign up for an OpenWeatherMap API Key:
    Go to OpenWeatherMap and sign up to get your free API key.
Replace the API Key:
    In the script.js file, replace 'your_api_key_here' with your actual API key.
Save Files:
    Save the HTML code as index.html and the JavaScript code as script.js in the same directory.
Open the Website:
    Open index.html in your browser.

Expected Output:

When you open the website and enter a city name, you'll get a report like this:

Weather in London Condition: clear sky Temperature: 15°C Humidity: 63% Wind Speed: 4.6 m/s Notes:

API Key Security: In production, it’s recommended to keep your API key secure, typically on the backend, to avoid exposing it publicly in your frontend code.
Error Handling: This basic implementation checks for missing or incorrect city names but could be expanded to handle other types of errors (e.g., network issues).

Steps Taken to Develop the Weather Report Website

The development of the weather report website using HTML, JavaScript, and an API involved a series of logical steps, from setting up the basic structure to integrating external data through an API. Below is a breakdown of the approach taken to complete the project:

  1. Project Planning and Requirements Gathering

    Goal: The goal was to create a simple weather report website where users can input a city name and receive real-time weather data. Key Requirements: A user-friendly interface to input a city name. Fetch real-time weather data for the entered city. Display relevant weather details, such as temperature, weather condition, humidity, and wind speed. Use a weather API to get live data. Display error messages in case of invalid input or API failure.

  2. Choosing the Weather API

    Research on APIs: After researching several weather APIs, I selected OpenWeatherMap for this project. It offers a free tier with basic weather data and is widely used and well-documented. Obtaining an API Key: I signed up for an account on OpenWeatherMap and generated an API key, which would be used to authenticate API requests. OpenWeatherMap's free API offers JSON-formatted data, which is easy to integrate into the website.

  3. Setting Up the Basic HTML Structure

    Creating the HTML Layout: I started by creating a basic structure for the website with the following: Input Field: An input field where the user can type in the city name they want the weather for. Button: A button labeled "Get Weather" that the user clicks to fetch the weather information. Weather Info Display Section: A section where the weather details will be shown after the data is fetched. Styling: Basic CSS was applied to create a simple and clean layout. The goal was to make the interface user-friendly by using centered content, simple input fields, and a clear call-to-action button.

  4. Writing JavaScript for API Integration

    Setting Up Variables: I declared the necessary variables, including the API URL and API key, and defined DOM elements that would be used in the JavaScript code (e.g., the input field, button, and weather display section). API Request: I used the fetch API in JavaScript to make HTTP requests to OpenWeatherMap. The request URL was constructed with the city name entered by the user, the API key, and a query for metric units (Celsius temperature). Handling User Input: I added an event listener to the "Get Weather" button. When the user clicks the button, the function reads the value from the input field (the city name) and triggers the fetch process. Fetching and Handling the Data: Using the async/await syntax, I fetched the weather data asynchronously. I used response.json() to parse the response into a JavaScript object, making it easier to access the data. Displaying the Weather: I extracted the necessary information (city name, weather condition, temperature, humidity, and wind speed) from the JSON response. This data was then dynamically inserted into the HTML using innerHTML, updating the display section with the weather details. Error Handling: I included error handling to manage scenarios like invalid city names (e.g., showing an alert if the city isn’t found). I also added error catching for failed API requests, displaying a user-friendly message if the fetch request fails.

  5. Styling and User Interface Improvements

    Responsive Layout: To ensure the website is mobile-friendly, I used simple responsive design techniques. For example, the input field and button widths were set to a percentage, making them flexible across different screen sizes. User Experience Enhancements: I added hover effects for the button to provide feedback when the user interacts with it. The weather report section is updated with clear text formatting to make the displayed information easy to read.

  6. Testing and Debugging

    Testing API Integration: I tested the API call with various city names (both valid and invalid) to make sure that the API returns correct results and handles errors gracefully. Testing Edge Cases: I tested scenarios such as: Entering an empty city name (prompting the user to input a valid city). Entering a city name that doesn’t exist (displaying an error message). Verifying that the weather data is correctly formatted and displayed. Cross-browser Testing: I checked the website on multiple browsers (Chrome, Firefox, Safari) to ensure consistent behavior and appearance.

  7. Final Adjustments and Deployment

    Code Cleanup: I reviewed the code to ensure it was clean, well-commented, and easy to follow. Deployment: For this project, I hosted the website locally for development purposes, but I could deploy it to a platform like GitHub Pages or Netlify for public access. These platforms allow easy deployment of static websites (HTML, CSS, and JavaScript). I tested the final version of the site after deployment to ensure everything worked as expected.

  8. Future Improvements and Enhancements

    Additional Weather Information: I could add more detailed weather data, such as pressure, sunrise/sunset times, and forecasts for the next few days. Geolocation Integration: Integrating the browser’s geolocation feature could allow users to automatically get weather data for their current location. Visual Enhancements: To improve the user experience, I could add icons representing different weather conditions (e.g., sunny, rainy, cloudy) by using weather icon data from the API. Error Handling Improvements: I could improve error handling by displaying more user-friendly messages when there are network issues or when the user enters an incorrect city name.

Conclusion

The project followed a systematic approach, starting from planning the basic requirements, selecting an API, creating the user interface with HTML, integrating the OpenWeatherMap API using JavaScript, and finally testing and deploying the application. The result is a functional weather report website that provides real-time weather information based on user input.

The process included iterative testing, troubleshooting, and refining the user experience. The project was a great exercise in both front-end web development and working with APIs.

About

Build a Website using an API with HTML, JavaScript, and JSON

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published