Let’s say we’re receiving real-time data from an external analytics provider via their REST API. Keeping track of this data is something we want to do with our dashboard.

We can integrate against REST APIs using Anvil’s http module.

To make a GET request, we run:

anvil.http.request("https://your-url-here")

Alternatively, we could use the standard python requests library from our Server Module, like so:

import requests

requests.get("https://your-url-here")

Note: A paid plan is required to use the requests module (here’s why)

For illustrative purposes, we’ll get our data from the Dark Sky weather API. This gives real-time weather data in JSON format.

First, let’s import Anvil’s http module. Add this line to the top of your Server Module:

import anvil.http

Add this function to your Server Module. It takes in latitude and longitude parameters and returns data from the API:

@anvil.server.callable
def get_weather_data(latitude, longitude):
  resp = anvil.http.request(
    "https://api.darksky.net/forecast/a2666092e36a5888c01e9f3b829fc332/%s,%s" % (latitude,longitude),
    json=True
    )
  # convert timestamp to datetime object
  time = datetime.fromtimestamp(resp['currently']['time'])
  # return time and temperature data
  return {'time':time, 'temp':resp['currently']['temperature']}

The DarkSky API key that we use for the tutorial has a limit on API requests per month. If you see a 403 'daily usage limit exceeded' error, this means our quota has been exceeded.

An alternative suggestion from the community is to use the Open Weather Map API