Code Examples with Async - Await Nov 5, 2017 Usage Here we present you the code examples of the library per API Method, as we do our best to provide the most up to date technologies, we created the examples using Async - Await functions as it is the latest form to implement the asynchronous methods with Node, as it is you may not be able to use it properly unless your Node.js environment is up to date. initSession() killSession() getMyProfiles() getActiveProfile() changeActiveProfile() getMyEntities() getActiveEntities() changeActiveEntities() getFullSession() getGlpiConfig() getItem() getAllItems() getSubItems() addItems() updateItems() deleteItems() recoveryPassword() resetPassword() Examples initSession(): there are two ways to init session using the API CLient. initSessionByCredentials: this way you require the user and password to access GLPI, the appToken is optional (async () => { try { const client = new GlpiRestClient(config.apirest) const Session = await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) console.log(Session) await client.killSession() } catch (err) { console.log(err) } })() initSessionbyUserToken: in this case you only require the user token, once again the app Token is optional (async () => { try { const client = new GlpiRestClient(config.apirest) const Session = await client.initSessionByUserToken(config.user.userToken, config.appToken) console.log(Session) await client.killSession() } catch (err) { console.log(err) } })() killSession(): close the user session (async () => { try { const client = new GlpiRestClient(config.apirest) // await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken); const Session = await client.killSession() console.log(Session) } catch (err) { console.log(err) } })() getMyProfiles(): return the profiles associated to the user (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const MyProfiles = await client.getMyProfiles() console.log(MyProfiles) await client.killSession() } catch (err) { console.log(err) } })() getActiveProfile(): return the current active profile (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const ActiveProfile = await client.getActiveProfile() console.log(ActiveProfile) await client.killSession() } catch (err) { console.log(err) } })() changeActiveProfile(): change active profile to one of the profiles obtained with ‘getMyProfiles()’ (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const MyProfiles = await client.getMyProfiles() const ChangeActiveProfile = await client.changeActiveProfile(MyProfiles.data[0].id) console.log(ChangeActiveProfile) await client.killSession() } catch (err) { console.log(err) } })() getMyEntities(): return all the possible entities of the current logged user (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const MyEntities = await client.getMyEntities() console.log(MyEntities) await client.killSession() } catch (err) { console.log(err) } })() getActiveEntities(): return active entities of current logged user (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const ActiveEntities = await client.getActiveEntities() console.log(ActiveEntities) await client.killSession() } catch (err) { console.log(err) } })() changeActiveEntities(): change active entity to one of the entities obtained with ‘getMyEntities’ (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const ChangeActiveEntities = await client.changeActiveEntities(null, true) console.log(ChangeActiveEntities) await client.killSession() } catch (err) { console.log(err) } })() getFullSession(): return the current php $_SESSION (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const FullSession = await client.getFullSession() console.log(FullSession) await client.killSession() } catch (err) { console.log(err) } })() getGlpiConfig(): return the current $CFG_GLPI (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const GlpiConfig = await client.getGlpiConfig() console.log(GlpiConfig) await client.killSession() } catch (err) { console.log(err) } })() getItem(): return the instance fields of itemtype identified by id (async () => { try { const client = new GlpiRestClient(config.apirest) let query = new GetItemQuery() query.with_networkports = true query.with_infocoms = true query.with_contracts = true query.with_documents = true await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const Item = await client.getItem(itemtype.User, 40, query.createQueryObject()) console.log(Item) await client.killSession() } catch (err) { console.log(err) } })() getAllItems(): return a collection of rows of the itemtype (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const AllItems = await client.getAllItems(itemtype.UserEmail) console.log(AllItems) await client.killSession() } catch (err) { console.log(err) } })() getSubItems(): return a collection of rows of the sub_itemtype for the identified item (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const SubItems = await client.getSubItems(itemtype.User, 37, itemtype.UserEmail) console.log(SubItems) await client.killSession() } catch (err) { console.log(err) } })() addItems(): add an object (or multiple objects) into GLPI (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const NewItem = await client.addItems(itemtype.UserEmail, [{users_id: 40, email: 'example@email.com'}, {users_id: 40, email: 'example2@email.com'}]) console.log(NewItem) await client.killSession() } catch (err) { console.log(err) } })() updateItems(): update an object (or multiple objects) existing in GLPI (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const Item = await client.updateItems(itemtype.UserEmail, null, [{id: 169, email: 'exam@email.com'}, {id: 170, email: 'exam2@email.com'}]) console.log(Item) await client.killSession() } catch (err) { console.log(err) } })() deleteItems(): delete an object existing in GLPI (async () => { try { const client = new GlpiRestClient(config.apirest) await client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) const ItemDelete = await client.deleteItems(itemtype.UserEmail, null, [{id: 167}, {id: 168}]) console.log(ItemDelete) await client.killSession() } catch (err) { console.log(err) } })() recoveryPassword(): sends a notification to the user to reset his password through email (async () => { try { const client = new GlpiRestClient(config.apirest) const recoveryPassword = await client.recoveryPassword(config.user.email) console.log(recoveryPassword) } catch (err) { console.log(err) } })() resetPassword(): sends a notification to the user to reset his password (async () => { try { const client = new GlpiRestClient(config.apirest) const resetPassword = await client.resetPassword(config.user.email, 'yourToken', 'yourNewPassword') console.log(resetPassword) } catch (err) { console.log(err) } })() For more information about the functions check our Code Documentation section Have feedback on this post? Let @glpi_project know on Twitter. Need help or found a bug? Contact us.