MetOcean API
A typescript wrapper for MetOcean Solutions' weather and marine APIs, featuring type-safety, error-handling and native JS Dates. Get an API key here.
Currently supports time-series, point, and route endpoints.
Warning: currently experimental. May be breaking changes yet.
Disclaimer: this package is unofficial and its creator is not affiliated with
MetOcean Solutions'.
Requirements
- Node.js v18 or higher (or works fine with a fetch polyfill on v16)
- ES6+
- Typescript 4.5.0+
Installation
$ npm install metocean-api
Basic Usage
Important: do not use this package client-side as you will expose your API key.
Create a new instance of MetOcean by passing in your API key.
import { MetOcean } from 'metocean-api';
const m = new MetOcean({ apiKey: '<YOUR-API-KEY>' });
Call a method on the instance to retrieve data from the API, with the added bonus of autocomplete in modern IDEs!
Handling Errors
See our full example with error-handling.
-
MetOceanIllegalArgumentError
Thrown when an invalid argument is provided to an API method, such as providing an empty array of variables
. This error is thrown before a request is made.
Thrown when the API responds with a bad HTTP code, such as when an invalid API key is provided.
Networking issues such as loss of internet connection also throw an error.
Full Example With Error-Handling
import {
MetOcean,
MetOceanError,
MetOceanIllegalArgumentError,
MetOceanRequestError
} from 'metocean-api';
const m = new MetOcean({ apiKey: 'your-api-key' });
try {
const data = await m.getPointTimeSeries({
points: [{ lat: -37.82, lon: 174.89 }],
time: { from: new Date(), repeat: 3 },
variables: ['cloud.cover']
});
console.log(data.variables['wave.height']?.data);
} catch (err) {
if(err instanceof MetOceanUnauthorizedError){
}
if(err instanceof MetOceanNotFoundError){
}
if(err instanceof MetOceanInputError){
}
if(err instanceof MetOceanServerError){
}
if(err instanceof MetOceanError){
console.log(err.httpStatusCode);
console.log(err.errorList.toString());
}else{
}
console.error(err);
}
Contribution
Contributions to this package are welcome. Please open an issue to discuss any changes, bugs or improvements you would like to make. Suggestions are welcome.
Current todo
- add support for /variables endpoint.
- add support for /models endpoint.
- write (better) tests
License
MIT License.