Search code examples
apiopenweathermap

How to find 7 days weather forecast in openweather API?


I am trying to find 7 days weather forecast using openweather API , but when I am calling the openweather API then the data are not coming for the current location, can anyone guide me to find the correct API to get weather forecast for the current location.

API's used so far

1. api.openweathermap.org/data/2.5/forecast?q={city name}&appid={API key}
2. https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude={part}&appid={API key}

Reference links:

  1. https://openweathermap.org/forecast5
  2. https://openweathermap.org/api/one-call-api

Request: https://api.openweathermap.org/data/2.5/forecast?q=India&appid={MY_API_ID} Response: (Added only 2 days data out of 40)

{
    "cod": "200",
    "message": 0,
    "cnt": 40,
    "list": [
        {
            "dt": 1600668000,
            "main": {
                "temp": 283.42,
                "feels_like": 282.81,
                "temp_min": 283.42,
                "temp_max": 284.26,
                "pressure": 1018,
                "sea_level": 1018,
                "grnd_level": 862,
                "humidity": 84,
                "temp_kf": -0.84
            },
            "weather": [
                {
                    "id": 802,
                    "main": "Clouds",
                    "description": "scattered clouds",
                    "icon": "03d"
                }
            ],
            "clouds": {
                "all": 32
            },
            "wind": {
                "speed": 0.1,
                "deg": 22
            },
            "visibility": 10000,
            "pop": 0,
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2020-09-21 06:00:00"
        },
        {
            "dt": 1600722000,
            "main": {
                "temp": 283.34,
                "feels_like": 282.29,
                "temp_min": 283.34,
                "temp_max": 283.34,
                "pressure": 1016,
                "sea_level": 1016,
                "grnd_level": 861,
                "humidity": 86,
                "temp_kf": 0
            },
            "weather": [
                {
                    "id": 500,
                    "main": "Rain",
                    "description": "light rain",
                    "icon": "10n"
                }
            ],
            "clouds": {
                "all": 66
            },
            "wind": {
                "speed": 0.82,
                "deg": 149
            },
            "visibility": 823,
            "pop": 0.35,
            "rain": {
                "3h": 0.18
            },
            "sys": {
                "pod": "n"
            },
            "dt_txt": "2020-09-21 21:00:00"
        }
    ],
    "city": {
        "id": 3168508,
        "name": "Innichen",
        "coord": {
            "lat": 46.7406,
            "lon": 12.2797
        },
        "country": "IT",
        "population": 3107,
        "timezone": 7200,
        "sunrise": 1600664205,
        "sunset": 1600708262
    }
}

Solution

  • You have a lot of issues and it would be helpful if you could narrow them down. But here's my best shot at helping in every way that I can:

    For a 7 day forecast, you need to request a 7 day forecast..

    In your example for a request 1. api.openweathermap.org/data/2.5/forecast?q={city name}&appid={API key} you also included a link that you used which is for a 5 day request. That's a problem if you want 7 days.

    If you want a 5 day forecast, the API you cited in your first example requires a city name and (if you want to include a country) it requires that the country name be in ISO 3166 so India would be "IND" not INDIA.

    Whether you want a 5 day or a 7 day, you will need to be more specific than INDIA.

    In the case of a 5 day, you will need a city name. For a 5 day forecast for Mumbai, you can use the API you tried in the first example like this:

      "https://api.openweathermap.org/data/2.5/forecast?q=mumbai&appid={yourAPIKey}"
    

    In the case of a 7 day, you will need latitude and longitude coordinates.
    For a 7 day forecast for Mumbai, you can use the API you tried in your second example like this:

       //includes lat and long for mumbai
     "https://api.openweathermap.org/data/2.5/onecall?lat=19.0760&lon=72.8777&appid={yourAPIkey}"
    
         
    

    I hope this answers your question.