0

List translations by key

by
Published Oct 17, 2025

List translations for a specific key.

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 translations by key
8
 * List translations for a specific key.
9
 */
10
export async function main(
11
	auth: Phrase,
12
	project_id: string,
13
	key_id: string,
14
	page: string | undefined,
15
	per_page: string | undefined,
16
	branch: string | undefined,
17
	sort: string | undefined,
18
	order: string | undefined,
19
	q: string | undefined
20
) {
21
	const url = new URL(`${auth.baseUrl}/projects/${project_id}/keys/${key_id}/translations`)
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
	]) {
30
		if (v !== undefined && v !== '' && k !== undefined) {
31
			url.searchParams.append(k, v)
32
		}
33
	}
34
	const response = await fetch(url, {
35
		method: 'GET',
36
		headers: {
37
			Authorization: 'ApiToken ' + auth.token
38
		},
39
		body: undefined
40
	})
41
	if (!response.ok) {
42
		const text = await response.text()
43
		throw new Error(`${response.status} ${text}`)
44
	}
45
	return await response.json()
46
}
47