0

Create a new saved query

by
Published Oct 17, 2025

Create a new saved query for the given organization.

Script sentry Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Create a new saved query
4
 * Create a new saved query for the given organization.
5
 */
6
export async function main(auth: RT.Sentry, body: Body) {
7
	const url = new URL(
8
		`https://${auth.region}.sentry.io/api/0/organizations/${auth.organizationSlug}/discover/saved/`
9
	)
10

11
	const response = await fetch(url, {
12
		method: 'POST',
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 user-defined saved query name.
36
	 */
37
	name: string
38
	/**
39
	 * The saved projects filter for this query.
40
	 */
41
	projects?: number[]
42
	/**
43
	 * The dataset you would like to query. Note: `discover` is a **deprecated** value. The allowed values are: `error-events`, `transaction-like`
44
	 *
45
	 * * `discover`
46
	 * * `error-events`
47
	 * * `transaction-like`
48
	 */
49
	queryDataset?: 'discover' | 'error-events' | 'transaction-like'
50
	/**
51
	 * The saved start time for this saved query.
52
	 */
53
	start?: string
54
	/**
55
	 * The saved end time for this saved query.
56
	 */
57
	end?: string
58
	/**
59
	 * The saved time range period for this saved query.
60
	 */
61
	range?: string
62
	/**
63
	 * The fields, functions, or equations that can be requested for the query. At most 20 fields can be selected per request. Each field can be one of the following types:
64
	 * - A built-in key field. See possible fields in the [properties table](/product/sentry-basics/search/searchable-properties/#properties-table), under any field that is an event property.
65
	 *     - example: `field=transaction`
66
	 * - A tag. Tags should use the `tag[]` formatting to avoid ambiguity with any fields
67
	 *     - example: `field=tag[isEnterprise]`
68
	 * - A function which will be in the format of `function_name(parameters,...)`. See possible functions in the [query builder documentation](/product/discover-queries/query-builder/#stacking-functions).
69
	 *     - when a function is included, Discover will group by any tags or fields
70
	 *     - example: `field=count_if(transaction.duration,greater,300)`
71
	 * - An equation when prefixed with `equation|`. Read more about [equations here](/product/discover-queries/query-builder/query-equations/).
72
	 *     - example: `field=equation|count_if(transaction.duration,greater,300) / count() * 100`
73
	 *
74
	 */
75
	fields?: string[]
76
	/**
77
	 * How to order the query results. Must be something in the `field` list, excluding equations.
78
	 */
79
	orderby?: string
80
	/**
81
	 * The name of environments to filter by.
82
	 */
83
	environment?: string[]
84
	/**
85
	 * Filters results by using [query syntax](/product/sentry-basics/search/).
86
	 */
87
	query?: string
88
	/**
89
	 * Aggregate functions to be plotted on the chart.
90
	 */
91
	yAxis?: string[]
92
	/**
93
	 * Visualization type for saved query chart. Allowed values are:
94
	 * - default
95
	 * - previous
96
	 * - top5
97
	 * - daily
98
	 * - dailytop5
99
	 * - bar
100
	 *
101
	 */
102
	display?: string
103
	/**
104
	 * Number of top events' timeseries to be visualized.
105
	 */
106
	topEvents?: number
107
	/**
108
	 * Resolution of the time series.
109
	 */
110
	interval?: string
111
	[k: string]: unknown
112
}
113