0

List keys

by
Published Oct 17, 2025

List all keys for the given project. Alternatively you can POST requests to /search.

Script phrase Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Phrase = {
3
	token: string
4
	baseUrl: string
5
}
6
/**
7
 * List keys
8
 * List all keys for the given project. Alternatively you can POST requests to /search.
9
 */
10
export async function main(
11
	auth: Phrase,
12
	project_id: string,
13
	page: string | undefined,
14
	per_page: string | undefined,
15
	branch: string | undefined,
16
	sort: string | undefined,
17
	order: string | undefined,
18
	q: string | undefined,
19
	locale_id: string | undefined
20
) {
21
	const url = new URL(`${auth.baseUrl}/projects/${project_id}/keys`)
22
	for (const [k, v] of [
23
		['page', page],
24
		['per_page', per_page],
25
		['branch', branch],
26
		['sort', sort],
27
		['order', order],
28
		['q', q],
29
		['locale_id', locale_id]
30
	]) {
31
		if (v !== undefined && v !== '' && k !== undefined) {
32
			url.searchParams.append(k, v)
33
		}
34
	}
35
	const response = await fetch(url, {
36
		method: 'GET',
37
		headers: {
38
			Authorization: 'ApiToken ' + auth.token
39
		},
40
		body: undefined
41
	})
42
	if (!response.ok) {
43
		const text = await response.text()
44
		throw new Error(`${response.status} ${text}`)
45
	}
46
	return await response.json()
47
}
48