# Accessing the Mender API with NodeJS - PAT version

**URL:** <https://hub.mender.io/t/accessing-the-mender-api-with-nodejs-pat-version/6494>\
**Category:** Tutorials\
**Tags:** api, mender-server, management-api\
**Created:** [January 18, 2024, 1:22pm UTC](https://hub.mender.io/t/accessing-the-mender-api-with-nodejs-pat-version/6494 "2024-01-18T13:22:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheYoctoJester](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/theyoctojester/32/1444_2.png) [@TheYoctoJester](https://hub.mender.io/u/TheYoctoJester)\
**Post date:** [January 18, 2024, 1:22pm UTC](https://hub.mender.io/t/accessing-the-mender-api-with-nodejs-pat-version/6494/1 "2024-01-18T13:22:38Z")

</div>

# Introduction

The Mender backend follows an API first philosphy. This means all functionality is exposed through documented endpoints, and you are invited to use those to match and implement your custom flows.

In this short tutorial, I will show you how to access the API from a NodeJS script using a [personal access token](https://docs.mender.io/server-integration/using-the-apis#personal-access-tokens). For more in-depth information on the API, check out the [documentation](https://docs.mender.io/server-integration/using-the-apis) and the [API reference](https://docs.mender.io/api/).

# Prerequisites

To follow the examples you need the following:

- a Hosted Mender account. We will use the US instance, but adapting to the EU one is straightforward. You can sign up [here](https://hosted.mender.io/signup)
- the [NodeJS](https://nodejs.org) runtime being installed

# Tutorial

## Step 0: Project setup

We will create a project named `mender-api-demo` using generic NodeJS practises.

```auto
mkdir mender-api-demo
cd mender-api-demo
npm init -y

```

For simplicity, we will use the [`axios`](https://github.com/axios/axios) library to do the API calls. Add it to the project:

```auto
npm i --save-prod axios

```

Now open a new file `index.js` in the editor of your choice, and add the line

```auto
const axios = require('axios');

```

to load the `axios` library. Then you’re ready to go!

## Step 1: Authentication - generating personal access token (PAT)

**NOTE: this is not possible for accounts which are using a SSO login such as Google or Microsoft currently**

Log into your Mender dashboard and open the “My profile” view. This is the second entry in the dropdown menu which appears when you click your mail address in the top right corner.

In “Personal access token management” section, click “Generate a token”. This opens a dialog to define name and expiration. The permission level is tied to your user permissions.

 ![Screenshot 2024-01-17 at 14.43.14](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/2X/a/ae8f85af667b78c3b95368417a6ea18771f0cc44.png)  
Once finished, click “Create Token”

The generated token is then displayed in a following view. Copy it to your clipboard and store it to a permanent place, as it cannot be shown again. If you lose the token, you need to revoke it and create a new one.

 ![Screenshot 2024-01-17 at 14.44.09](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/2X/b/bc3b81ab0436a194c196ffe79bf3b1551c644bdb.png)

## Step 2: API access

We will call the [`device list`](https://docs.mender.io/api/#management-api-device-inventory-list-device-inventories) API endpoint to receive a list of devices accepted on our account. Please note that the endpoint is paginated, so if you have a large number of devices then the returned array will not be exhaustive.

Add the following code to `index.js`, and insert your PAT into the `jwt` constant assignment:

```auto
const jwt = "...."; // paste your token here!

axios({
    method: 'GET',
    url: "https://hosted.mender.io/api/management/v1/inventory/devices",
    headers: {
      'Accept': 'application/json',
      'Authorization': 'Bearer ' + jwt,
    }
  });
})
.then((response) => {
  console.log("received device list: " + JSON.stringify(response.data));
})
.catch((error) => {
  console.log("error" + error);
});

```

This prints a list including an extensive list of details on each device - the inventory - to the console.

Congratulations, you have just successfully called the Mender server API!

# Next steps and conclusion

You are now perfectly equipped to start using the Mender API for your custom logic. Some ideas to explore:

- uploading artifacts
- creating deployments
- gathering statistics

By exposing and documenting all facets of the API, the Mender backend offers both high customizability and anti-lock in, as you can replace each part as needed. Web frontend, backend, none or both.

What did you build? Let us know in the comments!
