Code Examples with Promises Nov 5, 2017 Usage Here we present you the code examples of the library per API Method, we created these examples using functions with promises as it is easier to detect where occurred an error in the execution of your code. 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 Node API CLient initSessionByCredentials: this way you require the user and password to access GLPI, the appToken is optional const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { console.log(res) client.killSession() .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) initSessionbyUserToken: in this case you only require the user token, once again the app Token is optional const client = new GlpiRestClient(config.apirest) client.initSessionByUserToken(config.user.userToken, config.appToken) .then((res) => { console.log(res) client.killSession() .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) killSession(): close the user session const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.killSession() .then((res2) => { console.log(res2) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) getMyProfiles(): return the profiles associated to the user const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.getMyProfiles() .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) getActiveProfile(): return the current active profile const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.getActiveProfile() .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) changeActiveProfile(): change active profile to one of the profiles obtained with ‘getMyProfiles()’ const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.getMyProfiles() .then((res2) => { client.changeActiveProfile(res2.data[0].id) .then((res3) => { console.log(res3) client.killSession() .catch((err4) => { console.log(err4) }) }) .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) getMyEntities(): return all the possible entities of the current logged user const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.getMyEntities() .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) getActiveEntities(): return active entities of current logged user const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.getActiveEntities() .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) changeActiveEntities(): change active entity to one of the entities obtained with ‘getMyEntities’ const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { console.log(res) client.changeActiveEntities(null, true) .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) getFullSession(): return the current php $_SESSION const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.getFullSession() .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) getGlpiConfig(): return the current $CFG_GLPI const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.getGlpiConfig() .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) getItem(): return the instance fields of itemtype identified by id 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 client.initSessionByCredentials(config.user.name, config.user.password) .then((res) => { client.getItem(itemtype.User, 40, query.createQueryObject()) .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) getAllItems(): return a collection of rows of the itemtype onst client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.getAllItems(itemtype.UserEmail) .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) getSubItems(): return a collection of rows of the sub_itemtype for the identified item const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.getSubItems(itemtype.User, 37, itemtype.UserEmail) .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) addItems(): add an object (or multiple objects) into GLPI const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.addItem(itemtype.UserEmail, {users_id: 37, email: 'example@email.com'}) .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) updateItems(): update an object (or multiple objects) existing in GLPI const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.addItem(itemtype.UserEmail, {users_id: 37, email: 'example@email.com'}) .then((res2) => { console.log(res2) client.updateItem(itemtype.UserEmail, null, {id: res2.data.id, users_id: 37, email: 'example2@email.com'}) .then((res3) => { console.log(res3) client.killSession() .catch((err4) => { console.log(err4) }) }) .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) deleteItems(): delete an object existing in GLPI const client = new GlpiRestClient(config.apirest) client.initSessionByCredentials(config.user.name, config.user.password, config.appToken) .then((res) => { client.deleteItem(itemtype.UserEmail, null, {id: 162, users_id: 37, email: 'example@email.com'}) .then((res2) => { console.log(res2) client.killSession() .catch((err3) => { console.log(err3) }) }) .catch((err2) => { console.log(err2) }) }) .catch((err) => { console.log(err) }) recoveryPassword(): sends a notification to the user to reset his password through email const client = new GlpiRestClient(config.apirest) client.recoveryPassword(config.user.email) .then((res) => { console.log(res) }) .catch((err) => { console.log(err) }) resetPassword(): sends a notification to the user to reset his password const client = new GlpiRestClient(config.apirest) client.resetPassword(config.user.email, 'e446c1ff23d7ba097cc7104edaee5139ddf610be', 12345678) .then((res) => { console.log(res) }) .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.