Skip to content

Instantly share code, notes, and snippets.

@mike-weiner
Last active September 16, 2023 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-weiner/1f6d9dc424b2ad684e56b3af6866e82f to your computer and use it in GitHub Desktop.
Save mike-weiner/1f6d9dc424b2ad684e56b3af6866e82f to your computer and use it in GitHub Desktop.
A Python script to serve as a template for WeatherLink API calls using the newer authentication method.
@mike-weiner
Copy link
Author

Using the above template, this is how you could get the current weather conditions for a station.

import json
import requests

# API v2 Base URL
BASE_URL = "https://api.weatherlink.com/v2/"

# Your WeatherLink Account API Credentials
API_KEY = "<your-api-key>"
API_SECRET = "<your-api-secret>"

# API v2 Endpoint URL
# https://weatherlink.github.io/v2-api/api-reference
endpoint = "current"

# API Path Parameters
# Add the necessary _path_ parameters necessary for the API endpoint that you are querying
pathParameters = [
  <your-station-id>
]
  
# API Query String Parameters
# Add the necessary _query string_ parameters necessary for the API endpoint that you are querying
queryParameters = [
]

# Create final API URL
api_url = BASE_URL + endpoint 
api_url += ''.join("/" + str(param) for param in pathParameters)[0:] 
api_url += "?api-key=" + API_KEY 
api_url += ''.join("&" + str(x) + "=" + str(y) for (x,y) in queryParameters)[0:]


# Make call to API and pretty-print returned data
api_results = requests.get(
  headers= {
    "X-Api-Secret": API_SECRET
  },
  url=api_url, 
  verify=True,
)
print(json.dumps(json.loads(api_results.text), indent=4))

@mike-weiner
Copy link
Author

Using the above template, this is how you could get a historic weather conditions record for a station.

import json
import requests

# API v2 Base URL
BASE_URL = "https://api.weatherlink.com/v2/"

# Your WeatherLink Account API Credentials
API_KEY = "<your-api-key>"
API_SECRET = "<your-api-secret>"

# API v2 Endpoint URL
# https://weatherlink.github.io/v2-api/api-reference
endpoint = "historic"

# API Path Parameters
# Add the necessary _path_ parameters necessary for the API endpoint that you are querying
pathParameters = [
  <your-station-id>
]
  
# API Query String Parameters
# Add the necessary _query string_ parameters necessary for the API endpoint that you are querying
queryParameters = [
  ("start-timestamp", str(int(time.time())-10000)),
  ("end-timestamp", str(int(time.time())))
]

# Create final API URL
api_url = BASE_URL + endpoint 
api_url += ''.join("/" + str(param) for param in pathParameters)[0:] 
api_url += "?api-key=" + API_KEY 
api_url += ''.join("&" + str(x) + "=" + str(y) for (x,y) in queryParameters)[0:]


# Make call to API and pretty-print returned data
api_results = requests.get(
  headers= {
    "X-Api-Secret": API_SECRET
  },
  url=api_url, 
  verify=True,
)
print(json.dumps(json.loads(api_results.text), indent=4))

@mike-weiner
Copy link
Author

If you are looking for more documentation, there are some great docs at https://weatherlink.github.io/v2-api/tutorial!

@mike-weiner
Copy link
Author

The "older" signature authentication method is still supported. A template script for that authentication method is found here: https://gist.github.com/mike-weiner/3ea4654ccfec57234c0520e773f78225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment