Node.js Bungie.net API Library
This library aims to be the fastest, lightest-weight JavaScript implementation of the Bungie.net API. This project has 3 sacred goals
- Zero Dependency
- Fully Asynchronous
- Well Documented
Setup
- First create a project at
https://www.bungie.net/en/Application
- Make sure that your OAuth client type is "Confidential" if you need to make oAuth calls
- Install this library
npm install bungie-net-api
- Finally, create an API object as show below
const ApiCredentials = key : "you_secret_key" clientId : "your_client_id" clientSecret: "your_client_secret" const BungieLib = ; // This will load ALL micro-libraries const Api = ApiCredentials ;
- If you only need to load certain endpoints, you can do so by passing an array of micro-libraries that you will need to the BungieLib constructor
const ApiCredentials = key : "you_secret_key" clientId : "your_client_id" clientSecret: "your_client_secret" const BungieLib = ; // This will only load the Destiny2, Forum, and User Micro-Libraries const Api = ApiCredentials 'destiny2' 'forum' 'user' ;
- The first thing that you should do now is redirect your end user to the authentication endpoint and have them give your app permission to interact with their account
// Redirect the end user to the auth uri somehow WebSocket;
- Once the user has approved your application, they will be redirected to the uri that you specified at
https://www.bungie.net/en/Application/Detail/
and your accessCode will be a querystring parameter. Grab it associate it with this client somehow (I use a session store). You'll need this code if you want to make privileged API requests
Fully Asynchronous
The library makes extensive use of native ES6 promises. Any non-trivial work load is exectued asynchonously making the library extrememly scalable. All API call return an ES6 Promise that resolves with the parsed Bungie.net API response.
ApiUser;
This means that you can make as many API calls as you want simultaneously.
// All of these API calls will me made simultaneously. let calls = ApiUser ApiUser ApiDestiny2 ApiTrending ; // Once all of those API calls have finished, do work Promiseall calls ;
Api Call Chaining
The Flexibility of promises allows us to take multiple API calls and chain them one after the other. FOr instance, lets request an oAuth access token and then immediately refresh that token. (You would never do this in real life, but it's a good example of an instance when we need one promise to wait on another promise)
ApiOAuth ;
Requesting an oAuth token
ApiOAuth;
Refreshing an oAuth token
ApiOAuth;