Image by creativeart on Freepik
Have you ever wanted to build your own weather app but thought it would take hours of coding and complex tools? What if I told you that you can create a fully functional weather app in just 10 lines of code? With modern APIs and free online tools, this is not only possible but also incredibly easy, even for beginners. In this article, we’ll walk through the process step-by-step, provide examples, and link to free resources you can use to get started right away. Whether you’re a coding newbie or a seasoned developer looking for a quick project, this guide is for you.
Weather apps are a fantastic way to dip your toes into app development. They combine real-world data fetching, simple user interfaces, and practical utility—all while keeping the project lightweight. Plus, who doesn’t love checking the forecast with a tool they built themselves? By the end of this tutorial, you’ll have a basic weather app that fetches live data and displays it on a webpage—all in just 10 lines of code.
Before we dive into the code, let’s gather our free tools:
To keep this project under 10 lines, we’ll write everything in a single HTML file with embedded JavaScript. You can use an online editor like CodePen to avoid setting up local files. Here’s the plan:
Here’s the complete code to build a weather app that fetches the current temperature for a city (e.g., London):
<!DOCTYPE html>
<html>
<body>
<h2>Weather App</h2>
<p id="weather">Loading...</p>
<script>
fetch('https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY&units=metric')
.then(response => response.json())
.then(data => document.getElementById('weather').innerText = `${data.main.temp}°C in ${data.name}`);
</script>
</body>
</html>
Replace YOUR_API_KEY with the key you get from OpenWeatherMap. That’s it—10 lines, including HTML and JavaScript!
Let’s explore how this works:
Copy the code into CodePen, replace the API key, and hit "Run." You’ll see something like "15°C in London" (depending on the current weather). If it says "Loading...," double-check your API key or internet connection.
Want to let users enter their own city? Here’s a slightly expanded version (still beginner-friendly):
<!DOCTYPE html>
<html>
<body>
<input id="city" placeholder="Enter city" />
<button onclick="getWeather()">Get Weather</button>
<p id="weather">Result here</p>
<script>
function getWeather() {
let city = document.getElementById('city').value;
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=YOUR_API_KEY&units=metric`)
.then(res => res.json())
.then(data => document.getElementById('weather').innerText = `${data.main.temp}°C in ${data.name}`);
}
</script>
</body>
</html>
This version uses an input field and button, but we’ve kept the core logic concise. Try typing "Paris" or "New York" and clicking the button!
Building a weather app in 10 lines teaches you key coding concepts: APIs, asynchronous data fetching, and DOM manipulation. It’s a stepping stone to bigger projects like to-do lists or even mobile apps. Plus, it’s a cool party trick to say, “I built this in under 5 minutes!”
With just 10 lines of code and free tools like OpenWeatherMap and CodePen, you’ve created a working weather app. It’s simple, functional, and a great starting point for your coding journey. Try tweaking it—add icons, style it with CSS, or fetch forecasts for multiple days. The possibilities are endless, and the skills you’ve gained are invaluable. So, what’s the weather like in your city today? Fire up your app and find out!
Happy coding!