Getting started
Introduction
WeatherAPI.com provides access to weather and geo data via a JSON/XML restful API. It allows developers to create desktop, web and mobile applications using this data very easy.
We provide following data through our API:
Getting Started
You need to signup and then you can find your API key under your account, and start using API right away!
If you find any features missing or have any suggestions, please contact us.
Authentication
API access to the data is protected by an API key. If at anytime, you find the API key has become vulnerable, please regenerate the key using Regenerate button next to the API key.
Authentication to the WeatherAPI.com API is provided by passing your API key as request parameter through an API .
key parameter
key=YOUR_API_KEY
How to Build
You must have Python 2 >=2.7.9
or Python 3 >=3.4
installed on your system to install and run this SDK. This SDK package depends on other Python packages like nose, jsonpickle etc.
These dependencies are defined in the requirements.txt
file that comes with the SDK.
To resolve these dependencies, you can use the PIP Dependency manager. Install it by following steps at https://pip.pypa.io/en/stable/installing/.
Python and PIP executables should be defined in your PATH. Open command prompt and type pip --version
.
This should display the version of the PIP Dependency Manager installed if your installation was successful and the paths are properly defined.
- Using command line, navigate to the directory containing the generated files (including
requirements.txt
) for the SDK. - Run the command
pip install -r requirements.txt
. This should install all the required dependencies.
How to Use
The following section explains how to use the Weatherapi SDK package in a new project.
1. Open Project in an IDE
Open up a Python IDE like PyCharm. The basic workflow presented here is also applicable if you prefer using a different editor or IDE.
Click on Open
in PyCharm to browse to your generated SDK directory and then click OK
.
The project files will be displayed in the side bar as follows:
2. Add a new Test Project
Create a new directory by right clicking on the solution name as shown below:
Name the directory as "test"
Add a python file to this project with the name "testsdk"
Name it "testsdk"
In your python file you will be required to import the generated python library using the following code lines
from weatherapi.weatherapi_client import WeatherapiClient
After this you can write code to instantiate an API client object, get a controller object and make API calls. Sample code is given in the subsequent sections.
3. Run the Test Project
To run the file within your test project, right click on your Python file inside your Test project and click on Run
How to Test
You can test the generated SDK and the server with automatically generated test
cases. unittest is used as the testing framework and nose is used as the test
runner. You can run the tests as follows:
- From terminal/cmd navigate to the root directory of the SDK.
- Invoke
pip install -r test-requirements.txt
- Invoke
nosetests
Initialization
Authentication
In order to setup authentication and initialization of the API client, you need the following information.
Parameter | Description |
---|
key | TODO: add a description |
API client can be initialized as following.
key = 'key'
client = WeatherapiClient(key)
Class Reference
List of Controllers
APIsController
Get controller instance
An instance of the APIsController
class can be accessed from the API Client.
ap_is_controller = client.ap_is
get_realtime_weather
Current weather or realtime weather API method allows a user to get up to date current weather information in json and xml. The data is returned as a Current Object.Current object contains current or realtime weather information for a given city.
def get_realtime_weather(self,
q,
lang=None)
Parameters
Parameter | Tags | Description |
---|
q | Required | Pass US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/Longitude (decimal degree) or city name. Visit request parameter section to learn more. |
lang | Optional | Returns 'condition:text' field in API in the desired language. Visit request parameter section to check 'lang-code'. |
Example Usage
q = 'q'
lang = 'lang'
result = ap_is_controller.get_realtime_weather(q, lang)
Errors
Error Code | Error Description |
---|
400 | Error code 1003: Parameter 'q' not provided.Error code 1005: API request url is invalid.Error code 1006: No location found matching parameter 'q'Error code 9999: Internal application error. |
401 | Error code 1002: API key not provided.Error code 2006: API key provided is invalid |
403 | Error code 2007: API key has exceeded calls per month quota. Error code 2008: API key has been disabled. |
get_forecast_weather
Forecast weather API method returns upto next 10 day weather forecast and weather alert as json. The data is returned as a Forecast Object.
Forecast object contains astronomy data, day weather forecast and hourly interval weather information for a given city.
def get_forecast_weather(self,
q,
days,
dt=None,
unixdt=None,
hour=None,
lang=None)
Parameters
Parameter | Tags | Description |
---|
q | Required | Pass US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/Longitude (decimal degree) or city name. Visit request parameter section to learn more. |
days | Required | Number of days of weather forecast. Value ranges from 1 to 10 |
dt | Optional | Date should be between today and next 10 day in yyyy-MM-dd format |
unixdt | Optional | Please either pass 'dt' or 'unixdt' and not both in same request. unixdt should be between today and next 10 day in Unix format |
hour | Optional | Must be in 24 hour. For example 5 pm should be hour=17, 6 am as hour=6 |
lang | Optional | Returns 'condition:text' field in API in the desired language. Visit request parameter section to check 'lang-code'. |
Example Usage
q = 'q'
days = 30
dt = datetime.now()
unixdt = 30
hour = 30
lang = 'lang'
result = ap_is_controller.get_forecast_weather(q, days, dt, unixdt, hour, lang)
Errors
Error Code | Error Description |
---|
400 | Error code 1003: Parameter 'q' not provided.Error code 1005: API request url is invalid.Error code 1006: No location found matching parameter 'q'Error code 9999: Internal application error. |
401 | Error code 1002: API key not provided.Error code 2006: API key provided is invalid |
403 | Error code 2007: API key has exceeded calls per month quota. Error code 2008: API key has been disabled. |
get_history_weather
History weather API method returns historical weather for a date on or after 1st Jan, 2015 as json. The data is returned as a Forecast Object.
def get_history_weather(self,
q,
dt,
unixdt=None,
end_dt=None,
unixend_dt=None,
hour=None,
lang=None)
Parameters
Parameter | Tags | Description |
---|
q | Required | Pass US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/Longitude (decimal degree) or city name. Visit request parameter section to learn more. |
dt | Required | Date on or after 1st Jan, 2015 in yyyy-MM-dd format |
unixdt | Optional | Please either pass 'dt' or 'unixdt' and not both in same request. unixdt should be on or after 1st Jan, 2015 in Unix format |
endDt | Optional | Date on or after 1st Jan, 2015 in yyyy-MM-dd format'end_dt' should be greater than 'dt' parameter and difference should not be more than 30 days between the two dates. |
unixendDt | Optional | Date on or after 1st Jan, 2015 in Unix Timestamp format unixend_dt has same restriction as 'end_dt' parameter. Please either pass 'end_dt' or 'unixend_dt' and not both in same request. e.g.: unixend_dt=1490227200 |
hour | Optional | Must be in 24 hour. For example 5 pm should be hour=17, 6 am as hour=6 |
lang | Optional | Returns 'condition:text' field in API in the desired language. Visit request parameter section to check 'lang-code'. |
Example Usage
q = 'q'
dt = datetime.now()
unixdt = 30
end_dt = datetime.now()
unixend_dt = 30
hour = 30
lang = 'lang'
result = ap_is_controller.get_history_weather(q, dt, unixdt, end_dt, unixend_dt, hour, lang)
Errors
Error Code | Error Description |
---|
400 | Error code 1003: Parameter 'q' not provided.Error code 1005: API request url is invalid.Error code 1006: No location found matching parameter 'q'Error code 9999: Internal application error. |
401 | Error code 1002: API key not provided.Error code 2006: API key provided is invalid |
403 | Error code 2007: API key has exceeded calls per month quota. Error code 2008: API key has been disabled. |
search_autocomplete_weather
WeatherAPI.com Search or Autocomplete API returns matching cities and towns as an array of Location object.
def search_autocomplete_weather(self,
q)
Parameters
Parameter | Tags | Description |
---|
q | Required | Pass US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/Longitude (decimal degree) or city name. Visit request parameter section to learn more. |
Example Usage
q = 'q'
result = ap_is_controller.search_autocomplete_weather(q)
Errors
Error Code | Error Description |
---|
400 | Error code 1003: Parameter 'q' not provided.Error code 1005: API request url is invalid.Error code 1006: No location found matching parameter 'q'Error code 9999: Internal application error. |
401 | Error code 1002: API key not provided.Error code 2006: API key provided is invalid |
403 | Error code 2007: API key has exceeded calls per month quota. Error code 2008: API key has been disabled. |
get_ip_lookup
IP Lookup API method allows a user to get up to date information for an IP address.
def get_ip_lookup(self,
q)
Parameters
Parameter | Tags | Description |
---|
q | Required | Pass IP address. |
Example Usage
q = 'q'
result = ap_is_controller.get_ip_lookup(q)
Errors
Error Code | Error Description |
---|
400 | Error code 1003: Parameter 'q' not provided.Error code 1005: API request url is invalid.Error code 1006: No location found matching parameter 'q'Error code 9999: Internal application error. |
401 | Error code 1002: API key not provided.Error code 2006: API key provided is invalid |
403 | Error code 2007: API key has exceeded calls per month quota. Error code 2008: API key has been disabled. |
get_time_zone
Return Location Object
def get_time_zone(self,
q)
Parameters
Parameter | Tags | Description |
---|
q | Required | Pass US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/Longitude (decimal degree) or city name. Visit request parameter section to learn more. |
Example Usage
q = 'q'
result = ap_is_controller.get_time_zone(q)
Errors
Error Code | Error Description |
---|
400 | Error code 1003: Parameter 'q' not provided.Error code 1005: API request url is invalid.Error code 1006: No location found matching parameter 'q'Error code 9999: Internal application error. |
401 | Error code 1002: API key not provided.Error code 2006: API key provided is invalid |
403 | Error code 2007: API key has exceeded calls per month quota. Error code 2008: API key has been disabled. |
get_astronomy
Return Location and Astronomy Object
def get_astronomy(self,
q,
dt)
Parameters
Parameter | Tags | Description |
---|
q | Required | Pass US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/Longitude (decimal degree) or city name. Visit request parameter section to learn more. |
dt | Required | Date on or after 1st Jan, 2015 in yyyy-MM-dd format |
Example Usage
q = 'q'
dt = datetime.now()
result = ap_is_controller.get_astronomy(q, dt)
Errors
Error Code | Error Description |
---|
400 | Error code 1003: Parameter 'q' not provided.Error code 1005: API request url is invalid.Error code 1006: No location found matching parameter 'q'Error code 9999: Internal application error. |
401 | Error code 1002: API key not provided.Error code 2006: API key provided is invalid |
403 | Error code 2007: API key has exceeded calls per month quota. Error code 2008: API key has been disabled. |
Back to List of Controllers