Module Usage

Module Usage

If you are developing your own JavaScript application using a NodeJS server or using NPM libraries, you can use our NPM module instead which offers greater flexibility than using the standard combined library. Our NPM module has been transpiled for CommonJS (opens in a new tab) modules, which means you can start using it in your NodeJS applications without additional setup.

Once you have an active Aeris API account and access keys for your application, follow these steps to get started using the NPM module:

Step 1: Install the latest version of the SDK module:

yarn add @aerisweather/javascript-sdk
```a
 
Alternatively, if you're using NPM:
 
```bash
npm install @aerisweather/javascript-sdk

Step 2: Import the module into your project:

import AerisWeather from '@aerisweather/javascript-sdk';

Or, if you can't use the ES6 import method, you can import the module using require() instead:

const { AerisWeather } = require('@aerisweather/javascript-sdk');

Step 4: Import the stylesheet into your project if you are using any of the built-in views that are part of the SDK. You can choose to import the precompiled CSS or the SASS format if you have a CSS compilation task setup in your build process, such as using Webpack (opens in a new tab) and sass-loader (opens in a new tab):

// import the pre-compiled CSS
import '@aerisweather/javascript-sdk/dist/styles/styles.css';
 
// or, import the raw SASS, which requires a CSS compiler in your build process
import '@aerisweather/javascript-sdk/dist/styles/sass/styles.scss';

Step 3: Set your account access keys when the page and scripts have loaded:

const aeris = new AerisWeather('CLIENT_ID', 'CLIENT_SECRET');

Step 4: Start using the library by accessing the available convenience methods on your AerisWeather instance:

aeris.api().endpoint('observations').place('seattle,wa').get().then((result) => {
     var data = result.data.ob;
     console.log(`The current weather is ${data.weatherPrimary.toLowerCase()} and ${data.tempF} degrees.`);
});

Review the Requesting Data & Maps guide for information on getting data into your application now that you have everything set up.

Using Individual Classes

One benefit to using the SDK as an NPM module is that you have access to the entire set of classes throughout the SDK and aren't limited to functionality exposed by the core AerisWeather object.

For instance, you can opt to not use the AerisWeather class altogether and create request instances directly using imports from the SDK's module. The following is an example of creating an ApiRequest instance directly instead of using AerisWeather.api():

import ApiRequest from '@aerisweather/javascript-sdk/dist/network/weather-api/ApiRequest';
 
const request = new ApiRequest({
     client: {
          id: 'CLIENT_ID',
          secret: 'CLIENT_SECRET'
     }
});
 
// configure and perform the request
request.endpoint('forecasts').place('minneapolis,mn').get().then((result) => {
     console.log(result.data);
});

You can use the same approach by importing MapRequest directly as well to retrieve Raster Maps imagery:

import MapRequest from '@aerisweather/javascript-sdk/dist/network/maps/MapRequest';
 
const request = new MapRequest({
     client: {
          id: 'CLIENT_ID',
          secret: 'CLIENT_SECRET'
     }
});
 
// configure and perform the request
request.layers('flat,radar,admin').center('minneapolis,mn').zoom(8).size(500, 300).get().then((result) => {
    console.log(result.image, result.metadata);
});

Review our complete developer SDK documentation for the additional information about the classes and their roles used throughout the SDK.