Code samples

Every example below fetches a forecast for London and prints tomorrow's high. Swap in your own key from the dashboard - the free plan gives you 100,000 calls a month. Full endpoint reference lives in the API documentation.

curl

# current conditions + 7-day outlook
curl "https://api.weathermetro.com/v2/forecast\
?lat=51.5&lng=-0.13&daily&current&api_key=YOUR_KEY"

JavaScript (fetch)

const KEY = process.env.WEATHERMETRO_KEY;

const res = await fetch(
  `https://api.weathermetro.com/v2/forecast?lat=51.5&lng=-0.13&daily&api_key=${KEY}`
);
const { meta, response } = await res.json();
if (meta.error_type) throw new Error(meta.error_detail);

const daily = response.data.daily;
console.log(daily.time[1], daily.temperature_2m_max[1] + '°C');

Python

import os, requests

r = requests.get(
    "https://api.weathermetro.com/v2/forecast",
    params={"lat": 51.5, "lng": -0.13, "daily": "",
            "api_key": os.environ["WEATHERMETRO_KEY"]},
    timeout=10,
)
payload = r.json()
if payload["meta"].get("error_type"):
    raise RuntimeError(payload["meta"]["error_detail"])

daily = payload["response"]["data"]["daily"]
print(daily["time"][1], daily["temperature_2m_max"][1], "°C")

PHP

$key = getenv('WEATHERMETRO_KEY');
$url = 'https://api.weathermetro.com/v2/forecast?' . http_build_query([
    'lat' => 51.5, 'lng' => -0.13, 'daily' => '', 'api_key' => $key,
]);

$payload = json_decode(file_get_contents($url), true);
$daily = $payload['response']['data']['daily'];
echo $daily['time'][1] . ': ' . $daily['temperature_2m_max'][1] . "°C\n";

Go

resp, err := http.Get(
  "https://api.weathermetro.com/v2/forecast?lat=51.5&lng=-0.13&daily&api_key=" + key)
if err != nil { log.Fatal(err) }
defer resp.Body.Close()

var out struct {
  Response struct {
    Data struct {
      Daily struct {
        Time []string  `json:"time"`
        Max  []float64 `json:"temperature_2m_max"`
      } `json:"daily"`
    } `json:"data"`
  } `json:"response"`
}
json.NewDecoder(resp.Body).Decode(&out)
fmt.Println(out.Response.Data.Daily.Time[1], out.Response.Data.Daily.Max[1])

Ruby

require 'net/http'; require 'json'

uri = URI('https://api.weathermetro.com/v2/forecast')
uri.query = URI.encode_www_form(lat: 51.5, lng: -0.13, daily: '',
                                api_key: ENV.fetch('WEATHERMETRO_KEY'))

daily = JSON.parse(Net::HTTP.get(uri)).dig('response', 'data', 'daily')
puts "#{daily['time'][1]}: #{daily['temperature_2m_max'][1]}°C"

Other useful calls

# minute-scale rain outlook
/v2/nowcast?lat=40.64&lng=-73.78

# air quality & UV
/v2/airquality?lat=28.61&lng=77.21

# find a place, then use its coordinates
/v2/locations?q=kumasi

# what was the weather on this date?
/v2/history?lat=52.52&lng=13.40&start_date=2000-08-06&end_date=2000-08-06

Two things worth knowing. Responses always return HTTP 200 - check meta.error_type to detect failures. And the data sources behind every payload are credited on our attribution page; linking to it keeps you compliant with the upstream open-data licences.