Working with PagerDuty :: How to fetch audit records from PagerDuty for a month using rest API and Node JS
PagerDuty is a powerful incident management and response platform widely used for managing IT alerts and orchestrating on-call workflows. As an IT infrastructure engineer or developer, you might need to access and analyze audit records to track system changes, ensure compliance, or troubleshoot issues. Fortunately, PagerDuty provides a REST API that makes it straightforward to programmatically fetch audit records.
In this guide, we'll explore how to retrieve a month's worth of audit records from PagerDuty using the REST API and Node.js. We'll cover everything from setting up your API access keys to implementing efficient pagination for large datasets, ensuring you can effortlessly pull the data you need for analysis or reporting. Whether you're a seasoned developer or just getting started with PagerDuty integrations, this walkthrough will equip you with the skills to work effectively with its API.
Steps to Create a Read-Only API Key for Fetching Audit Records from PagerDuty
-
Log in to PagerDuty
- Access your PagerDuty account by logging in with your credentials.
- Ensure you have the necessary administrative privileges to manage API keys.
-
Navigate to API Access Keys
- In the PagerDuty dashboard, click on your profile avatar (usually in the top-right corner).
- Select Account Settings from the dropdown menu.
- In the settings menu, find and click API Access Keys under the Developers section.
-
Create a New API Key
- Click the Create New API Key button.
- In the dialog box, provide a descriptive name for the API key (e.g., "Read-Only Key for Audit Records").
-
Set Permissions
- Ensure the key has Read-Only permissions.
- This restricts the API key to fetching data without allowing it to modify any PagerDuty resources.
-
Save the API Key
- Once you’ve set the permissions, click Create Key to generate the API key.
- Copy the API key to a secure location as it will only be displayed once.
-
Store the API Key Securely
- Use a secure method to store the key, such as environment variables, a secrets manager, or an encrypted file.
- Avoid hardcoding the key directly in your source code.
-
Verify API Access
- Test the API key by making a simple
GET
request to the PagerDuty REST API (e.g., the/users
or/audit/records
endpoint) using tools like Postman or a basic Node.js script. - Ensure the API key works as intended and only retrieves data without modification permissions.
- Test the API key by making a simple
Once your API key is ready, you can use it to fetch audit records programmatically via the REST API in your Node.js application.
Node JS script for fetching audit record for a month and save in a CSV file :-
This code retrieves audit records from PagerDuty for a specified date range using the REST API and processes them for reporting. Here's a breakdown:
-
Fetch Audit Records: The
fetchAuditRecords
function usesaxios
to send paginated requests to the PagerDuty API and retrieve audit records based on the provided start and end dates. -
Process Records: The
processRecords
function formats the fetched records into a structured format, including details like changes made, actor information, and resource summaries. -
Save Records to CSV: The
saveToCSV
function converts the processed records into CSV format usingjson2csv
and saves the file locally. -
Main Execution: The
main
function coordinates the process by fetching records, formatting them, and saving the results to a CSV file. It also logs sample records for verification.
This script is ideal for exporting and analyzing PagerDuty audit logs programmatically.
Comments
Post a Comment