|
| 1 | +# netatmo-nodejs-api |
| 2 | + |
| 3 | +[](https://www.gnu.org/licenses/agpl-3.0) |
| 4 | +[](https://github.com/nioc/netatmo-nodejs-api/releases/latest) |
| 5 | + |
| 6 | +Node.js API wrapper for Netatmo API. |
| 7 | + |
| 8 | +## Key features |
| 9 | +- Authentication with all Netatmo provided methods: |
| 10 | + - Authorization code grant type |
| 11 | + - Client credentials grant type |
| 12 | + - Refresh token |
| 13 | +- Security API (get homes, get events) |
| 14 | +- Weather API (get public data, get stations data, get measure) |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +``` bash |
| 19 | +npm install netatmo-nodejs-api |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Basic example with refresh token and client credentials grant type |
| 25 | +```js |
| 26 | +const { NetatmoClient, SCOPE_BASIC_CAMERA } = require('netatmo-nodejs-api') |
| 27 | + |
| 28 | +// you need to set your own information |
| 29 | +const clientId = '60...' |
| 30 | +const clientSecret = 'abc...' |
| 31 | +const username = 'user@domain' |
| 32 | +const password = 'pass' |
| 33 | +let refreshToken = '' |
| 34 | +let accessToken = '' |
| 35 | +let expiresInTimestamp = 0 |
| 36 | + |
| 37 | +try { |
| 38 | + // create client |
| 39 | + const client = new NetatmoClient(clientId, clientSecret, SCOPE_BASIC_CAMERA, { timeout: 1000 }) |
| 40 | + |
| 41 | + // authenticate |
| 42 | + if (!client.checkAndSetAccesToken(accessToken, expiresInTimestamp)) { |
| 43 | + if (refreshToken) { |
| 44 | + // use previous refresh token |
| 45 | + ({ accessToken, refreshToken, expiresInTimestamp } = await client.authenticateByRefreshToken(refreshToken)) |
| 46 | + } else { |
| 47 | + // use user credentials |
| 48 | + ({ accessToken, refreshToken, expiresInTimestamp } = await client.authenticateByClientCredentials(username, password)) |
| 49 | + } |
| 50 | + // you should store accessToken, refreshToken, expiresInTimestamp for further request |
| 51 | + } |
| 52 | + |
| 53 | + // get data |
| 54 | + const homes = await client.getHomes() |
| 55 | +} catch (error) { |
| 56 | + console.log(error) |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Versioning |
| 61 | + |
| 62 | +netatmo-nodejs-api is maintained under the [semantic versioning](https://semver.org/) guidelines. |
| 63 | + |
| 64 | +See the [releases](https://github.com/nioc/netatmo-nodejs-api/releases) on this repository for changelog. |
| 65 | + |
| 66 | +## License |
| 67 | + |
| 68 | +This project is licensed under the GNU Affero General Public License v3.0 - see the [LICENSE](LICENSE.md) file for details |
0 commit comments