Code Samples


Some code examples showing how to use the Solar Forecast webservice from some common programming languages. It should be straight-forward to adapt these examples to other programming languages.

Note that the Python examples use the popular Requests package, which is not part of the standard library but can usually be installed without any problems (type pip install requests in a shell window). The PHP examples are pure PHP, which makes them look a bit cumbersome. Check the Guzzle library for a more comfortable way of doing HTTP requests.

Adding a site

Python

import requests
mdx_key = 'AAABBBCCCDDDEEEFFFGGGHHH11122233'
# replace with your KEY!
mdx_url = ('https://mdx.meteotest.ch/api_v1?key={key}&service=solarforecast'
           '&action=siteadd&latitude=47.2&longitude=7.2&inclination=30'
           '&azimuth=180&format=json'.format(key=mdx_key))

r = requests.get(mdx_url)
result = r.json()
# if it worked, variable 'result' contains details of
# created site. Otherwise 'result' contains an
# error message.

if r.status_code == 200:
  print("It worked!")
else:
  print("An error occurred:")
  # let's check what the problem is...
  print(result)
                

Reading forecast data

Python

import requests
mdx_key = 'AAABBBCCCDDDEEEFFFGGGHHH11122233'
# replace with your KEY!
mdx_url = ('https://mdx.meteotest.ch/api_v1?key={key}&service=solarforecast'
           '&action=getforecast&format=json'.format(key=mdx_key))

r = requests.get(mdx_url)
result = r.json()

if r.status_code == 200:
  print("forecast data:")
  print(result)
else:
  print("An error occurred:")
  # let's check what the problem is...
  print(result)