# HTTP get format for UK Met Office API

**URL:** https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255
**Category:** General
**Created:** [16 June 2023 14:19 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255 "2023-06-16T14:19:37Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [16 June 2023 14:19 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/1 "2023-06-16T14:19:37Z")

</div>

Can someone advise me how to structure msg to access the UK Met Office DataPoint API?

The base URL is [https://api-metoffice.apiconnect.ibmcloud.com/v0/forecasts/point/hourly](https://api-metoffice.apiconnect.ibmcloud.com/v0/forecasts/point/hourly)'

Required parameters are (I think) 'x-ibm-client-id', 'x-ibm-client-secret', 'latitude' and 'longitude' and I believe they should go in msg.headers (I also tried adding them to the URL)

I tried this

```auto
msg.url = 'https://api-metoffice.apiconnect.ibmcloud.com/v0/forecasts/point/hourly'
msg.headers = {};
msg.headers['x-ibm-client-idX-Auth-User'] = 'blah';
msg.headers['x-ibm-client-secret'] = 'blahblah';
msg.headers['latitude'] = 53.105019 
msg.headers['longitude'] = -3.912304
return msg;

```

which gives

```auto
{"httpCode":"401","httpMessage":"Unauthorized","moreInformation":"Client id missing."}

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [16 June 2023 14:28 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/2 "2023-06-16T14:28:12Z")

</div>

> [@jbudd](#):
>
> `msg.headers['x-ibm-client-idX-Auth-User'] = 'blah';`

This does not look right.

Try

```javascript
const lat = 53.105019
const lon = -3.912304
msg.url = `https://api-metoffice.apiconnect.ibmcloud.com/v0/forecasts/point/hourly?excludeParameterMetadata=false&includeLocationName=true&latitude=${lat}&longitude=${lon}`
msg.headers = {
    'x-ibm-client-id': "YOUR_CLIENT_ID",
    'x-ibm-client-secret': "YOUR_CLIENT_SECRET",
    accept: "application/json"
}

```

built from ideas in this thread: [New weather API from the MetOffice (worldwide) - Raspberry Pi Forums](https://forums.raspberrypi.com/viewtopic.php?t=262588)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [16 June 2023 14:30 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/3 "2023-06-16T14:30:15Z")

</div>

PS: there is really no need for a function node. The URL can be set up in the HTTP Req node as can the headers now too.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [16 June 2023 15:45 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/4 "2023-06-16T15:45:02Z")

</div>

> [@Steve-Mcl](#):
>
> Try
> 
> ```auto
> const lat = 53.105019
> const lon = -3.912304
> msg.url = `https://api-metoffice.apiconnect.ibmcloud.com/v0/forecasts/point/hourly?excludeParameterMetadata=false&includeLocationName=true&latitude=${lat}&longitude=${lon}`
> msg.headers = {
> 'x-ibm-client-id': "YOUR_CLIENT_ID",
> 'x-ibm-client-secret': "YOUR_CLIENT_SECRET",
> accept: "application/json"
> }
> 
> ```
> 
> built from ideas in this thread: [New weather API from the MetOffice (worldwide) - Raspberry Pi Forums](https://forums.raspberrypi.com/viewtopic.php?t=262588)

That works thanks Steve.

I could not work out the right format from the Met Office's own sample Python script, I was trying to use the cookbook recipe [https://cookbook.nodered.org/http/set-request-header](https://cookbook.nodered.org/http/set-request-header) which is for posting not requesting data  
I never thought to look in Raspberry forums since strangely there is so little Node-red there.

I know no need for a function, it's just the go-to when one doesn't know what one is doing. 🤪

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [18 June 2023 15:29 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/5 "2023-06-18T15:29:02Z")

</div>

I use a change node to input the parameters for the request node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d2b41ce1ee181a9d450aba75af0ed8de5a1d83d.png)

This also gets back the location name. Useful because it isn't always obvious what base location the API has chosen - it might not be the input lat/lon.

This is really important somewhere like Sheffield that has lots of micro-climates. The weather can be significantly different just 1/2 mile away.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [18 June 2023 15:33 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/6 "2023-06-18T15:33:08Z")

</div>

> [@jbudd](#):
>
> ```auto
> msg.headers['latitude'] = 53.105019 
> msg.headers['longitude'] = -3.912304
> 
> ```

Ah, love the location! Spent a LOT of time in that area in the past. 🙂

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [18 June 2023 16:04 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/7 "2023-06-18T16:04:26Z")

</div>

I'm finding the Met Office API hard to get a grip on. I have the single location forecast as in this thread working, though not for that particular location.

Strangely it complained that my latitude and longitude were too precise; I had to knock off a couple of decimal places before it would accept them, and then I think the forecast provided is just for the nearest named location in their database.

It's the weather maps I'm interested in at the moment. You need a different client id to access these, then create an "order" for the maps to be calculated [specially?] for you at 00:00 and 12:00. So it looks like there is no current conditions map.  
So far all I have retrieved from this API is this, which I think means they did not calculate my forecast maps yet.

```auto
WARNING: The run 0 has been asked for but doesn't appear in the order o091234540
WARNING: The run 12 has been asked for but doesn't appear in the order o091234540
WARNING: No runs for order o091234540were found. Don't expect any data.

```

It's all very odd, I asked for a UK & Ireland map, surely they already calculated it for someone else?

In summary, I can't so far see any advantage in the Met Office API over OpenWeatherMap.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [18 June 2023 16:45 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/8 "2023-06-18T16:45:26Z")

</div>

> [@jbudd](#):
>
> I think the forecast provided is just for the nearest named location in their database.

"Official" data for any country is limited to recognised observation points (I believe there are around 5,000 for the UK). Typically mostly airports. Of course, some API's also include data ripped from non-official sources such as home weather stations. Also, some API's do interpolation for forecasts and so claim to give localised forecasts down to a few hundred meters. Best to take these with a pinch of salt! I occasionally compare local forecasts and even those over the next few hours rarely agree with each other exactly.

As always, the best forecast is to look out the window and get good at judging things. 😁

> [@jbudd](#):
>
> It's the weather maps I'm interested in at the moment. You need a different client id to access these, then create an "order" for the maps to be calculated [specially?] for you at 00:00 and 12:00. So it looks like there is no current conditions map.

Never tried those with a free account. I expect you've already gone through all this - but in case anyone else wants to try:

The Met Office DataPoint login I have lists a number of products for observations and forecasts. The detailed API info is here: [DataPoint API reference (metoffice.gov.uk)](https://www.metoffice.gov.uk/binaries/content/assets/metofficegovuk/pdf/data/datapoint_api_reference.pdf).

But as well as the DataPoint API's, there is a separate DataHub set. [Home | Weather DataHub (ibmcloud.com)](https://metoffice.apiconnect.ibmcloud.com/metoffice/production/), This contains a lot more data but requires a separate account. They include a lot more imagery.

> All our products have a free subscription for long term low volume usage or as an introduction to the product which can be updated to a paid subscription for more data at a later time (if required).

> [@jbudd](#):
>
> In summary, I can't so far see any advantage in the Met Office API over OpenWeatherMap.

I generally find it to be far more accurate. Not really surprising since it comes direct from some of the most advanced weather models on Earth!

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [18 June 2023 19:25 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/9 "2023-06-18T19:25:42Z")

</div>

@jbudd Try this flow. Just add your client ID & client secret.

```auto
[{"id":"138e060b5da2f1af","type":"http request","z":"03cd1d2be26baaa9","name":"API call","method":"use","ret":"obj","paytoqs":"query","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":400,"y":400,"wires":[["ad4905267f9f1c5b"]]},{"id":"ad4905267f9f1c5b","type":"function","z":"03cd1d2be26baaa9","name":"format data","func":"let fullArray = msg.payload.features[0].properties.timeSeries\nfullArray = fullArray.slice(0, 37)\nconst tempArray = []\nvar code\nvar lookupTable = {\n 0: \"Clear night\",\n 1: \"Sunny day\",\n 2: \"Partly cloudy (night)\",\n 3: \"Partly cloudy (day)\",\n 4: \"Not used\",\n 5: \"Mist\",\n 6: \"Fog\",\n 7: \"Cloudy\",\n 8: \"Overcast\",\n 9: \"Light rain shower (night)\",\n 10: \"Light rain shower (day)\",\n 11: \"Drizzle\",\n 12: \"Light rain\",\n 13: \"Heavy rain shower (night)\",\n 14: \"Heavy rain shower (day)\",\n 15: \"Heavy rain\",\n 16: \"Sleet shower (night)\",\n 17: \"Sleet shower (day)\",\n 18: \"Sleet\",\n 19: \"Hail shower (night)\",\n 20: \"Hail shower (day)\",\n 21: \"Hail\",\n 22: \"Light snow shower (night)\",\n 23: \"Light snow shower (day)\",\n 24: \"Light snow\",\n 25: \"Heavy snow shower (night)\",\n 26: \"Heavy snow shower (day)\",\n 27: \"Heavy snow\",\n 28: \"Thunder shower (night)\",\n 29: \"Thunder shower (day)\",\n 30: \"Thunder\"\n}\n\nfor (let i = 0; i < fullArray.length; i++) {\n let a = (fullArray[i])\n let weatherCode = (lookupTable[a.significantWeatherCode])\n const ts = new Date(a.time).getTime()\n const tempData = {\n measurement: \"metoffice\",\n fields: {\n temperature: a.screenTemperature,\n maxTemp: a.maxScreenAirTemp,\n minTemp: a.minScreenAirTemp,\n feelsLike: a.feelsLikeTemperature,\n windGust: Math.round(a.windGustSpeed10m / 1609.344 * 3600),\n windSpeed: Math.round(a.windSpeed10m / 1609.344 * 3600),\n rainProb: a.probOfPrecipitation,\n rainPrecip: a.totalPrecipAmount,\n weatherCode: weatherCode,\n uvIndex: a.uvIndex\n },\n tags: {\n tag1: 'weather',\n tag2: 'metoffice'\n },\n timestamp: ts\n }\n tempArray.push(tempData)\n}\n\nmsg.payload = tempArray\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":570,"y":400,"wires":[["298c8848b85db6eb"]]},{"id":"4d3234f2043a477e","type":"change","z":"03cd1d2be26baaa9","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"excludeParameterMetadata\":true,\"includeLocationName\":true,\"latitude\":53.105019,\"longitude\":-3.912304}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"accept\":\"application/json\",\"x-ibm-client-secret\":\"client secret here\",\"x-ibm-client-id\":\"client id here\"}","tot":"json"},{"t":"set","p":"url","pt":"msg","to":"https://api-metoffice.apiconnect.ibmcloud.com/metoffice/production/v0/forecasts/point/hourly","tot":"str"},{"t":"set","p":"method","pt":"msg","to":"GET","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":275,"y":400,"wires":[["138e060b5da2f1af"]],"l":false,"info":"[https://metoffice.apiconnect.ibmcloud.com/metoffice/production/api](https://metoffice.apiconnect.ibmcloud.com/metoffice/production/api)"},{"id":"a8b5e205fd9fdd2a","type":"inject","z":"03cd1d2be26baaa9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":400,"wires":[["4d3234f2043a477e"]]},{"id":"298c8848b85db6eb","type":"debug","z":"03cd1d2be26baaa9","name":"debug 19","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":400,"wires":[]}]

```

EDIT - Sorry, re-read your last post & you already have it working!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [2 July 2023 19:25 UTC](https://discourse.nodered.org/t/http-get-format-for-uk-met-office-api/79255/10 "2023-07-02T19:25:55Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
