Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to filter alerts by time period in api integration using python

agilsrinivasan
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 14, 2025

I am trying to filter Alerts using API integration using my query and and list the alerts which appeared only in the last hour.

I am using the below code but it is not filtering based on the "createdAtSince" criteria:

import opsgenie_sdk
import requests
from datetime import datetime, timedelta, timezone

# Replace with your actual API key
API_KEY = "API_KEY"

headers = {
"Authorization": f"GenieKey {API_KEY}",
"Content-Type": "application/json"
}



# Create time window for alerts created approx. 1 minute ago (30 sec window)
end_time = datetime.now(timezone.utc)
start_time = end_time - timedelta(seconds=3600)



# Convert to milliseconds since epoch
start_epoch_ms = int(start_time.timestamp() * 1000)
end_epoch_ms = int(end_time.timestamp() * 1000)

params = {
"limit": 100,
"offset": 0,
"createdAtSince": start_epoch_ms,
"createdAtUntil": end_epoch_ms
}

params["query"] = """priority:(P1 or P2) AND NOT application:("Cloud Sync Module") AND detailsPair(severity:CRITICAL) AND NOT issueKey AND NOT incident-alert-type:Associated AND detailsPair(noc_bypass:false) AND (detailsPair(noc_desk:"Cluster 1") OR detailsPair(noc_desk:"Cluster 2") OR detailsPair(noc_desk:"Cluster 3")) AND (responders:"I_EXT_SERVICESUPPORT_TIERIANDII" OR responders:"Automation_Alert_Delay_TIERIANDII_DG") AND (teams:"Automation_Alert_Delay_TIERIANDII_DG" OR teams:"I_EXT_SERVICESUPPORT_TIERIANDII")"""

response = requests.get(BASE_URL, headers=headers, params=params)

if response.status_code == 200:
alerts = response.json().get('data', [])
for alert in alerts:
print(f"ID: {alert['id']} | Message: {alert['message']} | Status: {alert['status']}")
else:
print(f"Failed to fetch alerts: {response.status_code} - {response.text}")

1 answer

0 votes
Jehan Bhathena
Community Champion
June 15, 2025

Hi @agilsrinivasan 

since your question is more technical in nature you might get better I believe it might be better responded here https://community.developer.atlassian.com/, this the developer community link.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
ENTERPRISE
TAGS
AUG Leaders

Atlassian Community Events