0

Update a metric alert rule

by
Published Oct 17, 2025

Updates a metric alert rule.

Script sentry Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Update a metric alert rule
4
 * Updates a metric alert rule.
5
 */
6
export async function main(auth: RT.Sentry, alert_rule_id: string, body: Body) {
7
	const url = new URL(
8
		`https://${auth.region}.sentry.io/api/0/organizations/${auth.organizationSlug}/alert-rules/${alert_rule_id}/`
9
	)
10

11
	const response = await fetch(url, {
12
		method: 'PUT',
13
		headers: {
14
			'Content-Type': 'application/json',
15
			Authorization: 'Bearer ' + auth.token
16
		},
17
		body: JSON.stringify(body)
18
	})
19
	if (!response.ok) {
20
		const text = await response.text()
21
		throw new Error(`${response.status} ${text}`)
22
	}
23
	return await response.json()
24
}
25

26
/* eslint-disable */
27
/**
28
 * This file was automatically generated by json-schema-to-typescript.
29
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
30
 * and run json-schema-to-typescript to regenerate this file.
31
 */
32

33
export interface Body {
34
	/**
35
	 * The name for the rule.
36
	 */
37
	name: string
38
	/**
39
	 * A string representing the aggregate function used in this alert rule. Valid aggregate functions are `count`, `count_unique`, `percentage`, `avg`, `apdex`, `failure_rate`, `p50`, `p75`, `p95`, `p99`, `p100`, and `percentile`. See **Metric Alert Rule Types** under [Create a Metric Alert Rule](/api/alerts/create-a-metric-alert-rule-for-an-organization/#metric-alert-rule-types) for valid configurations.
40
	 */
41
	aggregate: string
42
	/**
43
	 * The time period to aggregate over.
44
	 *
45
	 * * `1` - 1 minute
46
	 * * `5` - 5 minutes
47
	 * * `10` - 10 minutes
48
	 * * `15` - 15 minutes
49
	 * * `30` - 30 minutes
50
	 * * `60` - 1 hour
51
	 * * `120` - 2 hours
52
	 * * `240` - 4 hours
53
	 * * `1440` - 24 hours
54
	 */
55
	timeWindow: 1 | 5 | 10 | 15 | 30 | 60 | 120 | 240 | 1440
56
	/**
57
	 * The names of the projects to filter by.
58
	 */
59
	projects: string[]
60
	/**
61
	 * An event search query to subscribe to and monitor for alerts. For example, to filter transactions so that only those with status code 400 are included, you could use `"query": "http.status_code:400"`. Use an empty string for no filter.
62
	 */
63
	query: string
64
	/**
65
	 * The comparison operator for the critical and warning thresholds. The comparison operator for the resolved threshold is automatically set to the opposite operator. When a percentage change threshold is used, `0` is equivalent to "Higher than" and `1` is equivalent to "Lower than".
66
	 *
67
	 * * `0` - Above
68
	 * * `1` - Below
69
	 */
70
	thresholdType: 0 | 1
71
	/**
72
	 *
73
	 * A list of triggers, where each trigger is an object with the following fields:
74
	 * - `label`: One of `critical` or `warning`. A `critical` trigger is always required.
75
	 * - `alertThreshold`: The value that the subscription needs to reach to trigger the
76
	 * alert rule.
77
	 * - `actions`: A list of actions that take place when the threshold is met.
78
	 * ```json
79
	 * triggers: [
80
	 *     {
81
	 *         "label": "critical",
82
	 *         "alertThreshold": 100,
83
	 *         "actions": [
84
	 *             {
85
	 *                 "type": "email",
86
	 *                 "targetType": "user",
87
	 *                 "targetIdentifier": "23489853",
88
	 *                 "inputChannelId": None
89
	 *                 "integrationId": None,
90
	 *                 "sentryAppId": None
91
	 *             }
92
	 *         ]
93
	 *     },
94
	 *     {
95
	 *         "label": "warning",
96
	 *         "alertThreshold": 75,
97
	 *         "actions": []
98
	 *     }
99
	 * ]
100
	 * ```
101
	 * Metric alert rule trigger actions follow the following structure:
102
	 * - `type`: The type of trigger action. Valid values are `email`, `slack`, `msteams`, `pagerduty`, `sentry_app`, `sentry_notification`, and `opsgenie`.
103
	 * - `targetType`: The type of target the notification will be sent to. Valid values are `specific`, `user`, `team`, and `sentry_app`.
104
	 * - `targetIdentifier`: The ID of the target. This must be an integer for PagerDuty and Sentry apps, and a string for all others. Examples of appropriate values include a Slack channel name (`#my-channel`), a user ID, a team ID, a Sentry app ID, etc.
105
	 * - `inputChannelId`: The ID of the Slack channel. This is only used for the Slack action, and can be used as an alternative to providing the `targetIdentifier`.
106
	 * - `integrationId`: The integration ID. This is required for every action type except `email` and `sentry_app.`
107
	 * - `sentryAppId`: The ID of the Sentry app. This is required when `type` is `sentry_app`.
108
	 * - `priority`: The severity of the Pagerduty alert or the priority of the Opsgenie alert (optional). Defaults for Pagerduty are `critical` for critical and `warning` for warning. Defaults for Opsgenie are `P1` for critical and `P2` for warning.
109
	 *
110
	 */
111
	triggers: unknown[]
112
	/**
113
	 * The name of the environment to filter by. Defaults to all environments.
114
	 */
115
	environment?: string
116
	/**
117
	 * The name of the dataset that this query will be executed on. Valid values are `events`, `transactions`, `metrics`, `sessions`, and `generic-metrics`. Defaults to `events`. See **Metric Alert Rule Types** under [Create a Metric Alert Rule](/api/alerts/create-a-metric-alert-rule-for-an-organization/#metric-alert-rule-types) for valid configurations.
118
	 */
119
	dataset?: string
120
	/**
121
	 * The type of query. If no value is provided, `queryType` is set to the default for the specified `dataset.` See **Metric Alert Rule Types** under [Create a Metric Alert Rule](/api/alerts/create-a-metric-alert-rule-for-an-organization/#metric-alert-rule-types) for valid configurations.
122
	 *
123
	 * * `0` - event.type:error
124
	 * * `1` - event.type:transaction
125
	 * * `2` - None
126
	 */
127
	queryType?: 0 | 1 | 2
128
	/**
129
	 * List of event types that this alert will be related to. Valid values are `default` (events captured using [Capture Message](/product/sentry-basics/integrate-backend/capturing-errors/#capture-message)), `error` and `transaction`.
130
	 */
131
	eventTypes?: string[]
132
	/**
133
	 * An optional int representing the time delta to use as the comparison period, in minutes. Required when using a percentage change threshold ("x%" higher or lower compared to `comparisonDelta` minutes ago). A percentage change threshold cannot be used for [Crash Free Session Rate](/api/alerts/create-a-metric-alert-rule-for-an-organization/#crash-free-session-rate) or [Crash Free User Rate](/api/alerts/create-a-metric-alert-rule-for-an-organization/#crash-free-user-rate).
134
	 */
135
	comparisonDelta?: number
136
	/**
137
	 * Optional value that the metric needs to reach to resolve the alert. If no value is provided, this is set automatically based on the lowest severity trigger's `alertThreshold`. For example, if the alert is set to trigger at the warning level when the number of errors is above 50, then the alert would be set to resolve when there are less than 50 errors. If `thresholdType` is `0`, `resolveThreshold` must be greater than the critical threshold. Otherwise, it must be less than the critical threshold.
138
	 */
139
	resolveThreshold?: number
140
	/**
141
	 * The ID of the team or user that owns the rule.
142
	 */
143
	owner?: string
144
	[k: string]: unknown
145
}
146