Get active and inactive users
Run a Python script against the Harness Audit API to identify which users logged in over a specified time period.
Identify which users logged in to your Harness account over a specific time period. This topic provides a Python script that queries the Harness Audit API for LOGIN events across a date range, compares the results against every user in your account, and categorizes each user as active, inactive, or deleted.
Login activity supports several account management tasks:
Compliance and auditing: Track user access for security and regulatory requirements.
License management: Identify active users to optimize license usage.
User lifecycle management: Find inactive users who may need to be offboarded.
What you will learn in this topic
By the end of this topic, you will be able to:
Understand how the script works and which output files it produces.
Run the script with a custom date range or environment variables.
Review the script parameters to control the account and reporting window.
Interpret the output files and count or extract user records.
Troubleshoot authentication, permission, and rate limit errors.
Before you begin
Before you run the script, ensure you have the following:
Python 3.x: Installed on the system where you run the script.
Python requests library: Install it with
pip install requests.API token: A token with permission to read audit logs and users. For more information, see Manage API keys.
Audit log permission: Permission to view audit logs in your Harness account. For more information, see Permissions reference.
Harness account ID: Available in any Harness URL, for example
https://app.harness.io/ng/account/<ACCOUNT_ID>/....
How the script works
Understand the output categories before you act on the results, because an empty login record does not always mean the account is safe to delete. The script queries the Harness Audit API for LOGIN events within a date range, compares that data against all users in your account, and writes three files.
active_users.ndjson: Users who logged in during the specified time period.
inactive_users.ndjson: Users who exist in the account but did not log in during the specified time period.
deleted_users.ndjson: Users who logged in during the specified time period but no longer exist in the account.
The output files use NDJSON format (newline-delimited JSON), where each line is a valid JSON object representing one user record.
Run the script
Save the complete script as get_inactive_users.py, then run it from the command line with your environment URL and credentials. By default, the script analyzes the last 30 days of login activity.
Specify a custom date range
Set an explicit window when you report on a fixed audit period, such as a quarter, rather than the trailing 30 days. Pass the --start and --end parameters:
Use environment variables
Set credentials as environment variables to keep tokens out of your shell history and process list. The script reads HARNESS_API_KEY for an API key and HARNESS_BEARER for a Bearer token.
Script parameters
Use these parameters to control the target account, the authentication method, and the reporting window.
--env
Yes
Harness environment URL in the format <domain>.harness.io/ng/account/<account_id>/
None
app.harness.io/ng/account/abc123/
--apikey
No*
Harness API key for authentication
HARNESS_API_KEY env var
pat.abc123.xyz...
--bearer
No*
Bearer token for authentication
HARNESS_BEARER env var
eyJhbGc...
--start
No
Start date and time in YYYY-MM-DD HH:MM format
30 days ago
2025-01-01 00:00
--end
No
End date and time in YYYY-MM-DD HH:MM format
Current time
2025-01-31 23:59
* One of --apikey or --bearer is required, or the corresponding environment variable.
Interpret the output
Read the output files to decide which accounts to offboard and which to retain. The script writes all three NDJSON files to the current directory.
active_users.ndjson
Contains audit log entries for users who logged in during the specified time period. Each line includes:
inactive_users.ndjson
Contains user records for users who exist in the account but did not log in during the specified time period. Each line includes:
deleted_users.ndjson
Contains audit log entries for users who logged in during the specified time period but no longer exist in the account.
Analyze the output
Process the NDJSON files with command-line tools when you need a quick count, or with Python when you need to feed the results into another system.
To count the records in each category, use wc:
To extract email addresses, use jq:
To process the records programmatically, read them in Python:
For accounts with many users or extensive audit history, the script can take several minutes to complete. It paginates through the data, fetching up to 1000 audit log entries or 100 users per page, and prints progress as it runs.
Complete script
Save the following as get_inactive_users.py.
Troubleshooting
Match the error the script prints to the corresponding fix.
Related articles
Manage users: Add, edit, and delete users, and act on the inactive accounts this script identifies.
Audit trail: Review the audit events that this script queries.
Manage API keys: Create the token the script uses to authenticate.
Harness API quickstart: Understand how to authenticate and call Harness APIs.
Last updated
Was this helpful?