Lansweeper Sites, Custom Fields, and API –
For Lansweeper On-Premise, I like to take advantage of custom fields to store various bits of data – from manual fields such as a description or definition (See The Importance of Asset Classification – Lansweeper ), to fields that I programmatically update from other systems via SQL Stored Procedures and Scheduled Tasks.
I couldn’t, however, do this with Lansweeper Sites – until recently – when we added the ability to edit asset data with the API! There was only one teeny, tiny problem: I had absolutely no idea how to do it. Naturally, I thought to myself – ‘I’ll bumble my way through it and do a Pro-Tip.’
Here’s a high-level overview of what I want to accomplish: Take a CSV with asset data, and update three custom fields with data:

There’s a lot of applications that have really bad API documentation – but lucky for me, Lansweeper isn’t one of them. Going to Documentation – Lansweeper provides not only the standard generated API documentation, but explanations, examples, and walkthroughs – exactly what I needed to get this Pro-Tip going.

The first thing I read was the ‘Authentication’ section – Documentation – Lansweeper – which walked me through how to create an application for me to gain access and authenticate to.
Armed with how to authenticate, the fun begins – so let’s begin!
We will first need to create an ‘API Client’ in Lansweeper Sites. You can do that by clicking on your avatar at the bottom left to go to ‘settings’ > ‘Developer Tools’:

Then, choose ‘Add New API Client’:

Select ‘OAuth’ for the authentication method, and add a name, description, and the callback URL (I used Postman’s callback API url). Choose ‘Other’ for ‘Integration’ and name accordingly:

One you create the API Client, you will be able to download the client IT and Client Secret:

Examining the .txt file, we see the client_id and the client_secret – we will need these later:

Now, we need to Add/Associate the appropriate Lansweeper Site (Remember, this example only works with a single site – you could get it to iterate through multiple sites with further development):


Now, Click ‘Authorize’ to obtain the authorization code – it will generate a token and pass to the Allowed callback URL in a new browser tab:

Copy the authorization code from the return URL:

So far so good. Next, I checked out ‘Making Your First Requests’ – which showed me how to do a few things with Postman – a great API testing application. I’ve used Postman a few times to do some test queries to grab some data, but not much else – but surely it can do much more, and allow someone with little to no API experience (me) to create something really useful.
After reading the examples of basic queries in the documentation, I can now start the process of setting up the custom fields and configuring Postman.
Here’s the overall steps that our Postman collection will take, and an overview of how we will make this happen:
AssetID.For this example – I have a CSV with some identifiers (SerialNumber, MAC, IP, FDQN) and three fields of data for them – each prefaced with ‘csvtestfield’ – which we will use to key off of to dynamically map the data to the fields:
| SerialNumber | MACAddress | IPAddress | FQDN | csvtestfield_custom1 | csvtestfield_custom2 | csvtestfield_custom3 |
We will need to create the respective custom fields in LS Sites by navigating to Inventory > Manage Custom Fields:

Make sure to match the field name exactly, as it is case sensitive: (csvtestfield_custom1, csvtestfield_custom2, csvtestfield_custom3) – in this example. the three fields will just be general text, so choose ‘Text field’ for the field type.

As it turns out, Postman can do a lot more than just testing APIs and running commands to get or update data. It can run through a chain of actions, execute scripts before and after each step, and collecting data and performing some ETL (extract, transform, load) functions – exactly what we need to take a CSV of asset information of some kind, and updating custom fields for each applicable asset in LS Sites. You can download the postman collections here (Copy the contents of the .docx file into a text file and save it as .json):
Set Up Variables. I know that starting examples have you entering in information in the headers section, and body/etc – but it’s always best to store things as variables – and Postman has a place for that – the ‘Environments’ section – where we will be storing everything we need (see ‘Environment Variables’ further below in this article). Some of these we will be putting the respective values for, but most of them will be dynamically populated – so it’s OK if most of them are initially blank. You can download the environment here, to then import into Postman:
Each of these environment variables plays a specific role in automating the API requests using Postman and Newman, ensuring that the right data is passed dynamically from the CSV and API responses. You will need to pre-populate the following fields:
client_idclient_secretauthorization_code(Note: You can either edit the variables in the GUI after you import the collection, or you can edit the .json file prior to importing)


The authorization_code variable should only be needed once, for the one-time run of the ‘Pro Tip CSV Authorization’ collection/step:




Now, you can utilize Postman’s ‘Collection Runner’ by right-cicking the ‘…’ and choosing ‘Run Collection’:

A neat feature in the Collection Runner is the ability to choose a file of data to test with – so we can use the CSV template file that we populated with real data:

The collection will run, and provide details on the progress:

We can then verify that the custom fields populated by pulling up an asset:

Now that I had this working, I needed a way to automate the process. There’s a great program called ‘Newman’ (the mail delivery guy and Jerry’s Nemesis in the show ‘Seinfeld’ – that is a command-line postman collection runner that can be scheduled. Perfect! All we need to do is export the collection, and the environment collection:


And then enter in your environment variables back in the environment .json file (reference the screenshot above that shows the .json variables in the text editor), as it removes them due to security protection –
NOTE: Protect your environment variables to prevent unauthorized access and potential security issues, as this pro-tip is just an example – you can use a variety of ‘secret’ managers such as AWS Secrets Manager, Azure Key Vault, Google Cloud Secret Manager, CyberArk Conjur, and more. As a fallback, since I don’t use any of those services, I used OpenSSL to encrypt the file – after which I would decrypt the file, tell Newman to run it, and then delete the unencrypted file after execution.
Here’s the neat thing – Newman can process the CSV file with the -d switch!
c:\temp>newman run "Pro Tip CSV Multiple Fields Consolidated.postman_collection.json" -e "Lansweeper API.postman_environment.json" -d "example_csv_template.csv"
Now Newman will run the collection, just like the collection runner in Postman did:


Congratulations, you can now schedule it as a task, and perform updates on custom fields from an on-premise CSV!
To Customize To Your Needs:

So there you have it – you don’t need to develop an application, or use expensive software in order to connect to Lansweeper’s API and import or update asset information from an on-premise data source – you can simply use Postman, and automate with a Postman Collection Runner such as Newman.
Here’s some of the many possibilities that you can do with this script:
As usual, Happy sweeping everyone!
Jacob
Node.js is a runtime environment for JavaScript, and npm is its package manager. Installing them allows you to use Newman for running Postman collections.
node -v npm -v
Newman is a command-line collection runner for Postman. It allows you to run Postman collections directly from the command line.
npm install -g newman
Postman is a widely used tool for API development and testing. You will use it to create and export API collections.
OpenSSL can be used to encrypt sensitive data like environment variables to secure your API tests.
openssl version
access_tokenAssetIDauthorization_codeclient_idclient_secretcsvtestfield_custom1csvtestfield_custom1_keycsvtestfield_custom2csvtestfield_custom2_keycsvtestfield_custom3csvtestfield_custom3_keyFQDNIPAddressMACAddressredirect_urirefresh_tokenSerialNumbersite_idtoken_expiryrefresh_token is present in the environment.refresh_token is found, it logs an error and sets a flag (skip_next_request) to prevent further requests from running.refresh_token is found, the skip_next_request flag is cleared.https://api.lansweeper.com/api/integrations/oauth/tokengrant_type: refresh_tokenclient_id: retrieved from the environment ({{client_id}})client_secret: retrieved from the environment ({{client_secret}})refresh_token: retrieved from the environment ({{refresh_token}})200.access_token to the environment and calculates its expiry (token_expiry).refresh_token is provided in the response, it updates that in the environment as well.access_token.site_id for further requests. NOTE: This script in it’s current form only functions for a single site. Further modification would be needed to iterate through multiple sites.access_token from the environment and adds it to the request headers (Authorization: Bearer).https://api.lansweeper.com/api/v2/graphql{ "query": "{ authorizedSites { sites { id name } } }" } site_id of the first authorized site is saved to the environment.AssetID.access_token is added to the request headers.site_id is retrieved from the environment, and the script ensures that the required CSV data (serial number, MAC, IP, or FQDN) is available.{ site(id: "siteID") { assetResources(assetPagination: { limit: 5, page: FIRST }, fields: ["assetBasicInfo.name", "assetCustom.serialNumber", "assetBasicInfo.mac", "assetBasicInfo.ipAddress"], filters: { conjunction: AND, groups: [ { conjunction: OR, conditions: [ { path: "assetCustom.serialNumber", operator: EQUAL, value: "serialNumber" }, { path: "assetBasicInfo.mac", operator: EQUAL, value: "macAddress" }, { path: "assetBasicInfo.ipAddress", operator: EQUAL, value: "ipAddress" }, { path: "assetBasicInfo.name", operator: EQUAL, value: "fQDN" } ] } ] }) { items } } }https://api.lansweeper.com/api/v2/graphqlAssetID is stored in the environment.access_token is added to the request headers.site_id is dynamically injected into the GraphQL query in the request body.https://api.lansweeper.com/api/v2/graphql{ site(id: "{{site_id}}") { customFields { name key } } } csvtestfieldX from the CSV, the matching custom field key from the API response is saved in the environment as csvtestfieldX_key.Explore Lansweeper for free.
No credit card required.